@Autowired @Resource @Qualifier @Primary 注解理解

package com.example.ylk.springleran;



import com.example.ylk.springleran.controller.AutoTest;
//import com.example.ylk.springleran.service.AutoTest;
import com.example.ylk.springleran.service.InterfaceA;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;

@SpringBootApplication
public class SpringleranApplication {

    //@Autowired    通过类型匹配 通过在@service等注册时加入value名字和@Qualifier来实现同名匹配 也可以配合@Primary 不过@Qualifier优先级更高
    //@Resource     beanName匹配 没有指定name 就按类型匹配 存在同名一定要通过name指定名字

    //没有指定 @Qualifier 也没有@Primary 不知道找哪一个
//    @Autowired
//    InterfaceA c;

// 这里要使用接口
//    @Autowired
//    @Qualifier(value = "A")
//    InterfaceA a;
//
//    @Autowired
//    @Qualifier(value = "B")
//    InterfaceA b;

    // 不加@Qualifier 有@Primary默认选第一个
/*

    @Autowired
    //@Qualifier(value = "A")
    InterfaceA a;

    @Autowired
    //@Qualifier(value = "B")
    InterfaceA b;
*/
    //有@Primary 且指定的A 这是 @Qualifier指定B 还是会选B @Qualifier优先级高

// 这里要使用接口 因为 @Qualifier(value = "B")找的是B 你的类型是A 无法注入

    @Autowired
    @Qualifier(value = "B")
    InterfaceA a;

    @Autowired
    //@Qualifier(value = "B")
    InterfaceA b;

    @Resource(name = "A")
    InterfaceA aa;

    // 若是没有注册过的bean 指定required = false 否则会出现找不到
    @Autowired(required = false)
    C c;



    public static void main(String[] args) {
        //SpringApplication.run(SpringleranApplication.class, args);

        ApplicationContext applicationContext = SpringApplication.run(SpringleranApplication.class, args);;
//        AutoTest demoService = (AutoTest) applicationContext.getBean("sdsd");
//        demoService.setAge(12);
//        demoService.setName("uuy");
//        System.out.println(demoService.getAge());

        AutoTest demoService2 =  applicationContext.getBean(AutoTest.class);
        demoService2.setAge(12);
        demoService2.setName("uuy");
        System.out.println(demoService2.getAge());

        SpringleranApplication SpringleranApplication = applicationContext.getBean(SpringleranApplication.class);
        SpringleranApplication.test();


    }

    public void test(){
        System.out.println(a.sayhi());
        System.out.println(b.sayhi());
    }

}
@Service(value = "A")
//@Primary
class A implements InterfaceA {
    @Override
    public String sayhi() {
        return "hihi";
    }
}
@Service(value = "B")
class B implements InterfaceA{
    @Override
    public String sayhi() {
        return "hello";
    }
}

class C implements InterfaceA{
    @Override
    public String sayhi() {
        return "nono";
    }
}

image.png

image.png

你可能感兴趣的:(@Autowired @Resource @Qualifier @Primary 注解理解)