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

简介

在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");
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		// TODO Auto-generated method stub
		System.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 {

	@Override
	public Object postProcessAfterInitialization(Object arg0, String arg1)
			throws BeansException {
		System.out.println("执行后");
		return arg0;
	}

	@Override
	public 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.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
		http://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 hierarchy
15: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
构造方法
执行前
initializing
init
执行后
王涛:hahahah

接下来对上述类和执行结果进行解释

 

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

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

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

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值