Autowired This is a spring annotation

org.springframework.beans.factory.annotation.Autowired

@resource This is a Java native annotation

javax.annotation.Resource

@autoWired is injected by type by default, but can be injected by name in conjunction with @qualifier

@Resource is injected according to the name property inside

For interface-oriented programming, an interface and an implementation class are indistinguishable.

This error will be reported if you do not specify two implementation classes

Org. Springframework. Beans. Factory. BeanCreationException bean creation failed

expected single matching bean but found 2: personServiceImpl1,personServiceImpl2

You can write one instance of a Service interface and two serviceImpl implementation classes

public interface PersonService {public List<Person> listPerson();
}
Copy the code
@Service
public class PersonServiceImpl1 implements PersonService{
    @Autowired
    private PersonMapper mapper;

    public List<Person> listPerson() {
        System.out.println("PersonServiceImpl1.java");
        returnmapper.listPerson(); }}Copy the code
@Service
public class PersonServiceImpl2 implements PersonService{
    @Autowired
    private PersonMapper mapper;

    public List<Person> listPerson() {
        System.out.println("PersonServiceImpl2.java");
        returnmapper.listPerson(); }}Copy the code

Controller layer to make injection calls

@Controller
public class PersonController {
    @Autowired
    @Qualifier("personServiceImpl1")
    private PersonService service;
    
    @Resource(name="personServiceImpl2")
    private PersonService service2;
    
    @RequestMapping("listPerson")
    @ResponseBody
    public void listPerson(){
        List<Person> list = service.listPerson();
        System.out.println(list);
    }
    
    @RequestMapping("listPerson2")
    @ResponseBody
    public void listPerson2(){ List<Person> list = service2.listPerson(); System.out.println(list); }}Copy the code