Spring Autowire ByType ByName Constuctor

spring+autowire+bytype+byname+constuctor

Click here to download eclipse supported ZIP file



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


 

    
package com.cv.spring.annotation.autowire;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

  /**
   @param args
   */
  public static void main(String[] args) {
    String configLocation = "autoDetect.xml";
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
        configLocation);
    String name = "kenny";
    Performer performer = (PerformerapplicationContext.getBean(name);
    performer.perform();
  }

}

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


 

    
package com.cv.spring.annotation.autowire;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

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

    String configLocation = "byConstructor.xml";

    ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
        configLocation);

    String name = "instrumentList";

    Performer performer = (PerformerapplicationContext.getBean(name);

    performer.perform();

  }

}

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


 

    
package com.cv.spring.annotation.autowire;

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

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


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

    String configLocation = "byName.xml";

    ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
        configLocation);

    String name = "kenny";

    Performer performer = (PerformerapplicationContext.getBean(name);

    performer.perform();

    LOGGER.info("============================================");
    

  }

}

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


 

    
package com.cv.spring.annotation.autowire;

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

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

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

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

    String configLocation = "byType.xml";

    ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
        configLocation);

    String name = "kenny";

    Performer performer = (PerformerapplicationContext.getBean(name);

    performer.perform();

    LOGGER.info("============================================");
  

  }

}

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


 

    
package com.cv.spring.annotation.autowire;

import org.apache.log4j.Logger;


/**
 @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");

  }

}

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


 

    
package com.cv.spring.annotation.autowire;

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.annotation.autowire;

/**
 @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.annotation.autowire;

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

/**
 @author Chandra Vardhan
 
 */

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

  
  private String song;
  private Instrument instrument;

  public InstrumentList(Instrument instrument) {    

    this.instrument =  instrument;
  }
  public InstrumentList() {    

  }
  public Instrument getInstrument() {
    return instrument;
  }

  @Autowired
  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.annotation.autowire;

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 Performer.java file having the source code to execute business logic.


 

    
package com.cv.spring.annotation.autowire;

/**
 @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.annotation.autowire;

import org.apache.log4j.Logger;

/**
 @author Chandra Vardhan
 
 */

public class Piano implements Instrument {
  
  private final static Logger LOGGER = Logger.getLogger(Piano.class);

  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.annotation.autowire;


/**
 @author Chandra Vardhan
 
 */

public interface Poem {

  public void recite();

}



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>SpringAutowireByTypeByNameConstuctor</artifactId> <name>SpringAutowireByTypeByNameConstuctor</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 autoDetect.xml spring configuration file and these entries are used in the application.


 
<?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"
default-autowire="byType">
<bean id="kenny" class="com.cv.spring.annotation.autowire.InstrumentList">
<property name="song" value="JingleBells" />
</bean>
<bean name="instrument" class="com.cv.spring.annotation.autowire.Gitter" />
<bean name="harmonia" class="com.cv.spring.annotation.autowire.Harmonia"
primary="true" />
<bean name="piano" class="com.cv.spring.annotation.autowire.Piano" />
</beans>

This is byConstructor.xml spring configuration file and these entries are used in the application.


 
<?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">
<bean id="instrumentList" class="com.cv.spring.annotation.autowire.InstrumentList"
autowire="constructor">
<property name="song" value="JingleBells" />
</bean>

<bean name="piano" class="com.cv.spring.annotation.autowire.Piano" />
<bean name="instrument" class="com.cv.spring.annotation.autowire.Gitter" />
<bean name="piano2" class="com.cv.spring.annotation.autowire.Piano"
autowire-candidate="false" />
</beans>

This is byName.xml spring configuration file and these entries are used in the application.


 
<?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">

<bean id="kenny" class="com.cv.spring.annotation.autowire.InstrumentList" autowire="byName">
<property name="song" value="JingleBells" />
</bean>

<bean name="piano" class="com.cv.spring.annotation.autowire.Piano" primary="true"/>
<bean name="instrument" class="com.cv.spring.annotation.autowire.Gitter" />
<bean name="piano2" class="com.cv.spring.annotation.autowire.Piano" autowire-candidate="false"/>
</beans>

This is byType.xml spring configuration file and these entries are used in the application.


 
<?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">
<bean id="kenny" class="com.cv.spring.annotation.autowire.InstrumentList" autowire="byType">
<property name="song" value="JingleBells" />
</bean>
<bean name="instrument" class="com.cv.spring.annotation.autowire.Gitter" />
<bean name="harmonia" class="com.cv.spring.annotation.autowire.Harmonia" primary="true" />
<bean name="piano" class="com.cv.spring.annotation.autowire.Piano" />
</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