Spring and Springboot annotations for DI
2016-04-14 18:29
459 查看
摘要: Basic annotations for DI
Basic annotaions to describe a bean:
The above annotations can have a optional element: String value. The value may indicate a suggestion for a logical component name, to be turned into a Spring bean in case of an autodetected component.
@Autowired
Marks a constructor, field, setter method or config method as to be autowired by Spring's dependency injection facilities.
If autowire for an interface, it will search for the implementation and inject it.
If there'are multiple implementation, you can use a Map to store the injected implementations.
If you have an interface named
And two implementations:
You can have a map of
In this case the map will contain two entries:
@Qualifier
Use with @AutowiredThis annotation may be used on a field or parameter as a qualifier for candidate beans when autowiring. It may also be used to annotate other custom annotations that can then in turn be used as qualifiers.
spring @Autowired @Qualifier @Resource
http://ljhzzyx.blog.163.com/blog/static/38380312201371385956237/
@Autowired: inject by type defaultly;
@Resource: inject by name defaultly.
Spring Injection with @Resource, @Autowired and @Inject
http://blogs.sourceallies.com/2011/08/spring-injection-with-resource-and-autowired/
For example:
In this test I use a ‘@Qualifier’ annotation to point to the qualified name of the ‘Person’ component.
All of these annotations inject the ‘Person’ bean.
Basic annotaions to describe a bean:
| Annotation | Meaning | +------------+-----------------------------------------------------+ | @Component | generic stereotype for any Spring-managed component | | @Repository| stereotype for persistence layer | | @Service | stereotype for service layer | | @Controller| stereotype for presentation layer (spring-mvc) |
The above annotations can have a optional element: String value. The value may indicate a suggestion for a logical component name, to be turned into a Spring bean in case of an autodetected component.
@Autowired
Marks a constructor, field, setter method or config method as to be autowired by Spring's dependency injection facilities.
If autowire for an interface, it will search for the implementation and inject it.
If there'are multiple implementation, you can use a Map to store the injected implementations.
If you have an interface named
Example
public interface Example { }
And two implementations:
@Component("foo") public class FooExample implements Example { } @Component("bar") public class BarExample implements Example { }
You can have a map of
Examplebeans injected:
@Component public class ExampleConsumer { private final Map<String, Example> examples; @Autowired public ExampleConsumer(Map<String, Example> examples) { this.examples = examples; } }
In this case the map will contain two entries:
"foo"->
FooExampleinstance
"bar"->
BarExampleinstance
@Qualifier
Use with @AutowiredThis annotation may be used on a field or parameter as a qualifier for candidate beans when autowiring. It may also be used to annotate other custom annotations that can then in turn be used as qualifiers.
spring @Autowired @Qualifier @Resource
http://ljhzzyx.blog.163.com/blog/static/38380312201371385956237/
@Autowired: inject by type defaultly;
@Resource: inject by name defaultly.
Spring Injection with @Resource, @Autowired and @Inject
http://blogs.sourceallies.com/2011/08/spring-injection-with-resource-and-autowired/
Annotations
Annotation | Package | Source |
---|---|---|
@Resource | javax.annotation | Java |
@Inject | javax.inject | Java |
@Qualifier | javax.inject | Java |
@Autowired | org.springframework.bean.factory | Spring |
package com.sourceallies.person; ... ("personBean") @Component @Qualifier("personBean") public class Person implements Party { } |
@Resource @Qualifier("personBean") private Party party; |
@Autowired @Qualifier("personBean") private Party party; |
@Inject @Qualifier("personBean") private Party party; |
To add List of Beans
In this test I inject a list of beans.@Resource private List<Party> parties; |
@Autowired private List<Party> parties; |
@Inject private List<Party> parties; |
相关文章推荐
- java-注解annotation
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- Spring和ThreadLocal
- Spring Boot 开发微服务
- Spring整合Quartz(JobDetailBean方式)
- Spring整合Quartz(JobDetailBean方式)
- 模拟Spring的简单实现
- Spring整合WebSocket应用示例(上)
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- springmvc 发送ajax出现中文乱码的解决方法汇总
- SpringMVC框架下JQuery传递并解析Json格式的数据是如何实现的
- struts2 spring整合fieldError问题
- spring的jdbctemplate的crud的基类dao
- 读取spring配置文件的方法(spring读取资源文件)
- Spring Bean基本管理实例详解
- Java Annotation Overview详解
- java实现简单美女拼图游戏