Spring Bean Definition And Everything Annotation Project

Click here to download eclipse supported ZIP file



This is AllBeansDefinition.java file having the source code to execute business logic.


 

    
/**
 
 */
package com.cv.spring.annotation.purejava;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
@author Chandra Vardhan
*
*/
@Configuration
public class AllBeansDefinition {
  
  private final static Logger LOGGER = Logger.getLogger(AllBeansDefinition.class);


  public AllBeansDefinition() {
    LOGGER.info("AllBeansDefinition() 0-arg cons...");
  }

  @Bean
  public FirstBean getFirstBeanInstance() {
    return new FirstBean();
  }

  @Bean
  public SecondBean getSecondBeanInstance() {
    return new SecondBean();
  }

  @Bean
  public ThirdBean getThirdBeanInstance() {
    return new ThirdBean();
  }  

}


This is AnnotationTest.java file having the source code to execute business logic.


 

    
package com.cv.spring.annotation.purejava;

import org.apache.log4j.Logger;
import org.springframework.context.support.ClassPathXmlApplicationContext;



/**
@author Chandra Vardhan
*
*/
public class AnnotationTest {
  
  private final static Logger LOGGER = Logger.getLogger(AnnotationTest.class);


  public AnnotationTest() {

  }

  public static void main(String[] args) {

    LOGGER.info("BeanTest class...");

    String configLocation = "spring-config.xml";

    ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
        configLocation);

    String name = "getFirstBeanInstance";

    FirstBean firstBean = (FirstBeanapplicationContext
        .getBean(name);

    LOGGER.info(firstBean);

  }

}


This is AnnotationTest2.java file having the source code to execute business logic.


 

    
package com.cv.spring.annotation.purejava;

import org.apache.log4j.Logger;
import org.springframework.context.support.ClassPathXmlApplicationContext;


/**
@author Chandra Vardhan
*
*/
public class AnnotationTest2 {

  private final static Logger LOGGER = Logger.getLogger(AnnotationTest2.class);

  /**
   @param args
   */
  public static void main(String[] args) {

    LOGGER.info("BeanTest class...");

    String configLocation = "spring-config.xml";

    ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
        configLocation);

    String name = "poeticDuke";

    Performer performer = (PerformerapplicationContext
        .getBean(name);

    LOGGER.info(performer.getClass());
    
    performer.perform();

  }

}


This is BeanAnnotation.java file having the source code to execute business logic.


 

    

package com.cv.spring.annotation.purejava;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 @author Chandra Vardhan
 
 */
@Configuration
public class BeanAnnotation {
  
  private final static Logger LOGGER = Logger.getLogger(BeanAnnotation.class);


  @Bean
  public Performer duke() {
    LOGGER.info("duke() method");
    return new Juggler();
  }

  @Bean
  public Performer duke15() {
    LOGGER.info("duke15() method");
    return new Juggler(15);
  }

  @Bean
  public Performer kenny() {
    LOGGER.info("kenny() method");
    Instrumentalist kenny = new Instrumentalist();
    kenny.setSong("Jingle Bells");
    return kenny;
  }

  @Bean
  public Poem sonnet29() {
    LOGGER.info("sonnet29() method");
    return new Sonnet29();
  }

  @Bean
  public Performer poeticDuke() {
    LOGGER.info("poeticDuke() method");
    return new PoeticJuggler(sonnet29());
  }
}


This is FirstBean.java file having the source code to execute business logic.


 

    
package com.cv.spring.annotation.purejava;

import org.apache.log4j.Logger;

/**
@author Chandra Vardhan
*
*/
public class FirstBean {
  
  private final static Logger LOGGER = Logger.getLogger(FirstBean.class);

  public FirstBean() {
    
    LOGGER.info("Am from FirstBean() 0-arg cons...");
  }
  
  
}


This is Instrumentalist.java file having the source code to execute business logic.


 

    

package com.cv.spring.annotation.purejava;

import org.apache.log4j.Logger;

/**
 @author Chandra Vardhan
 
 */

public class Instrumentalist implements Performer {

  private final static Logger LOGGER = Logger.getLogger(Instrumentalist.class);
  
  private String song;

  public String getSong() {

    return song;
    
  }

  public Instrumentalist() {

    LOGGER.info("Instrumentalist() ... 0-arg cons ... class");
  }

  public void setSong(String string) {

    song = string;

  }

  @Override
  public void perform() {

    LOGGER.info("perform() method ...Instrumentalist class");
    
  }

}


This is Juggler.java file having the source code to execute business logic.


 

    
/**
 
 */
package com.cv.spring.annotation.purejava;

import org.apache.log4j.Logger;

/**
 @author Chandra Vardhan
 *
 */

public class Juggler implements Performer {

  private final static Logger LOGGER = Logger.getLogger(Juggler.class);

  private int i;

  public Juggler() {
    LOGGER.info("Juggler() 0-arg..cons..class");
  }

  /**
   @param i
   
   */

  public Juggler(int i) {

    this.i = i;

    LOGGER.info("the value of i is : " + i);
  }

  @Override
  public void perform() {

    LOGGER.info("perform() from Juggler class...");

  }

}


This is Performer.java file having the source code to execute business logic.


 

    
/**
 
 */
package com.cv.spring.annotation.purejava;

/**
 @author Chandra Vardhan
 *
 */

public interface Performer {
  
  void perform();

}


This is Poem.java file having the source code to execute business logic.


 

    
package com.cv.spring.annotation.purejava;

/**
 @author Chandra Vardhan
 
 */

public interface Poem {

  void recite();

}


This is PoeticJuggler.java file having the source code to execute business logic.


 

    
package com.cv.spring.annotation.purejava;

import org.apache.log4j.Logger;

/**
 @author Chandra Vardhan
 
 */

public class PoeticJuggler implements Performer {
  
  private final static Logger LOGGER = Logger.getLogger(PoeticJuggler.class);

  private Poem sonnet29;

  public PoeticJuggler() {

    LOGGER.info("PoeticJuggler() 0-arg cons...");
  }

  public PoeticJuggler(Poem sonnet29) {

    this.sonnet29 = sonnet29;

  }

  /*
   * (non-Javadoc)
   
   * @see com.cv.annotation.purejava.Performer#perform()
   */
  @Override
  public void perform() {

    sonnet29.recite();
    
    LOGGER.info("perform() from PoeticJuggler class...");
    
    

  }

}


This is SecondBean.java file having the source code to execute business logic.


 

    
/**
 
 */
package com.cv.spring.annotation.purejava;

import org.apache.log4j.Logger;

/**
 @author Chandra Vardhan
 *
 */

public class SecondBean {

  private final static Logger LOGGER = Logger.getLogger(SecondBean.class);

  
  public SecondBean() {
    
    LOGGER.info("Am from SecondBean() 0-arg cons...");
    
  }

}


This is Sonnet29.java file having the source code to execute business logic.


 

    
package com.cv.spring.annotation.purejava;

import org.apache.log4j.Logger;

/**
 @author Chandra Vardhan
 *
 */

public class Sonnet29 implements Poem {

  private final static Logger LOGGER = Logger.getLogger(Sonnet29.class);

  public Sonnet29() {

    LOGGER.info("Sonnet29 class 0-arg cons...");

  }

  @Override
  public void recite() {
  
    LOGGER.info("recite() from Sonnet29 class");
  }

}


This is ThirdBean.java file having the source code to execute business logic.


 

    
/**
 
 */
package com.cv.spring.annotation.purejava;

import org.apache.log4j.Logger;

/**
 @author Chandra Vardhan
 *
 */

public class ThirdBean {
  
  private final static Logger LOGGER = Logger.getLogger(ThirdBean.class);


  public ThirdBean() {
    
    LOGGER.info("Am from ThirdBean() 0-arg cons...");
  }

}




This is pom.xml file having the entries of dependency jars and information to build the application .


	
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">.0" encoding="UTF-8"?> <modelVersion>4.0.0</modelVersion> <groupId>com.cv.spring.annotation</groupId> <artifactId>SpringBeanDefinitionAndEverythingAnnotation</artifactId> <name>SpringBeanDefinitionAndEverythingAnnotation</name> <packaging>jar</packaging> <version>1.0</version> <properties> <java-version>1.8</java-version> <log4j-version>1.2.16</log4j-version> <org.springframework-version>4.1.6.RELEASE</org.springframework-version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>


This is spring-config.xml file having the spring configuration properties.


 
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.cv.spring.annotation.purejava"/>
</beans>



This is log4j.properties file having the entries for logging the information into the console/file.



#By default enabling Console appender
# Root logger option
log4j.rootLogger=INFO, stdout

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p [%c]:%L -->> %m%n

# Redirect log messages to a log file
#log4j.appender.file=org.apache.log4j.RollingFileAppender
#log4j.appender.file.File=C:\servlet-application.log
#log4j.appender.file.MaxFileSize=5MB
#log4j.appender.file.MaxBackupIndex=10
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n


No comments:

Post a Comment