@Autowired和@Resource的区别

6 篇文章 0 订阅
3 篇文章 0 订阅

3.22 @Autowired 和 @Resource

步骤模拟演示二者区别:

@Resource默认通过名称注入,找不到再通过类型注入;如果两种方法都没完成,可以通过指定名称注入。

@Autowired默认通过类型注入,可以通过指定名称注入

1、首先声明MyService接口和两个实现类MyServiceImpl01和MyServiceImpl02

public interface MyService {
    void hello();
}

// @Service没有指定Bean的名称,默认名称按照首字母小写的规则声明,即:myServiceImpl01作为Bean的名称
@Service
public class MyServiceImpl01 implements MyService{

    @Override
    public void hello() {
        System.out.println("MyServiceImpl01 print hello!");
    }
}

@Service
public class MyServiceImpl02 implements MyService{

    @Override
    public void hello() {
        System.out.println("MyServiceImpl02 print hello!");
    }
}

2、测试方法:

@SpringBootApplication
public class AprilApplication implements CommandLineRunner {

    // 默认通过名称(myService)注入,没有找到再通过类型注入
    // 1.通过名称myService没有找到可以注入的Bean
    // 2.通过类型注入,此时有两个Bean实现了MyService,spring不知道应该注入哪个实现类
    // 因此抛出异常:
    // No qualifying bean of type 'com.april.service.MyService' available: expected single matching bean but found 2: myServiceImpl01,myServiceImpl02
    @Resource
    private MyService myService;

    @Override
    public void run(String... args) {
        myService.hello();
    }
    
    public static void main(String[] args) {
        SpringApplication.run(AprilApplication.class, args);
    }

}

3、@Resource解决方案:

@SpringBootApplication
public class AprilApplication implements CommandLineRunner {

    // 通过指定名称注入
    @Resource
    private MyService myServiceImpl01;

    @Override
    public void run(String... args) {
        myServiceImpl01.hello();
    }
    
    public static void main(String[] args) {
        SpringApplication.run(AprilApplication.class, args);
    }

}

控制台输出:
MyServiceImpl01 print hello!

4、@Autowired

@SpringBootApplication
public class AprilApplication implements CommandLineRunner {

    // 通过类型注入,如果存在多个类型,可以通过指定名称注入
    // 此时通过类型MyService查找有两个Bean,因此抛出异常
    // Field myService in com.april.AprilApplication required a single bean, but 2 were found:
    @Autowired
    private MyService myService;

    @Override
    public void run(String... args) {
        myService.hello();
    }
    
    public static void main(String[] args) {
        SpringApplication.run(AprilApplication.class, args);
    }

}

5、@Autowired解决方案:指定名称注入

@SpringBootApplication
public class AprilApplication implements CommandLineRunner {

    // 1.直接指定名称注入
    @Autowired
    private MyService myServiceImpl01;
    
    // 2.或者使用注解指定名称注入
    @Autowired
    @Qualifier("myServiceImpl02")
    private MyService myService;

    @Override
    public void run(String... args) {
	    myServiceImpl01.hello();
        myService.hello();
        
    }
    
    public static void main(String[] args) {
        SpringApplication.run(AprilApplication.class, args);
    }

}

控制台输出:
MyServiceImpl01 print hello!
MyServiceImpl02 print hello!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值