Spring Component Annotation2 Project

Click here to download eclipse supported ZIP file



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


 

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

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

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


  public AnotherComponent() {

  }

  public void wannaPlay() {
    LOGGER.info("wannaPlay() ..ham saat saat me rehethe he...");
  }

  public void plays() {

    System.out
        .println("plays()..ham saat saat me rehethe he..AnotherComponent. ");

  }

}


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


 

    
package com.cv.spring.annotation.component;

import javax.inject.Inject;

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

/**
 @author Chandra Vardhan
 */

@Component("componentBeanValue")
public class ComponentBean {
  
  private final static Logger LOGGER = Logger.getLogger(ComponentBean.class);


  public ComponentBean() {
    LOGGER.info("ComponentBean class-0-arg constructor...");
  }

  @Inject
  AnotherComponent anotherComponent; 
  
  @Inject
  private Instrument instrument;
  
  public Instrument getInstrument() {
    return instrument;
  }

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

  public AnotherComponent getAnotherComponent() {
    return anotherComponent;
  }

  public void setAnotherComponent(AnotherComponent anotherComponent) {
    this.anotherComponent = anotherComponent;
  }

  public void play() {

    LOGGER.info("play() in ComoponentBean class...");
        
    anotherComponent.wannaPlay();
    
    anotherComponent.plays();
    
    instrument.plays();
    
  }

}


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


 

    
package com.cv.spring.annotation.component;

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


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

  public ComponentBeanImpl() {
    
    LOGGER.info("componentBeanImpl class 0-arg cons...");
  }
  
  public void wannaPlay() {

    LOGGER.info("wannaPlay() in ComoponentBeanImpl class...");
        
//    anotherComponent.wannaPlay();
    
  }


}


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


 

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

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

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

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

  public ComponentTest() {

  }

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

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

    String configLocation = "componentBean.xml";

    ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
        configLocation);

    String name = "componentBeanValue";

    ComponentBean componentBean = (ComponentBeanapplicationContext.getBean(name);

    LOGGER.info(componentBean);
    
    componentBean.play();

  }

}


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


 

    
package com.cv.spring.annotation.component;

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

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

  public FilterTest() {

  }

  public static void main(String[] args) {

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

    String configLocation = "filter.xml";

    ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
        configLocation);

    String name = "componentBeanValue";

    ComponentBean componentBean = (ComponentBeanapplicationContext
        .getBean(name);

    LOGGER.info(componentBean);

    componentBean.play();

  }

}


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


 

    
package com.cv.spring.annotation.component;

import org.apache.log4j.Logger;


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

  public Gitter() {
    
  }
  
  @Override
  public void plays() {

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

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

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

  public Harmonia() {

  }

  public void plays() {

    LOGGER.info("plays() ...Harmonia...har har...");

  }

}


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


 

    
package com.cv.spring.annotation.component;

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

  void plays();
  
}


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


 

    
package com.cv.spring.annotation.component;

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 SkipIT {
}




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>SpringComponentAnnotation2</artifactId> <name>SpringComponentAnnotation2</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 componentBean.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:annotation-config />
-->
<context:component-scan base-package="com.cv.spring.annotation.component">
<context:include-filter type="annotation" expression="com.cv.spring.annotation.component.Harmonia"/>
</context:component-scan>
</beans>


This is filter.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:annotation-config/>
<context:component-scan base-package="com.cv.spring.annotation.component">

<context:include-filter type="annotation" expression="com.cv.spring.annotation.component.SkipIT"/>

</context:component-scan>


</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