读书人

springIOC学习札记

发布时间: 2012-10-13 11:38:17 作者: rapoo

springIOC学习笔记

spring支持多个配置文件的管理:

<import resource="daos.xml"/>
?<import resource="bizs.xml"/>将所有的文件都引入进来

测试的时候可以使用

ApplicationContext context = new ClassPathXmlApplicationContext(
??????? new String[] {"services.xml", "daos.xml"});引入多个文件

如果你的程序只需要bean的管理,那么导入commons-logging-1.1.1.jar,spring-context.jar,spring-core.jar,spring-beans.jar

依赖注入使得程序之间的耦合性降低了,spring支持构造器注入,setter方式的注入,注解式的注入。

可以使用带参的构造方法进行依赖注入,

<bean name="foo" value="7500000"/> <constructor-arg type="java.lang.String" value="42"/></bean>

进行初始化的注入。当然通过index属性指定比type更加明确。

bean的引用多样化:

<idref local="theTargetBean"/>同一配置文件

<idref bean="theTargetBean" />多个配置文件
<property name="targetName" value="theTargetBean" />不检查
<property name="targetName" ref="theTargetBean" />

区别在于检查和不检查。

内部bean中的??? <property name="adminEmails">
??????? ??????? class="com.example.Person"
??????? p:name="John Doe"
??????? p:spouse-ref="jane"/>
我不喜欢,没提示,呵呵

如果想对bean进行延迟加载,使用lazy-init="true"

使用自动装配可与减少配置量,但一定要避免不明确的装配行为.

声明式作用域的配置:

<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
??????? <property name="scopes">
??????????? <map>
??????????????? <entry key="thread">
??????????????????? <bean class="com.foo.ThreadScope"/>
??????????????? </entry>

??????????? </map>
??????? </property>
??? </bean>
可以给bean指定初始化方法,销毁方法等,在spring2.5配置文件中开启<context:annotation-config/>

可以给类进行元注解的配置。

对于非web程序使用ctx.registerShutdownHook();关闭ioc容器