IOC Annotation_Autowired_Qulifier

想要使用Annotation,需配置:

 

dtd xsd确定XML语法:老的是dtd 新的用xsd
dtd、xsd一般称为XML的schema
用网站作为key 标识了xsd文件,不会冲突

<?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.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd"><context:annotation-config/> </beans>
@Autowired会从容器中,找到一个相同类型的bean注入进来
<context:annotation-config/>
当配置了他时,会初始化4个Bean:用来处理Annotation
The implicitly registered post-processors include AutowiredAnnotationBeanPostProcessorCommonAnnotationBeanPostProcessorPersistenceAnnotationBeanPostProcessor, as well as the aforementioned RequiredAnnotationBeanPostProcessor.
 
@Autowired
a)   默认按类型by type
b)   如果想用byName,使用@Qulifier
c)   写在private field(第三种注入形式)(不建议,破坏封装)
d)   如果写在set上,@qualifier需要写在参数上
 
如果配置了多个类型相同的Bean,则根据名字取:
@Autowired
 public void setUserDAO(@Qualifier("u") UserDAO userDAO) {
  this.userDAO = userDAO;
 }

猜你喜欢

转载自leon-s-kennedy.iteye.com/blog/1536020
ioc