DI注解

问题引入:

在过去,我们经常使用setter来给属性赋值,这样会造成要写很多代码,如果属性一多,代码量就非常的惊人,虽然说不用我们自己写setter,但是代码量还是要算的。

那么,为了解决这个问题,spring就引入了注解来为Di中的属性赋值。

我们拿代码来说话

public class Person implements Serializable {
    //基本类型
    private Long pid;
    //String
    private String pname;
    //引用类型
    //引入注解,name在这里指的是配置文件中bean的id
    @Resource(name="student_annotation")
    private Student student;
    //集合
    private List list;
    private Set set;
    private Map map;

    public void showStudent(){
        this.student.show();
    }

}



public class Student implements Serializable {
    public void show(){
        System.out.println("student");
    }
}

配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">
           <context:annotation-config></context:annotation-config>
           <bean id="person_annotation" class="cn.ansel.annotation.Person"></bean>
           <bean id="student_annotation" class="cn.ansel.annotation.Student"></bean>
</beans>

细心的人会发现,配置文件多了几行数据,没错。

         xmlns:context="http://www.springframework.org/schema/context"
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-2.5.xsd

是它的命名空间及地址

又因为我们使用了注解,所以要加上注解解析器,否则注解不起作用

<!--
    引入注解解析器
--> 
        <context:annotation-config></context:annotation-config>

测试类

public class annotationTest {
    @Test
    public void test(){
        //启动spring容器
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
        //得到对象
        Person person=(Person) applicationContext.getBean("person_annotation");
        //调用方法
        person.showStudent();
    }
}

运行:
这里写图片描述
成功输出student.

注解的运行流程及resource的取值:

1、启动spring容器,加载spring的配置文件
2、为配置文件中每一个bean创建对象
3、查找每一个bean中是否有@Resource,如果有的话,再看Resource有没有值,如果没有写name的话,默认是“”
这里写图片描述
从recource的源代码中可以看到,name的默认取值是空字符串,并且该注解可以存在于(如下为Type的源代码)
这里写图片描述
4、(a)如果resource里面的name没有写,即默认为空字符串,那么spring容器会按照使用该注解的引用名作为bean的id来与配置文件中的bean匹配。如果这一次匹配失败,那么spring容器会继续根据当前引用对象的类型与配置文件的bean匹配(!注意:这时候的匹配,如果相同的类很多的话,也会报错),如果还是匹配失败,那么还是报错。
(b)如果resource中的name有值,那么直接根据name的值与配置文件中bean的id匹配,如果匹配成功就直接创建对象,然后为这个引用赋值,如果匹配失败直接报错。
5、创建完对象,赋好值之后,就是我们客户端的调用,调用完后就是关闭spring容器。

注意:

由于这里的@Resource是javax.annotation.Resource 包(扩展包),这时候spring不服,于是推出了自己的注解:@Autowired
这个注解的作用是按照与引用类相同的类型与配置文件进行匹配。然后还有一个
@Qualifier(“student_annotation”)`,因为qualifier里面有一个属性value,所以前面直接把它的值写上去。这两个注解加起来的作用跟@Resource的作用是一样的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值