Spring Assignable Component Annotation Project

Click here to download eclipse supported ZIP file



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


 

    
package com.cv.spring.component.assignable;

/**
 @author Chandra Vardhan
 */
public interface ComponentMarker {
  
  void component();
  
}


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


 

    
package com.cv.spring.component.assignable;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
/**
 @author Chandra Vardhan
 */
@Component
public class Dependency {
  
  private final static Logger LOGGER = Logger.getLogger(Dependency.class);

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("Dependency : ");
        sb.append(ObjectUtils.identityToString(this));
        sb.append("{}");
        LOGGER.info("toString for Dependency class..."+sb.toString());
        return sb.toString();
    }
}


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


 

    
package com.cv.spring.component.assignable;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 @author Chandra Vardhan
 */
public class MainProgram {
  private final static Logger LOGGER = Logger.getLogger(MainProgram.class);

  public static void main(String[] argsthrows Exception {
    ApplicationContext ac = new ClassPathXmlApplicationContext(
        "spring-config.xml");
    String[] beanNames = ac.getBeanDefinitionNames();
    for (String beanName : beanNames) {
      Object o = ac.getBean(beanName);
      LOGGER.info("Bean object is : " + o);
      if (o != null && o instanceof ComponentMarker) {
        MarkedBean m = (MarkedBeano;
        m.component();
      }
    }
  }
}


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


 

    
package com.cv.spring.component.assignable;

import org.apache.log4j.Logger;

/**
 @author Chandra Vardhan
 */

public class MarkedBean implements ComponentMarker {

  
  private final static Logger LOGGER = Logger.getLogger(MarkedBean.class);
  
    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("MarkedBean");
        sb.append("{}");
        
        LOGGER.info("toString for MarkedBean class..."+sb.toString());
        return sb.toString();
    }

  @Override
  public void component() {
    LOGGER.info("component() called from the MarkedBean class...");
  }
}




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>SpringAssignableComponentAnnotation</artifactId> <name>SpringAssignableComponentAnnotation</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:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.cv.spring.component.assignable">
<context:include-filter type="assignable" expression="com.cv.spring.component.assignable.ComponentMarker"/>
</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