@Autowired和@Resource的区别

先看一个简单的用法

有两个service类(只贴实现类的代码):

@Service
public class TeamServiceImpl implements TeamService {

    @Override
    public void teamInfo() {
        System.out.println("teamInfo");
    }

}
@Service
public class CompanyServiceImpl implements CompanyService {

    @Override
    public void companyInfo() {
        System.out.println("companyInfo");
    }

}

在另外一个测试类中引用上面的两个service,一个用@Autowired注解,一个用@Resource注解:

@Service
public class TestServiceImpl implements TestService {

    @Autowired
    private TeamService teamService;

    @Resource
    private CompanyService companyService;

    @Override
    public void organization() {

        teamService.teamInfo();

        companyService.companyInfo();

    }
}

执行TestService里的organization()方法,可以看到引用进来的两个类里的方法都能正常执行:

 

 但是在IDEA用@Autowired时,会有黄色波浪线提示,大概意思是:spring推荐用构造函数的方式注入bean

 那我们再改成下面这样的方式:

@Service
public class TestServiceImpl implements TestService {

    @Resource
    private CompanyService companyService;

    private final TeamService teamService;

    @Autowired
    public TestServiceImpl(TeamService teamService) {
        this.teamService = teamService;
    }

    @Override
    public void organization() {

        teamService.teamInfo();

        companyService.companyInfo();

    }
}

调用TestService里的organization()方法,能正常打印输出。

直接使用变量方式注入的优点是代码简洁,使用构造器,个人觉得最大的一个好处是防止注入的类为空,另外循环依赖提示的也会更加提前一点。
关于这个问题,有一篇文章写的比较详细,请参考:https://www.cnblogs.com/joemsu/p/7688307.html

再看另外两个例子

第一个例子,用@Autowired时

两个类,都实现TeamService

TeamSecondServiceImpl implements TeamService

TeamServiceImpl implements TeamService

测试类里引入TeamService

@Service
public class TestServiceImpl implements TestService {

    private final TeamService teamService;

    @Autowired
    public TestServiceImpl(TeamService teamService) {
        this.teamService = teamService;
    }

    @Override
    public void organization() {

        teamService.teamInfo();

    }
}

启动项目时,会报错

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.xxx.TestServiceImpl required a single bean, but 2 were found:
    - teamSecondServiceImpl: defined in file [/xxx/xxx/TeamSecondServiceImpl.class]
    - teamServiceImpl: defined in file [/xxx/xxx/TeamServiceImpl.class]


Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

错误里的提示也非常明显,两个类实现同一个接口,不知道加载哪一个了,要么用@Primary标记优先级,要么用@Qualifier把bean名字加上。

简单点,直接用@Qualifier:

@Service
public class TestServiceImpl implements TestService {

    private final TeamService teamService;

    @Autowired
    public TestServiceImpl(@Qualifier("teamSecondServiceImpl")TeamService teamService) {
        this.teamService = teamService;
    }

    @Override
    public void organization() {

        teamService.teamInfo();

    }
}

执行organization()方法,正常输出:

第二个例子,用@Resource时

两个类都实现CompanyService

CompanySecondServiceImpl implements CompanyService

CompanyServiceImpl implements CompanyService

测试类里引入 CompanyService

@Service
public class TestServiceImpl implements TestService {

    @Resource
    private CompanyService companyService;

    @Override
    public void organization() {

        companyService.companyInfo();

    }
}

启动项目时,报错和上面一样,同样加上@Qualifier就可以了

@Service
public class TestServiceImpl implements TestService {

    @Resource
    @Qualifier("companySecondServiceImpl")
    private CompanyService companyService;

    @Override
    public void organization() {

        companyService.companyInfo();

    }
}

或者直接在@Resource加上name属性

@Resource(name = "companySecondServiceImpl")
private CompanyService companyService;

 额外说一点,在用@Qualifier时,里面bean的名字直接是写的要引用的类名,并且是小写开头,但是我并没有在对应的类上加上这个名字,是因为如果使用了@Component、@Repository、@Service、@Controller,不额外定义bean的name话,spring会默认以类名称的的首字母小写作为bean的name

@Autowired和@Resource简单的区别

  • @Resource是java自己的注解,@Autowired是spring的注解
  • @Resource默认按name进行注入,@Autowired默认按type注入
  • 当bean有冲突时或无法识别时,@Resource可以通过name注入,@Autowired只能通过@Qualifier或@Primary配合使用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值