注入注解
@Repository DAO层注解
@Component
@Service是@Component注解的一个特例
@Autowired
@Resource
@Configuration
@Bean
SpringBoot 的几种注入bean 注解
@Autowired
- @Autowired注解作用在构造函数、方法、方法参数、类字段以及注解上
- @Autowired注解可以实现Bean的自动注入
按类型注入
在Spring Boot应用启动时,Spring容器会自动装载一个org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor处理器,当容器扫扫描到@Autowired注解时,就会在IoC容器就会找相应类型的Bean,并且实现注入。
@Qualifier注解
请看 package pan.day.learn.spring.beans.qualifier
当一个接口有两个实现类时
1 |
|
@Resource
@Resource 是JDK1.6支持的注解,默认按照名称进行装配,名称可以通过name属性进行指定,如果没有指定name属性,当注解写在字段上时,默认取字段名,按照名称查找,如果注解写在setter方法上默认取属性名进行装配。当找不到与名称匹配的bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。
1 | 名称可以通过name属性进行指定,name属性一旦指定,就只会按照名称进行装配。 |
@Autowired 与 @Resource
1 |
|
大头
@Component
可被 @ComponentScan(“”) 扫到的注解
细化的@Component:
@Controller 标注在Controller层
@Service 标注在Service层
@Repository 标注在dao层
@Configuration 与 @Bean
见: pan.day.learn.spring.beans.configs
这两个一般一起用了
1 | //创建一个class配置文件 |
@Configuration 本身就是一个 @Component
所以可以使用@Autowried 注入这个@Configuration,但是它的职能主要是配置啊。
参考
https://blog.csdn.net/lipinganq/java/article/details/79167982