当我们使用Spring容器管理对象时,需要对对象进行一些后期处理时,比如数据处理、数据预加载,可以使用BeanPostProcessor接口。
简单演示它的用法。
定义扫描包,显示定义BeanPostProcessor的实现类:
<?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"> <!-- scan the component from the base package -->
<context:component-scan base-package="com.nicchagil.springapplication.No004BeanPostProcessor" /> <bean id="myBeanPostProcessor" class="com.nicchagil.springapplication.No004BeanPostProcessor.MyBeanPostProcessor" /> </beans>
实现BeanPostProcessor:
package com.nicchagil.springapplication.No004BeanPostProcessor; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor; public class MyBeanPostProcessor implements BeanPostProcessor { public Object postProcessBeforeInitialization(Object obj, String arg1)
throws BeansException {
if (obj != null) {
System.out.println("postProcessBeforeInitialization : " + obj.getClass().getName());
} if (obj instanceof UserService) {
System.out.println("some operation in this point.");
} return obj;
} public Object postProcessAfterInitialization(Object obj, String arg1)
throws BeansException {
if (obj != null) {
System.out.println("postProcessAfterInitialization : " + obj.getClass().getName());
} if (obj instanceof UserService) {
System.out.println("some operation in this point.");
} return obj;
} }
被操作的bean:
package com.nicchagil.springapplication.No004BeanPostProcessor; import org.springframework.stereotype.Service; @Service
public class UserService { public void queryUser() {
System.out.println("query user method.");
} }
用于测试的入口类:
package com.nicchagil.springapplication.No004BeanPostProcessor; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class HowToUse { public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("spring.xml");
UserService us = context.getBean("userService", UserService.class);
us.queryUser();
} }
日志:
七月 11, 2016 10:31:13 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@427a39ea: startup date [Mon Jul 11 22:31:13 CST 2016]; root of context hierarchy
七月 11, 2016 10:31:13 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring.xml]
七月 11, 2016 10:31:13 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@6371be16: defining beans [userService,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,myBeanPostProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
postProcessBeforeInitialization : com.nicchagil.springapplication.No004BeanPostProcessor.UserService
some operation in this point.
postProcessAfterInitialization : com.nicchagil.springapplication.No004BeanPostProcessor.UserService
some operation in this point.
query user method.