@Resource那点事

前言:

    在项目开发中,@Autowired和@Resource之争,一直搞不清楚,反正也不想搞清楚到底什么时候用@Autowired,什么场景下用@Resource,就一直用@Autowired得了。

    现在又到了还账的时候了,必须写一篇文章搞懂@Resource那点事

    上一篇文章写了@Autowired的使用方式,有兴趣的读者可以找来看下,后面我们会对这两种作对比。

 

1.@Resource介绍

    首先@Resource这个注解不是Spring的,而是JDK的,包路径为javax.annotation.Resource。内容如下

@Target({TYPE, FIELD, METHOD})
@Retention(RUNTIME)
public @interface Resource {
    /**
     * The JNDI name of the resource.  For field annotations,
     * the default is the field name.  For method annotations,
     * the default is the JavaBeans property name corresponding
     * to the method.  For class annotations, there is no default
     * and this must be specified.
     */
    String name() default "";
    ...
}

    可以看到这个注解可以添加在类、属性、方法上。

    最主要的属性是name,可以用来指定具体需要加载的bean的名称,如果不指定的话,默认会使用属性的名称。

    下面来看下使用,然后再做总结

 

1.@Resource示例

    示例主要还是基于@Autowired时候的示例,还是Service和DAO那点事,具体可参考https://blog.csdn.net/qq_26323323/article/details/90140201 

 

    1)Service、DAO层接口及实现类

// DAO
public interface StudentDao {

    public void add(Student stu);
    public void delete(Student stu);
}

// Service
public interface StudentService {

    public void add(Student stu);
    public void delete(Student stu);
}

// 主动指定一个名称,不使用自定义的名称studentDaoImpl
@Repository("stuDao")
public class StudentDaoImpl implements StudentDao {}

// 创建另一个实现,名称为stuDao1
@Repository("stuDao1")
public class StudentDaoImpl1 implements StudentDao {

    @Override
    public void add(Student stu) {
        System.out.println("dao1 insert student..." + stu);
    }

    @Override
    public void delete(Student stu) {
        System.out.println("dao1 delete student..." + stu);
    }
}


// ServiceImpl
@Service("stuResService")
public class StudentResourceServiceImpl implements StudentService {

//    @Resource(name = "stuDao1")
    @Resource
    private StudentDao studentDao;

    @Override
    public void add(Student stu) {
        studentDao.add(stu);
    }

    @Override
    public void delete(Student stu) {
        studentDao.delete(stu);
    }
}

    2)AnnotationConfiguration类用于扫描包下的带有Annotation的类

@Configuration
@ComponentScan(basePackages = "annotation")
public class AnnotationConfiguration {
}

    这些环节都一样,不再赘述

    3)测试

@Test
public void testResourceAnnotationConfig(){
    AnnotationConfigApplicationContext applicationContext
        = new AnnotationConfigApplicationContext(AnnotationConfiguration.class);

    // 获取我们指定的service
    StudentService studentService = (StudentService) applicationContext.getBean("stuResService");
    Student stu = new Student();
    stu.setName("lucy");
    stu.setAge(20);
    studentService.add(stu);
}

// 结果会报错,报错信息如下
// No qualifying bean of type 'annotation.StudentDao' available: expected single matching bean but found 2: stuDao,stuDao1

    总结:之所以会报错,是因为@Resource的加载流程如下:

    * 先根据名称匹配,查看是否有一个name为studentDao的bean,如果有就直接装配

    * 如果名称匹配不上,则按照类型进行匹配,查找是否有这个类型的bean

    当前会查找到两个StudentDao的实现类,所以直接报错

 

    那我们该如何修改到正确呢?

@Service("stuResService")
public class StudentResourceServiceImpl implements StudentService {

    @Resource(name = "stuDao1")
    private StudentDao studentDao;

    像这样,我们直接指定需要加载的bean的名称即可

 

2.@Resource和@Primary

    上面这种方式,是@Resource常用的一种方式,直接指定bean 的名称,那么如果我们就是想使用类型加载呢?此时又有几个StudentDao的实现类,该如何做呢?    

    答案就是:我们可以在我们期望被加载到的StudentDao实现类上加上@Primary,代表这个会被优先加载即可

 

3.@Resource与@Autowired

    以下总结摘自https://www.cnblogs.com/think-in-java/p/5474740.html  

    * @Autowired默认是按照类型加载(byType)依赖对象的。如果需要按照名称来加载,那么需要配合@Qualifier来使用

    * @Resource默认是按照名称加载(byName)依赖对象的。@Resource有两个重要的属性:name和type,而Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以,如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不制定name也不制定type属性,这时将通过反射机制使用byName自动注入策略

    

 

参考:

https://docs.spring.io/spring/docs/4.3.23.RELEASE/spring-framework-reference/htmlsingle/ 

代码地址:https://github.com/kldwz/springstudy  

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

恐龙弟旺仔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值