Spring 2.5 学习笔记
?Bean的生命周期:
? 当Bean的scope是singleton时.Bean的创建时间是Spring容器启动的时候创建的.当scope是prototype的时候Bean的创建时间是获取对象的时候创建的.
scope为singleton的时候可以配置lazy-init="true" 延迟初始化
如果想全局配置lazy-init="true" 的话 可以在Beans节点配置default-lazy-init="true"
ps:lazy-init属性尽量不要用,除非特别需要.
?init-method="?" 指定Bean中一个方法,当实例化完成后执行的方法.(初始化方法)
destory-method="?"? 指定Bean中一个方法,当Bean销毁的时候调用.
AbstractApplicationContext 这个抽象类中有close方法可以去销毁spring容器..
?
Set注入
<property name="someSet"> <set> <value>just some string</value> <ref bean="myDataSource" /> </set></property>
?
List注入
<property name="someList"> <list> <value>a list element followed by a reference</value> <ref bean="myDataSource" /> </list></property>?
Map注入
<property name="someMap"> <map> <entry> <key> <value>an entry</value> </key> <value>just some string</value> </entry> <entry> <key> <value>a ref</value> </key> <ref bean="myDataSource" /> </entry> </map></property>?
Properties注入
<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>?
?
Annotations:
?? 命名空间:
<?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" 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"> <context:annotation-config/></beans>
?
在JAVA代码中使用@Autowired或者@Resource注解方式进行注入
@Autowired默认使用类型进行装配, @Resource默认按照名称去进行装配.当找不到名称的时候再去用类型进行装配.
推荐使用@Resource进行装配. @Resource是JDK自带的注解.@Autowired是Spring提供的注解.
@Resource(name="xxx")? 可以写在定义属性的代码上.也可以写在属性的set方法上.效果相同? 如果不指定(name=?)属性 根据编码的属性定义名称去查找.
需要被注入的对象必须在XML中注入
?