三种实例化bean的方式:
1.使用类构造器实例化
2.使用静态工厂方法实例化
public static PersonServiceBean createPersonServiceBean() {
return new PersonServiceBean();
}
<bean id="personService2" class="com.ethan.service.factory.PersonServiceBeanFactory" factory-method="createPersonServiceBean"/>
3.使用实例工厂方法实例化
public PersonServiceBean createPersonServiceBean2() {
return new PersonServiceBean();
}
<bean id="personServiceFactory" class="com.ethan.service.factory.PersonServiceBeanFactory"></bean>
<bean id="personService3" factory-bean="personServiceFactory" factory-method="createPersonServiceBean2"></bean>
bean的作用域:
<bean id="personService" class="com.ethan.service.impl.PersonServiceBean" scope="prototype"/>
默认单例singleton
prototype
bean的生命周期:
什么时候初始化bean:默认构造函数打印一句话
sigleton:
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"beans.xml"});
输出init..........在容器初始化时,初始化bean
prototype:
getBean()时初始化bean
<bean id="personService" class="com.ethan.service.impl.PersonServiceBean" scope="prototype" lazy-init="false"/>
设置lazy-init为false后,启动时也不会初始化bean
单例时:设置lazy-int="true",容器启动时,不会初始化bean
全局:<beans default-lazy-int="true>
指定初始和销毁方法,进行一些操作:
<bean id="personService" class="com.ethan.service.impl.PersonServiceBean" init-method="init" destroy-method="close"/>
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"beans.xml"});
ctx.close();//关闭容器
关闭容器:
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@7c64dc11:
2012-2-29 15:04:24 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
信息: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@311671b2:
<property name="personDao">
<bean class="com.ethan.dao.impl.PersonDaoBean"/>
</property>
为基本属性注入
<property name="name" value="ethan"/>
集合属性装配:
private Set<String> sets = new HashSet<String>();
<property name="sets">
<set>
<value>1</value>
<value>2</value>
<value>3</value>
</set>
</property>
<property name="lists">
<list>
<value>1</value>
<value>2</value>
<value>3</value>
</list>
</property>
private Properties properties = new Properties();
<property name="properties">
<props>
<prop key="key1">1</prop>
<prop key="key2">2</prop>
<prop key="key3">3</prop>
</props>
</property>
for(Object key:personService.getProperties().keySet()
personService.getProperties().getProperty((String)key);
---------------------------
<property name="maps">
<map>
<entry key="key1" value="value1"/>
<entry key="key2" value="value2"/>
<entry key="key3" value="value3"/>
</map>
</property>
for(Object key:personService.getMaps().keySet())
personService.getMaps().get((String)key);
构造器注入:
<constructor-arg index="0" type="com.ethan.dao.PersonDao" ref="personDao"/>
<constructor-arg index="1" value="77"/>
@Autowire default >>>type
设置 required=false 则可以为null
@Resource default >>>name 2>>>type
在字段上或方法上,默认取属性名
@Resource(name="xxx")如果属性名和配置名不一样
@Resource(name="xxx") private PersonDao personDao;
1.使用类构造器实例化
2.使用静态工厂方法实例化
public static PersonServiceBean createPersonServiceBean() {
return new PersonServiceBean();
}
<bean id="personService2" class="com.ethan.service.factory.PersonServiceBeanFactory" factory-method="createPersonServiceBean"/>
3.使用实例工厂方法实例化
public PersonServiceBean createPersonServiceBean2() {
return new PersonServiceBean();
}
<bean id="personServiceFactory" class="com.ethan.service.factory.PersonServiceBeanFactory"></bean>
<bean id="personService3" factory-bean="personServiceFactory" factory-method="createPersonServiceBean2"></bean>
bean的作用域:
<bean id="personService" class="com.ethan.service.impl.PersonServiceBean" scope="prototype"/>
默认单例singleton
prototype
bean的生命周期:
什么时候初始化bean:默认构造函数打印一句话
sigleton:
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"beans.xml"});
输出init..........在容器初始化时,初始化bean
prototype:
getBean()时初始化bean
<bean id="personService" class="com.ethan.service.impl.PersonServiceBean" scope="prototype" lazy-init="false"/>
设置lazy-init为false后,启动时也不会初始化bean
单例时:设置lazy-int="true",容器启动时,不会初始化bean
全局:<beans default-lazy-int="true>
指定初始和销毁方法,进行一些操作:
<bean id="personService" class="com.ethan.service.impl.PersonServiceBean" init-method="init" destroy-method="close"/>
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"beans.xml"});
ctx.close();//关闭容器
关闭容器:
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@7c64dc11:
2012-2-29 15:04:24 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
信息: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@311671b2:
<property name="personDao">
<bean class="com.ethan.dao.impl.PersonDaoBean"/>
</property>
为基本属性注入
<property name="name" value="ethan"/>
集合属性装配:
private Set<String> sets = new HashSet<String>();
<property name="sets">
<set>
<value>1</value>
<value>2</value>
<value>3</value>
</set>
</property>
<property name="lists">
<list>
<value>1</value>
<value>2</value>
<value>3</value>
</list>
</property>
private Properties properties = new Properties();
<property name="properties">
<props>
<prop key="key1">1</prop>
<prop key="key2">2</prop>
<prop key="key3">3</prop>
</props>
</property>
for(Object key:personService.getProperties().keySet()
personService.getProperties().getProperty((String)key);
---------------------------
<property name="maps">
<map>
<entry key="key1" value="value1"/>
<entry key="key2" value="value2"/>
<entry key="key3" value="value3"/>
</map>
</property>
for(Object key:personService.getMaps().keySet())
personService.getMaps().get((String)key);
构造器注入:
<constructor-arg index="0" type="com.ethan.dao.PersonDao" ref="personDao"/>
<constructor-arg index="1" value="77"/>
@Autowire default >>>type
设置 required=false 则可以为null
@Resource default >>>name 2>>>type
在字段上或方法上,默认取属性名
@Resource(name="xxx")如果属性名和配置名不一样
@Resource(name="xxx") private PersonDao personDao;
本文深入探讨了Spring框架中Bean的三种实例化方式、作用域设置、生命周期管理及属性注入机制,包括类构造器、静态工厂方法和实例工厂方法的使用,详细解释了Bean的默认单例与原型作用域,以及如何通过初始化和销毁方法进行操作。同时,阐述了如何实现属性的注入,包括基本属性、集合属性、配置文件注入和构造器注入,并介绍了@Autowire和@Resource注解的用法。

2万+

被折叠的 条评论
为什么被折叠?



