Spring BeanAware Impl Project

Click here to download eclipse supported ZIP file



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


 

    
package com.cv.spring.bean.aware;

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

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

  @Override
  public void setApplicationContext(ApplicationContext arg0)
      throws BeansException {
    LOGGER.info("setApplicationContext called...." + arg0);
  }

  public void main() {
    LOGGER.info("main() called....");
  }

}


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


 

    
package com.cv.spring.bean.aware;

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
/**
 @author Chandra Vardhan
 */
public class BeanFactoryAwareImpl implements BeanFactoryAware {
  private final static Logger LOGGER = Logger.getLogger(BeanFactoryAwareImpl.class);
  @Override
  public void setBeanFactory(BeanFactory arg0throws BeansException {
    LOGGER.info("setBeanFactory called...."+arg0);
  }
  public void main() {
    LOGGER.info("main() called....");
  }

}


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


 

    
package com.cv.spring.bean.aware;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.BeanNameAware;
/**
 @author Chandra Vardhan
 */
public class BeanNameAwareImpl implements BeanNameAware {  
  private final static Logger LOGGER = Logger.getLogger(BeanNameAwareImpl.class);
  @Override
  public void setBeanName(String arg0) {
    LOGGER.info("setBeanName called..."+arg0);
  }
  public void main() {
    LOGGER.info("main() called....");
  }

}


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


 

    
package com.cv.spring.bean.aware;

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
/**
 @author Chandra Vardhan
 */
public class BeanPostProcessorImpl implements BeanPostProcessor {  
  private final static Logger LOGGER = Logger.getLogger(BeanPostProcessorImpl.class);
  @Override
  public Object postProcessAfterInitialization(Object arg0, String arg1)
      throws BeansException {
    LOGGER.info("postProcessAfterInitialization called...."+arg0);
    LOGGER.info("postProcessAfterInitialization called...."+arg1);
    return arg0;
  }

  @Override
  public Object postProcessBeforeInitialization(Object arg0, String arg1)
      throws BeansException {
    LOGGER.info("postProcessBeforeInitialization called...."+arg0);
    LOGGER.info("postProcessBeforeInitialization called...."+arg1);
    return arg0;
  }
  
  public void main() {
    LOGGER.info("main() called....");
  }


}


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


 

    
package com.cv.spring.bean.aware;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 @author Chandra Vardhan
 */
public class KnightTest {
  private final static Logger LOGGER = Logger.getLogger(KnightTest.class);
  /**
   @param args
   */
  public static void main(String[] args) {

    String configLocation = "applicationContext.xml";
    ApplicationContext factory = new ClassPathXmlApplicationContext(configLocation);
    LOGGER.info(factory.containsBean("quest"));
    
    BeanPostProcessorImpl b =(BeanPostProcessorImpl)factory.getBean("quest4");
    b.main();
    
    ApplicationContextAwareImpl a =(ApplicationContextAwareImpl)factory.getBean("quest3");
    a.main();
    
    
    BeanFactoryAwareImpl beanFactoryAwareImpl =(BeanFactoryAwareImpl)factory.getBean("quest2");
    beanFactoryAwareImpl.main();
    
    BeanNameAwareImpl beanNameAwareImpl =(BeanNameAwareImpl)factory.getBean("quest");
    beanNameAwareImpl.main();    
    
    
  }
}




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>SpringBeanAwareImpl</artifactId> <name>SpringBeanAwareImpl</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 applicationContext.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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="quest" class="com.cv.spring.bean.aware.BeanNameAwareImpl" />
<bean id="quest2" class="com.cv.spring.bean.aware.BeanFactoryAwareImpl" />
<bean id="quest3" class="com.cv.spring.bean.aware.ApplicationContextAwareImpl" />
<bean id="quest4" class="com.cv.spring.bean.aware.BeanPostProcessorImpl" />
</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