想要使用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 AutowiredAnnotationBeanPostProcessor, CommonAnnotationBeanPostProcessor, PersistenceAnnotationBeanPostProcessor, 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;
}
分享到:
相关推荐
SpringIoCanotation Spring IoC @Annotation-有和没有@Autowired 实验!
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; /** * @author :xiaolinzi * @date :2020-4-14 20:46 * @email : xiaolinzi95_27@163....
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Scope; import org.spring...
xml java系统源码 ...5.annotation(autowired,resource,@Qualifier) * 6.bean的生命周期 init-method,destroy-method,initiableBean, DisposableBean,lasyinit,singleton,prototype @PostConstruct @P
请举例解释Required annotation 请举例解释Autowired注解 请举例说明Qualifier注解 构造方法注入和设值注入有什么区别 Spring框架中有哪些不同类型的事件 FileSystemResource和ClassPathResource有何区别 Spring ...
第五课:IOC(DI)配置及应用 9 一、 什么是IOC、DI 9 二、 编辑xml文件时,没有提示 9 三、 注入类型(Injecting dependencies) 9 (一) setter注入类型Setter Injection 9 (二) 构造方法Constructor Injection 10 四、...
第五课:IOC(DI)配置及应用 9 一、 什么是IOC、DI 9 二、 编辑xml文件时,没有提示 9 三、 注入类型(Injecting dependencies) 9 (一) setter注入类型Setter Injection 9 (二) 构造方法Constructor Injection 10 四、...
stn-annotation 基于spring-aop实现的bean注册,注入与管理。。。很蠢, 简单实现IOC功能 主要涉及反射与自定义注解的使用 @Bean 用于对象的自动创建 @Autowired 用于自动注入 Aware接口 用于在创建对象时候自动调用...
3.11.1. @Autowired 3.11.2. 基于注解的自动连接微调 3.11.3. CustomAutowireConfigurer 3.11.4. @Resource 3.11.5. @PostConstruct 与 @PreDestroy 3.12. 对受管组件的Classpath扫描 3.12.1. @Component和更...
3.11.1. @Autowired 3.11.2. 基于注解的自动连接微调 3.11.3. CustomAutowireConfigurer 3.11.4. @Resource 3.11.5. @PostConstruct 与 @PreDestroy 3.12. 对受管组件的Classpath扫描 3.12.1. @Component和更...
MyBatis 目录(?)[-] mybatis实战教程mybatis in action之一开发环境搭建 mybatis实战教程mybatis in action之二以接口的方式编程 mybatis实战教程mybatis in action之三实现数据的增删改查 mybatis实战教程mybatis ...
4. The IoC container ........................................................................................................ 22 4.1. Introduction to the Spring IoC container and beans ..................
4. The IoC container ........................................................................................................ 22 4.1. Introduction to the Spring IoC container and beans ..................