Talk about @ Autowired annotation injection write interface name or implement class name

  • 2021-12-12 08:45:44
  • OfStack

Directory with @ Autowired annotation injection, write interface name or implementation class name from a programmer 1 Q&A question Spring Autowired injection interface several questions 1. How does Spring know which implementation to inject? 2. Do you need @ Qualifier and @ Resource annotations? 3. Why is @ Autowired used on interface instead of implementation classes?

Inject with @ Autowired annotation, write interface name or implementation class name

1 Q&A question from a programmer

1. I don't understand why the annotation @ repository annotates the implementation class UserDAOImpl of interface UserDAO, while the annotation @ Autowired is used in UserServiceImpl to inject the attribute private UserDAO userDAO for automatic assembly, and why the final result is an instance of UserDAOImpl.

The above is a question from a classmate, and I also have such a question-

2. When @ Service annotates the service layer, in unitest, it is obtained from ApplicationContext. getBean ("Implementation class name, lowercase initials").

That is, the Bean initialized in the container should follow the implementation class name rule. This is not a problem if configured with xml, because xml can specify id, id is the interface, and class points to the implementation class.

3. Answer from netizens: This is actually an object that creates an implementation class but references the interface type, that is, "InjectionDao injectionDao = new InjectionDaoImpl ()", which is actually an application of Java polymorphism (upward transformation). Add @ Repository annotation at the implementation class, which means new InjectionDaoImpl (), while defining the attribute InjectionDAO injectionDAO in InjectionServiceImpl is to transform the InjectionDaoImpl object from new up to InjectionDao type.

Several Problems of Autowired Injection Interface in Spring

1. How does Spring know which implementation to inject?

As long as there is only a single implementation of the interface and that implementation is annotated with @Component with Spring's component scan enabled, Spring framework can find out the (interface, implementation) pair.

If component scan is not enabled, then you have to define the bean explicitly in your application-config.xml (or equivalent spring configuration file).

If Spring is configured with component and scan, and only one implementation of the interface to be injected, the spring framework can automatically assemble interface with the implementation. If component scan is not configured, you must define this bean in application-config. xml (or equivalent configuration file).

It can be understood that component-scan is declared in the application-service. xml configuration file


<context:component-scan base-package="com.system.service.impl"/>

There is an interface class under com. system. service, and an implementation class corresponding to the interface under com. system. service. impl, which is annotated with @ Service annotation.

When using this interface class, you can do the following:


@Autowired
private UserloginService userloginService;

In this way, automatic assembly is carried out.

2. Do you need @ Qualifier and @ Resource annotations?

Once you have more than one implementation, then you need to qualify each of them and during auto-wiring, you would need to use the @Qualifier annotation to inject the right implementation, along with @Autowired annotation.

If you are using @Resource (J2EE semantics), then you should specify the bean name using the name attribute of this annotation.

1 If there are multiple implementations of an interface, each specialization identification is required and marked with @ Qualifier and @ Autowired1 during auto-load. If you are using @ Resource annotation, you should use the attribute name in resource to annotate @ Autowired.

3. Why is @ Autowired used on interface instead of implementation classes?

Firstly, it is always a good practice to code to interfaces in general.

Secondly, in case of spring, you can inject any implementation at runtime.

A typical use case is to inject mock implementation during testing stage.

First of all, using interfaces in general is a common and beneficial transformation technology.

Secondly, in spring, you can inject various implementations during operation. A classic case is to inject simulated implementation classes in the testing stage.


Related articles: