码迷,mamicode.com
首页 > 编程语言 > 详细

【Spring】对象后期处理,BeanPostProcessor

时间:2016-07-11 23:51:28      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:

当我们使用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>
View Code

 

实现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;
    }

}
View Code

 

被操作的bean:

技术分享
package com.nicchagil.springapplication.No004BeanPostProcessor;

import org.springframework.stereotype.Service;

@Service
public class UserService {
    
    public void queryUser() {
        System.out.println("query user method.");
    }

}
View Code

 

用于测试的入口类:

技术分享
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();
    }

}
View Code

 

日志:

技术分享
七月 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.
View Code

 

【Spring】对象后期处理,BeanPostProcessor

标签:

原文地址:http://www.cnblogs.com/nick-huang/p/5661898.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!