Spring Autowire Bean Definition Project

Click here to download eclipse supported ZIP file



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


 

    
package com.cv.spring.autowire.annotation;

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

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

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

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

    String configLocation = "annotation.xml";

    ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
        configLocation);

    String name = "duke";

    PoeticJugg performer = (PoeticJuggapplicationContext.getBean(name);

    performer.perform();

    LOGGER.info("====================================");
    
    String name2 = "kenny";

    Performer performer2 = (PerformerapplicationContext.getBean(name2);

    performer2.perform();

  }

}


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


 

    
package com.cv.spring.autowire.annotation;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

@Component
/**
@author Chandra Vardhan
*
*/
public class Bean {
  

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

  private String name;

  private int age;

  private String college;

  
  private Poem poem;
  
  
  private Instrument instrument;
  
  public Instrument getInstrument() {
    return instrument;
  }

  public void setInstrument(Instrument instrument) {
    this.instrument = instrument;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    this.age = age;
  }

  public String getCollege() {
    return college;
  }

  public void setCollege(String college) {
    this.college = college;
  }

  public Bean() {
    LOGGER.info("Bean()");
  }

  public void perform() {
    
    LOGGER.info("perform method...Bean");
    
    LOGGER.info("Bean [name=" + name + ", age=" + age + ", college=" + college
        "]");
    
    poem.recite();
    LOGGER.info(instrument);
    
    instrument.play();
    
  }

  
  public Poem getPoem() {
    return poem;
  }

  public void setPoem(Poem poem) {
    this.poem = poem;
  }
  
  @Override
  public String toString() {
    return "Bean [name=" + name + ", age=" + age + ", college=" + college
        "]";
  }

  

}


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


 

    
package com.cv.spring.autowire.annotation;

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

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

  public BeanTest() {
    LOGGER.info("BeanTest()");
  }

  /**
   @param args
   */
  public static void main(String[] args) {
    
    LOGGER.info("BeanTest class...");
    
    String configLocation = "bean2.xml";

    ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
        configLocation);

    String name = "bean";

    Bean performer = (BeanapplicationContext.getBean(name);

    performer.perform();
  

  }

}


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


 

    
package com.cv.spring.autowire.annotation;

import org.apache.log4j.Logger;

//@Qualifier("stringed")

//@StringedInstrument2
//@Strummed

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

  public Gitter() {
    
  }
  
  public void play() {
    
    LOGGER.info("TO TO TI TI  TE TE...TOOOOOO...Gitter");

  }

}


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


 

    
package com.cv.spring.autowire.annotation;

import org.apache.log4j.Logger;

/**
@author Chandra Vardhan
*
*/
public class Harmonia implements Instrument {
  
  private final static Logger LOGGER = Logger.getLogger(Harmonia.class);
  
  public Harmonia() {
    
  }
  
  public void play() {
    
    LOGGER.info("HAR HAR...moooo moooooo...Harmonia");

  }

}


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


 

    
package com.cv.spring.autowire.annotation;

/**
@author Chandra Vardhan
*
*/
public interface Instrument {

   void play();

}


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


 

    
package com.cv.spring.autowire.annotation;


import javax.inject.Inject;
import javax.inject.Named;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;


/**
@author Chandra Vardhan
*
*/
public class InstrumentList implements Performer {
  
  private final static Logger LOGGER = Logger.getLogger(InstrumentList.class);
  
  @Value("jab jab theare paas me aaya...")
//  @Value("#{systemProperties.myFavoriteSong}")
  private String song;
  
//  @Qualifier("stringed")//@Qualifier("gitter")
  
  @Inject
  @Named("gitter")
  private Instrument instrument;

  public InstrumentList() {

  }

  public Instrument getInstrument() {
    return instrument;
  }

  public void setInstrument(Instrument instrument) {
    this.instrument = instrument;
  }

  public String getSong() {
    return song;
  }
  
  public void setSong(String song) {
    this.song = song;
  }

  public void perform() {

    LOGGER.info("Playing " + song + " : ");
    instrument.play();

  }

}


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


 

    
package com.cv.spring.autowire.annotation;

import org.apache.log4j.Logger;

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

  private int beanBags = 3;

  public Jugg() {

  }

  public Jugg(int beanBags) {

    this.beanBags = beanBags;
  }

  public void perform() {
    
    LOGGER.info("JUGGLING " + beanBags + " BEANBAGS");

  }

}


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


 

    
package com.cv.spring.autowire.annotation;

import org.apache.log4j.Logger;

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

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


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


 

    
package com.cv.spring.autowire.annotation;

import org.apache.log4j.Logger;


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

  /**
   
   */
  public Lesson() {
    LOGGER.info("Lesson() cons...");
  }

}


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


 

    
package com.cv.spring.autowire.annotation;



/**
@author Chandra Vardhan
*
*/
public interface Performer {


  void perform();
}


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


 

    
package com.cv.spring.autowire.annotation;

import org.apache.log4j.Logger;

/**
@author Chandra Vardhan
*
*/
public class Piano implements Instrument {
  
  private final static Logger LOGGER = Logger.getLogger(Piano.class);
  
  public Piano() {
    
  }
  
  public void play() {
    
    LOGGER.info("PINK PINK PUKK PUKKKKK POOOOOOOO...");

  }

}


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


 

    
package com.cv.spring.autowire.annotation;

/**
@author Chandra Vardhan
*
*/
public interface Poem {

  public void recite();

}


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


 

    
package com.cv.spring.autowire.annotation;

import org.apache.log4j.Logger;


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

  private static String[] LINES = {
      "Am chandravardhan, done my gradution in KITS.",
      "Am very much open friendly because I am working with open source technologies.",
      "Am sincerely looking for a partner." };

  public PoemImpl() {

  }

  public void recite() {

    LOGGER.info("Am recited....");

    for (int i = 0; i < LINES.length; i++) {

      LOGGER.info(LINES[i]);
    }
    
  }

}


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


 

    
package com.cv.spring.autowire.annotation;

import org.apache.log4j.Logger;


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

  private static String[] LINES = {
      "Am kodam, interested in womens and their psychology.",  "Am very open friendly because I am working with open source technologies.",
      "Am sincerely looking for a partner who have excellent character." };

  public PoemImpl2() {

  }

  public void recite() {

    LOGGER.info("Am recited....");

    for (int i = 0; i < LINES.length; i++) {

      LOGGER.info(LINES[i]);
    }
    
  }

}


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


 

    
package com.cv.spring.autowire.annotation;

import java.util.HashSet;
import java.util.Set;

import javax.inject.Inject;

import org.apache.log4j.Logger;


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

  int beanBags;
  public int getBeanBags() {
    return beanBags;
  }

  public void setBeanBags(int beanBags) {
    this.beanBags = beanBags;
  }

  private Poem poem2;
  private Lesson lesson;
  private Summary summary;

  private Set<Knife> knives;

  @Inject
  public PoeticJugg(Provider<Knife> knifeProvider, Poem poem2) {

    this.poem2 = poem2;
    knives = new HashSet<Knife>();

    for (int i = 0; i < 5; i++) {
      knives.add(knifeProvider.get());
    }

  }

  private PoeticJugg() {

  }

  private PoeticJugg(Poem poem2) {

    super();
    LOGGER.info("1-args cons.....");
    this.poem2 = poem2;
  }

  private PoeticJugg(Poem poem2, int beanBags) {

    super(beanBags);
    
    this.beanBags = beanBags;
    LOGGER.info("2-args cons.....");

    LOGGER.info("Poem " + poem2);
    this.poem2 = poem2;

  }

  private PoeticJugg(Poem poem2, Lesson lesson, int beanBags) {

    super(beanBags);
    LOGGER.info("3-args cons.....");

    LOGGER.info("Poem " + poem2 + ", Lesson" + lesson);
    this.poem2 = poem2;
    this.lesson = lesson;

  }

//  @Inject
  private PoeticJugg(Poem poem2, Lesson lesson, Summary summary, int beanBags) {

    super(beanBags);
    LOGGER.info("4-args cons.....");

    LOGGER.info("Poem " + poem2 + ", Lesson" + lesson + ", Summary"
        + summary);
    this.poem2 = poem2;

    this.lesson = lesson;

    this.summary = summary;
  }

  public Poem getPoem() {
    return poem2;
  }

  public void setPoem(Poem poem2) {
    this.poem2 = poem2;
  }

  public void perform() {

    super.perform();
    LOGGER.info("while reciting....");
    poem2.recite();

  }

}


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


 

    
package com.cv.spring.autowire.annotation;




/**
@author Chandra Vardhan
*
*/
@SuppressWarnings("hiding")
public interface Provider<Knife> {

  Knife get();

}


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


 

    
package com.cv.spring.autowire.annotation;

import org.apache.log4j.Logger;



/**
@author Chandra Vardhan
*
*/
public class ProviderKnife implements Provider<Knife> {
  
  private final static Logger LOGGER = Logger.getLogger(ProviderKnife.class);

  /**
   
   */
  public ProviderKnife() {

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

  @Override
  public Knife get() {
    LOGGER.info("get()...ProviderKnife");
    return new Knife();
  }

}


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


 

    
package com.cv.spring.autowire.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.beans.factory.annotation.Qualifier;
/**
 @author Chandra Vardhan
 */
@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface StringedInstrument {
}


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


 

    
package com.cv.spring.autowire.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import javax.inject.Qualifier;
/**
 @author Chandra Vardhan
 */
@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface StringedInstrument2 {
}


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


 

    
package com.cv.spring.autowire.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.beans.factory.annotation.Qualifier;
/**
 @author Chandra Vardhan
 */
@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Strummed {
}


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


 

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

import org.apache.log4j.Logger;


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

  /**
   
   */
  public Summary() {
    
    LOGGER.info("Summary() 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>SpringAutowireBeanDefinition</artifactId> <name>SpringAutowireBeanDefinition</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> <dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> <version>1</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 annotation.xml file having the spring configuration properties.


 
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!--
<context:annotation-config/> -->
<context:component-scan base-package="com.cv.spring.autowire.annotation">
</context:component-scan>
<!-- This is required when we are using annotations in our java application... -->
<!-- <bean id="duke" class="com.cv.autowire.PoeticJugg"> <constructor-arg>
<bean id="poem" class="com.cv.autowire.PoemImpl" /> </bean> -->
<bean id="duke" class="com.cv.spring.autowire.annotation.PoeticJugg">
<!-- <constructor-arg name="beanBags" value="99" /> -->

<property name="beanBags" value="99" />
<!-- <property name="poem"></property> -->
</bean>
<bean name="poem" class="com.cv.spring.autowire.annotation.PoemImpl" />
<bean name="poem2" class="com.cv.spring.autowire.annotation.PoemImpl2" />
<bean name="providerKnife" class="com.cv.spring.autowire.annotation.ProviderKnife" />
<bean name="lesson" class="com.cv.spring.autowire.annotation.Lesson" />
<bean name="summary" class="com.cv.spring.autowire.annotation.Summary" />
<bean id="kenny" class="com.cv.spring.autowire.annotation.InstrumentList">
</bean>

<bean name="gitter" class="com.cv.spring.autowire.annotation.Gitter">
<!-- <qualifier value="stringed"/> -->
</bean>
</beans>


This is bean2.xml file having the spring configuration properties.


 
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- <context:annotation-config/> -->
<context:component-scan base-package="com.cv.spring.autowire.annotation.component">
<context:include-filter type="annotation"
expression="com.cv.spring.autowire.annotation.Gitter" />
</context:component-scan>
<bean id="bean" class="com.cv.spring.autowire.annotation.Bean">
<property name="name" value="chote" />
<property name="age" value="43" />
<property name="college" value="KITS" />
<property name="poem" ref="poem"></property>
<property name="instrument" ref="gitter"></property>
</bean>
<bean name="poem" class="com.cv.spring.autowire.annotation.PoemImpl" />
<bean name="gitter" class="com.cv.spring.autowire.annotation.Gitter" />
</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