Spring Basic Qualifier Autowired Annotation Project

Click here to download eclipse supported ZIP file



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


 

    
package com.cv.spring.basic;


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

  void play();

}


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


 

    
package com.cv.spring.basic;

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

import com.cv.spring.basic.model.City;

/**
 @author Chandra Vardhan
 */
public class Instrumentalist implements Performer {
  
  private final static Logger LOGGER = Logger.getLogger(Instrumentalist.class);
  
  @Autowired
  @Qualifier("string2")
  private String song;
  
  @Autowired
  private Instrument instrument;  
  private int age;  
  private City city;
  
  public Instrumentalist() {
  }

  public int getAge() {
    return age;
  }

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

  public Instrumentalist(Instrument instrument) {

    this.instrument = instrument;
  }

  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 City getCity() {
    return city;
  }

  public void setCity(City city) {
    this.city = city;
  }

  public void perform() {
    LOGGER.info("Instrument is");
    instrument.play();
    LOGGER.info("Song : " + song);
  }

}


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


 

    
package com.cv.spring.basic.model;


/**
 @author Chandra Vardhan
 *
 */
public class City {
  
  private String name;

  private String state;
  
  private int population;
  public String getName() {
    return name;
  }
  
  public void setName(String name) {
    this.name = name;
  }
  
  public String getState() {
    return state;
  }
  
  public void setState(String state) {
    this.state = state;
  }
  
  public int getPopulation() {
    return population;
  }
  
  public void setPopulation(int population) {
    this.population = population;
  }
}


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


 

    
package com.cv.spring.basic.model;

import org.apache.log4j.Logger;

import com.cv.spring.basic.Instrument;

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

  public Piano() {
  }

  public void play() {
    LOGGER.info("Piano :::: PLINK PLINK");
  }
}


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


 

    
package com.cv.spring.basic.model;

import org.apache.log4j.Logger;

import com.cv.spring.basic.Instrument;

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

  public Saxophone() {
  }

  public void play() {
    LOGGER.info("Saxophone ::: TOOT TOOT TOOT");
  }
}


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


 

    
package com.cv.spring.basic;

/**
 @author Chandra Vardhan
 */
public interface Performer {
     public void perform();
}


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


 

    
package com.cv.spring.basic;

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


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


  /**
   
   */
  public Test() {
  }

  /**
   @param args
   */
  public static void main(String[] args) {
    ConfigurableApplicationContext applicationContext = new ClassPathXmlApplicationContext(
        "spring-config.xml");
    Performer performer = (PerformerapplicationContext.getBean("instrumentalist");
    performer.perform();

  }

}




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>SpringBasciQualifierAutowiredAnnotation</artifactId> <name>SpringBasciQualifierAutowiredAnnotation</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.


 
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd"
default-init-method="make" default-destroy-method="breakVal">
<context:annotation-config></context:annotation-config>
<bean id="instrumentalist" class="com.cv.spring.basic.Instrumentalist" />

<bean id="saxophone" class="com.cv.spring.basic.model.Saxophone" />
<bean id="string" class="java.lang.String">
<constructor-arg value="Chic chic chic..." />
</bean>
<bean id="string2" class="java.lang.String">
<constructor-arg value="Chic buk chic buk..." />
</bean>
</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