官方文档: [url]http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/beans.html[/url]
[b]
1。scope作用域,默认为单实例[/b]
singleton 单实例
prototype 每次生成新实例
request 每个http request生成新实例
session 每个http session生成新实例
对于request和session得在servlet容器中,且做如下配置。
web.xml使用相关类如DispatcherServlet或RequestContextListener或RequestContextFilter以将request通过ThreadLocal的方式暴露给spring IoC容器。
如
spring配置文件给相关bean指定生成代理,通过代理来控制根据request或session来是否生成新的实例。
也可以自定义scope,参考:[url]http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/beans.html#beans-factory-scopes-custom-using[/url]
[b]2。默认启动时初始化单实例Bean[/b]
可以针对所有或单个bean定义进行更改,bean除了true,false,还有default值用于使用beans的全局定义。
[b]3。Bean实例化[/b]
空构造器直接实例化,工厂静态方法以及实例工厂方法。
还有一种就是本身作为FactoryBean,例如:
[b]4。依赖注入主要有两种方式,构造器以及属性注入。[/b]
对于构造器,可以加index,type或name进行特指注入哪个参数。
以下列举一些相关的配置方式。
[b]Null:[/b]
<null/>
[b]容器类型:[/b]
还有一种衍生的方式,就是自动根据名字或类型注入(autowire),但这种方式对于复杂的配置文件所指不明确,一般不这么配。
[b]5。回调方法[/b]
init-method和destroy-method,空参数方法。
org.springframework.beans.factory.InitializingBean和org.springframework.beans.factory.DisposableBean,与上面类似,建议使用上面的非侵入法方式。
一系列Aware用于获取Spring中的一些上下文信息,如以下列举的一些。
ApplicationContextAware, BeanClassLoaderAware, BeanNameAware, ServletContextAware
以及一系列扩展点。
BeanPostProcessor,如AOP auto-proxying据说就是通过此种方式实现的。
BeanFactoryPostProcessor,如用于替换属性值的PropertyPlaceholderConfigurer就是通过此种方式实现的。
[b]
1。scope作用域,默认为单实例[/b]
<bean name="beanA" class="test.beans.BeanA" scope="singleton"/>
singleton 单实例
prototype 每次生成新实例
request 每个http request生成新实例
session 每个http session生成新实例
对于request和session得在servlet容器中,且做如下配置。
web.xml使用相关类如DispatcherServlet或RequestContextListener或RequestContextFilter以将request通过ThreadLocal的方式暴露给spring IoC容器。
如
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
spring配置文件给相关bean指定生成代理,通过代理来控制根据request或session来是否生成新的实例。
<bean name="beanA" class="test.beans.BeanA" scope="singleton">
<aop:scoped-proxy/>
</bean>
也可以自定义scope,参考:[url]http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/beans.html#beans-factory-scopes-custom-using[/url]
[b]2。默认启动时初始化单实例Bean[/b]
可以针对所有或单个bean定义进行更改,bean除了true,false,还有default值用于使用beans的全局定义。
<beans default-lazy-init="true">
<bean name="beanA" class="test.beans.BeanA" lazy-init="true"/>
[b]3。Bean实例化[/b]
空构造器直接实例化,工厂静态方法以及实例工厂方法。
<bean id="exampleBean" class="examples.ExampleBean"/>
<bean id="clientService" class="examples.ClientService" factory-method="createInstance"/>
<bean id="clientService" factory-bean="serviceLocator" factory-method="createClientServiceInstance"/>
还有一种就是本身作为FactoryBean,例如:
public class BeanC implements FactoryBean {
public Object getObject() throws Exception {
return new BeanB();
}
public Class getObjectType() {
return BeanB.class;
}
public boolean isSingleton() {
return true;
}
}
[b]4。依赖注入主要有两种方式,构造器以及属性注入。[/b]
<bean name="beanA" class="test.beans.BeanA" >
<property name="beanB" ref="beanB"></property>
</bean>
<bean name="beanB" class="test.beans.BeanB" >
<constructor-arg value="42"/>
<constructor-arg ref="beanC"/>
</bean>
对于构造器,可以加index,type或name进行特指注入哪个参数。
以下列举一些相关的配置方式。
[b]Null:[/b]
<null/>
[b]容器类型:[/b]
<bean id="moreComplexObject" class="example.ComplexObject">
<!-- results in a setAdminEmails(java.util.Properties) call -->
<property name="adminEmails">
<props>
<prop key="administrator">administrator@example.org</prop>
<prop key="support">support@example.org</prop>
<prop key="development">development@example.org</prop>
</props>
</property>
<!-- results in a setSomeList(java.util.List) call -->
<property name="someList">
<list>
<value>a list element followed by a reference</value>
<ref bean="myDataSource" />
</list>
</property>
<!-- results in a setSomeMap(java.util.Map) call -->
<property name="someMap">
<map>
<entry key="an entry" value="just some string"/>
<entry key ="a ref" value-ref="myDataSource"/>
</map>
</property>
<!-- results in a setSomeSet(java.util.Set) call -->
<property name="someSet">
<set>
<value>just some string</value>
<ref bean="myDataSource" />
</set>
</property>
</bean>
还有一种衍生的方式,就是自动根据名字或类型注入(autowire),但这种方式对于复杂的配置文件所指不明确,一般不这么配。
<bean name="beanA" class="test.beans.BeanA" autowire="byName"/>
[b]5。回调方法[/b]
init-method和destroy-method,空参数方法。
<bean name="beanA" class="test.beans.BeanA" init-method="init" destroy-method="destroy"/>
org.springframework.beans.factory.InitializingBean和org.springframework.beans.factory.DisposableBean,与上面类似,建议使用上面的非侵入法方式。
一系列Aware用于获取Spring中的一些上下文信息,如以下列举的一些。
ApplicationContextAware, BeanClassLoaderAware, BeanNameAware, ServletContextAware
以及一系列扩展点。
BeanPostProcessor,如AOP auto-proxying据说就是通过此种方式实现的。
public interface BeanPostProcessor {
Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;
Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;
}
BeanFactoryPostProcessor,如用于替换属性值的PropertyPlaceholderConfigurer就是通过此种方式实现的。
public interface BeanFactoryPostProcessor {
void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;
}