Spring中BeanPostProcessor与InitializingBean接口的关系与应用

来源:互联网 发布:vb里的option 编辑:程序博客网 时间:2024/05/20 12:50

简介

在Spring框架中,bean的定义,从编写到配置再到最终的getbean调用,框架都有相应的实现规则,具体如下所述。

bean的定义

package com.spring.beans;import javax.ejb.Init;import org.springframework.beans.factory.InitializingBean;public class HelloBean implements InitializingBean {public HelloBean() {System.out.println("构造方法");}private String name;private String nullTest;private int age;public String getNullTest() {return nullTest;}public void setNullTest(String nullTest) {this.nullTest = nullTest;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public void prints(String str) {System.out.println(str + ":hahahah");}public void init() {System.out.println("init");}@Overridepublic void afterPropertiesSet() throws Exception {// TODO Auto-generated method stubSystem.out.println("initializing");}}
BeanPostProcessor定义

package com.spring.test;import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.BeanPostProcessor;public class BeanPostProcessor_Imp implements BeanPostProcessor {@Overridepublic Object postProcessAfterInitialization(Object arg0, String arg1)throws BeansException {System.out.println("执行后");return arg0;}@Overridepublic Object postProcessBeforeInitialization(Object arg0, String arg1)throws BeansException {System.out.println("执行前");return arg0;}}
测试类的定义

package com.spring.test;import java.util.List;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.core.io.FileSystemResource;import org.springframework.core.io.Resource;import com.spring.beans.BigBearBean;import com.spring.beans.HelloBean;import com.spring.beans.List_Map_Bean;import com.spring.beans.smallBearBean;import com.spring.interfaces.Animal;public class HelloTest {public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("hellotest.xml");HelloBean HB = (HelloBean) ctx.getBean("hello");HB.prints("王涛");System.out.println(HB.getName() + "\n------------");}}
配置文件

<?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:util="http://www.springframework.org/schema/util"xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"><bean id="hello" class="com.spring.beans.HelloBean" init-method="init"><property name="name"><value><![CDATA[<wb<t>]]></value></property><property name="age" value="23" /><property name="nullTest"><value></value></property></bean><bean class="com.spring.test.BeanPostProcessor_Imp"></bean></beans>

执行结果

15:38:12,269 INFO  [context.support.ClassPathXmlApplicationContext] Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@61a48515: startup date [Fri Jun 23 15:38:12 CST 2017]; root of context hierarchy15:38:12,343 INFO  [factory.xml.XmlBeanDefinitionReader] Loading XML bean definitions from class path resource [hellotest.xml]15:38:12,588 INFO  [factory.support.DefaultListableBeanFactory] Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@25ff3700: defining beans [hello,com.spring.test.BeanPostProcessor_Imp#0]; root of factory hierarchy构造方法执行前initializinginit执行后王涛:hahahah
接下来对上述类和执行结果进行解释

InitializingBean接口:当Spring中的bean实现了这个接口,那么在bean实例化之前会调用这个接口对应的方法(测试的时候发现,构造方法永远是第一个执行的,所以针对这种说法我个人不是很赞同,应该是实例化的过程中)。

BeanPostProcessor接口:这个接口是单独成类的,它的作用范围是Spring容器,一旦在容器中配置了这个类,那么该容器中所有bean在初始化的前后都会调用这个接口对应的方法,具体的配置如: <bean class="com.spring.test.BeanPostProcessor_Imp"></bean>

构造方法,InitializingBean和BeanPostProcessor的执行顺序:构造方法-->BeanPostProcessor-->InitializingBean-->bean中的初始化方法。

阅读全文
0 0
原创粉丝点击