Spring的配置依赖 协调不同步的Bean Bean的后处理器 容器的后处理器

BeanFactory是在需要一个Bean实例时才创建Bean实例

ApplicationContext是在系统启动时自动实现Spring容器的所有Bean

在Spring中可能使用Bean的lazy-load=“true”属性解决ApplicationContext默认行为

 

Bean的元素

value:它的是基本数据类型的

ref:是引用别一个Bean它的属性有二个Bean和local

bean:这里的Bean是指嵌套Bean,它只对外围Bean有效

list Set Map Props:是对Bean实现的集合属性

 

Bean的自动装配autoWire

none:不自动装配

byName:是根据id的名字自动装配

byType:是根据Bean的实例类型装配

constructor:根据构造器来装配

autodetect:BeanFactory根据内部结构来决定使用construtor 或byType其中的那一个

 

Bean元素的依赖检测dependency-check类型

none:不检测

simple:对基本数据类型和集合进行依赖检测

object:仅对合作者Bean进行依赖检测

all:对所有Bean进行依赖检测

 

 定制Bean的生命周期行为

1.注入依赖关系后行为

    使用init-method属性:在实例中定义一个方法通过此属性调用

    使用InitializingBean接口会自动调用afterPropertiesSet()这个方法

 2.即将销毁Bean之前行为

   使用destroy-method属性:在实例中定义一个方法通过此属性调用

    使用DisposableBean接口会自动调用destroy()这个方法

 

 协调不同步的Bean

Singleton的Bean是一个共享的实例,而non-Singleton的Bean是每次都会请求一个新new的实例;当Singleton依赖于一个non-Singleton的实例Bean时,默认的non-Singleton 的Bean不会默认New一个新的实例的;

解决办法:

1.每次需要一个non-Singleton的Bean的实例时,都主动请求一个新的Bean实例,保证每次请求的产生的Bean都是新的;

2.调用方法注入

首先是在实现的类是添加一个抽象方法

 

例子:

二个实现类

public abstract class Chinese implements Person
{
	private Person meiGou;

	public void say()
	{
		meiGou.say();
	}

	public void sayBye()
	{
		meiGou.sayBye();
	}

	public void setMeiGou(MeiGou meiGou)
	{
		this.meiGou = meiGou;
	}
         //方法注入所需要的方法是用spring提供
	public abstract Person createObj();

	public Person getMeiGou()
	{
		return meiGou;
	}

	public void setMeiGou(Person meiGou)
	{
		this.meiGou = meiGou;
	}
	
}
public class MeiGou implements Person
{
 
 
	public void say()
	{
		System.out.println("MeiGou Say");
	}

	public void sayBye()
	{
		System.out.println("MeiGou sayBye");
	}
 
}


调用:

public class TestMain
{

	public static void main(String[] args)
	{
		ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
		BeanFactory factory = (BeanFactory)ctx;
		Chinese chinese =  (Chinese) factory.getBean("chinese");
		
		MeiGou m1 = (MeiGou) chinese.getMeiGou();
		MeiGou m2 = (MeiGou) chinese.getMeiGou();
		System.out.println("没有采用:lookup-method:"+(m1 == m2));
		MeiGou m3 = (MeiGou) chinese.createObj();
		MeiGou m4 = (MeiGou) chinese.createObj();
		System.out.println("采用:lookup-method:"+(m3 == m4));
		//chinese.say();
		
	}
}


Spring配置文件

<?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" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">


	<bean id="chinese" class="com.jask.spring.Chinese">
		<lookup-method name="createObj" bean="meiGou"/>
		<property name="meiGou">
			<ref local="meiGou"/>
		</property>
	</bean>
	<bean id="meiGou" class="com.jask.spring.MeiGou"  scope="prototype"></bean>


</beans>


 

Bean的后处理器

特点:

1.不对个提供服务,所有不需要ID属性

2.它负责对容器中的其它Bean执行处理,为容器中的目标Bean生成代理所以叫Bean后处理器,

它是在Bean创建后对其进行进一步的加强处理

Bean实例实现这个BeanPostProcessor接口

postProcessBeforeInitialization方法:正初始化

postProcessAfterInitialization:初始化完成

 

 容器的后处理器

定义:在容器初始化结后,对容器进行额外的处理

容器后处理器接口:BeanFactoryPostProcessor

容器提供的后处理器常用实现类:

PropertyPlaceholderConfigurer:org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

它用于读取Java Properties文件中的属性,如数据库连接的配置信息

自定义名.自定义属性=value;

 PropertyOverrideConfigurer: org.springframework.beans.factory.config.PropertyOverrideConfigurer;

它用于读取Java Properties文件中的属性,利用属性文件中的相关信息覆盖XML配置文件中定义默认信息;

PropertyOverrideConfigurer文件里的格式:BeanName.property=value;

 

Web应用中自动加载ApplicationContext

 方法一:ContextLoaderLinstener
<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:beans.xml</param-value>
	</context-param>


方法二:ContextLoaderServlet

 

	<Servlet>
		<Servlet-name>ContextLoaderServlet</param-Servlet>
		<Servlet-value>org.springframework.web.context.ContextLoaderServlet</param-Servlet>





                  <load-on-startup>1</load-onstartup><!--确定Servlet启动级别,值越小越优先-->

	</Servlet>


 

你可能感兴趣的:(Spring的配置依赖 协调不同步的Bean Bean的后处理器 容器的后处理器)