- IOC: Inversion of Control 控制反转
①自己控制→容器控制
②控制具体实现→控制抽象(接口)
- DI:Dependency Injection 依赖注入
依赖于容器注入的对象
- 注入方法
a) setter(重要)
<property name="userDAO" ref="u"/>
或
<property name="userDAO">
<ref bean="u"/>
</property>
b) 构造方法
<constructor-arg>
<ref bean="u"/>
</constructor-arg>
c) 接口注入
- bean标签 name/id 区别:name可以加特殊字符
- 生命周期
a) lazy-init 这个bean在容器初始化的时候不进行初始化,用到的时候初始化,应用启动很慢的时候用
b)init-method destroy-methd 不要和prototype一起用(了解)
- Annotation: 注解
a) 默认按类型by type
b) 如果想用byName,使用@Qulifier
c) 写在private field(第三种注入形式)(不建议,破坏封装)
d) 如果写在set上,@qualifier需要写在参数上
e)启用注解<context:annotation-config/>
<?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>
- @Resource(重要)
@Resource(name="xxx")
a) 加入:j2ee/common-annotations.jar
b) 默认按名称,名称找不到,按类型
c) 可以指定特定名称
d) 推荐使用
e) 不足:如果没有源码,就无法运用annotation,只能使用xml
- @Component @Service @Controller @Repository
a) 初始化的名字默认为类名首字母小写
b) 可以指定初始化bean的名字
- @PostConstruct = init-method; @PreDestroy = destroy-method;
浙公网安备 33010602011771号