Usage of @Autowired

@Autowired is wired by type.

If an interface UserInterface has multiple implementation classes
AImpl(@service(name="userInterface1"))

BImpl(@service(name="userInterface2"))

These implementation classes are added to the Spring container.

There are several ways to inject instances:
1. Distinguish different instances by adding the annotation @Qualifier
@Autowire
@Qualifier("userInterface1")
iUserInterface iUserInterface;

2. Distinguish different instances by @Resource(name="userInterface1")
@Resource(name="userInterface1")
iUserInterface iUserInterface;

3. Keep userInterface consistent with the AImpl class object name (this method is not recommended)
@Autowired
private IUserInterface userInterface1;
@Autowired
private IUserInterface userInterface2;

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326614534&siteId=291194637