pom.xml文件加入spring-context依赖,间接加入了spring-aop依赖,有aop依赖才能使用注解
@Component | |
@Scope | 定义bean作用范围 |
@PostConstruct @PreDestory | 定义bean生命周期 |
@Autowired
为Spring 框架提供的注解,需要导入包org.springframework.beans.factory.annotation.Autowired
1、定义bean
@Component @Controller @Service @Repository <context:component-scan/> | 纯注解开发: @configuration @ComponentScan AnnotationConfigApplicationContext |
2、依赖注入
@Autowired 自动装配模式(按类型),不用set方法,基于反射(暴力反射)
@Qualifier 指定名称,必须依赖@Autowired (有相同bean时使用)
@Value 简单类型注入
以配置的形式加载@PropertySource("***.properties"),多文件使用数组格式,不允许使用通配符*
3、第三方bean
、定义一个方法获得要管理的对象
、添加@Bean表示当前方法的返回值的一个bean
、(1)导入式 @Import(***.class),此注解只能添加一次,多个数据使用数组格式
、(2)扫描式@ComponentScan("配置类所在的包")-----@Configuration