Spring and Springboot annotations for DI

大憨熊 提交于 2020-02-28 12:38:03

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:

@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" -> 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

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

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.

@Resource
private List<Party> parties;
@Autowired
private List<Party> parties;
@Inject
private List<Party> parties;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!