博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring and Springboot annotations for DI
阅读量:7209 次
发布时间:2019-06-29

本文共 2837 字,大约阅读时间需要 9 分钟。

hot3.png

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 Example beans injected:

@Componentpublic class ExampleConsumer {    private final Map
 examples;    @Autowired    public ExampleConsumer(Map
 examples) {        this.examples = examples;    }}

In this case the map will contain two entries:

  • "foo" -> FooExample instance

  • "bar" -> BarExample instance

 

@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

@Autowired: inject by type defaultly;

@Resource: inject by name defaultly.

Spring Injection with @Resource, @Autowired and @Inject

Annotations

Annotation Package Source
@Resource javax.annotation Java
@Inject javax.inject Java
@Qualifier javax.inject Java
@Autowired org.springframework.bean.factory Spring

For example:

package com.sourceallies.person;...("personBean")@Component@Qualifier("personBean")public class Person implements Party { }

 

In this test I use a ‘@Qualifier’ annotation to point to the qualified name of the ‘Person’ component.

@Resource@Qualifier("personBean")private Party party;
@Autowired@Qualifier("personBean")private Party party;
@Inject@Qualifier("personBean")private Party party;

All of these annotations inject the ‘Person’ bean.

To add List of Beans

In this test I inject a list of beans.

@Resourceprivate List
 parties;
@Autowiredprivate List
 parties;
@Injectprivate List
 parties;

转载于:https://my.oschina.net/u/2274721/blog/659651

你可能感兴趣的文章
Swoole 2019 :化繁为简、破茧成蝶
查看>>
Android RTL 及小语种 适配
查看>>
走近webpack(1)--多入口及devServer的使用
查看>>
jquery-weui滚动加载问题解决
查看>>
SpringBoot整合Shiro使用Ehcache等缓存无效问题
查看>>
“产学合作勇创新·协同育人书新篇”贵州理工大数据学院数据科学训练营结题答辩报告会圆满举行...
查看>>
EDEN-MACE 1.4.0 更新,增加数据清理功能
查看>>
ASP.Net中实现上传过程中将文本文件转换成PDF的方法
查看>>
营收放缓、股价暴跌、高管离职,Facebook迎来至暗时刻?
查看>>
MySQL探秘(二):SQL语句执行过程详解
查看>>
使用Akka持久化——消息发送与接收
查看>>
Spring框架之Filter应用
查看>>
在IDEA中设置自己的名字和时间
查看>>
@NotBlank注解地正确使用
查看>>
Android--音乐播放器
查看>>
互联网巨头布阵LoRaWAN,是又一春天还是不容乐观?
查看>>
CSS 全解析实战(一)-导读
查看>>
【深度学习再突破】让计算机一眼认出“猫”:哈佛提出新高维数据分析法
查看>>
C++程序设计基础(7)位运算
查看>>
MSDN-9月杂志推荐
查看>>