Spring详解(四)------注解配置DI

 第一步:在 applicationContext.xml 中引入命名空间

这里我们简单讲解一下这里引入的命名空间,简单来说就是用来约束xml文件格式的。第一个 xmlns:context ,这表示标签格式应该是 <context:标签名>

第二步:在 applicationContext.xml 文件中引入注解扫描器

<!-- 组件扫描,扫描含有注解的类 -->

    <context:component-scan base-package="com.ys.annotation"></context:component-scan>

  base-package:表示含有注解类的包名

     如果扫描多个包,则上面的代码书写多行,改变 base-package 里面的内容即可!

第三步:在 Person 类中添加注解@Component

  @Component

  如果一个类上加了@Component注解,就会进行如下的法则

              如果其value属性的值为""

                    @Component

                    public class Person {}

                      等价于

                    <bean id="person" class="..Person">

             如果其value属性的值不为""

                    @Component("p")

                    public class Person {}

                     等价于
 
                   <bean id="p" class="..Person">

  那么这就很好理解测试程序中,我们直接  context.getBean("person") 这样写。

2、@Repository    @Service   @Controller

  此外:下面三个注解是 @Component 注解的衍生注解,功能一样

@Repository :dao层
@Service:service层
@Controller:web层

3、注解 @Resource

  @Resource 注解,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。 通过 @Resource 的使用来消除 set ,get方法。

  首先创建一个 学生类 Student.java

  

  然后在 Person 类中添加一个属性 Student

  

  那么我们如何获取 Person 对象,并调用 showStudent()方法呢?这个问题简化就是如何给属性 Student 实例化,也就是依赖注入

  不使用注解:

<property name="students">
    <ref bean="student"/>
</property>
<bean id="student" class="com.ys.annotation_di.Student"></bean>

  使用注解:

  

  @Resource注解以后,判断该注解name的属性是否为""(name没有写)

    ①、如果没有写name属性,则会让属性的名称的值和spring配置文件bean中ID的值做匹配(如果没有进行配置,也和注解@Component进行匹配),如果匹配成功则赋值,如果匹配不成功,则会按照spring配置文件class类型进行匹配,如果匹配不成功,则报错

    ②、如果有name属性,则会按照name属性的值和spring的bean中ID进行匹配,匹配成功,则赋值,不成功则报错

4、注解 @Autowired

  功能和注解 @Resource 一样,可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。只不过注解@Resource 是按照名称来进行装配,而@Autowired 则是按照类型来进行装配。

第一步:创建接口 PersonDao

package com.ys.autowired;
 
public interface PersonDao {
     
    public void savePerson();
 
}

第二步:创建一个接口实现类 PersonDaoImplOne

package com.ys.autowired;
 
import org.springframework.stereotype.Component;
 
@Component("personDaoImplOne")
public class PersonDaoImplOne implements PersonDao{
 
    @Override
    public void savePerson() {
        System.out.println("save Person One");
         
    }
 
}

第三步:创建PersonService

package com.ys.autowired;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
@Service("personService")
public class PersonService{
    @Autowired
    private PersonDao personDao;
     
    public void savePerson() {
        this.personDao.savePerson();
    }
 
}

  注意:这里我们在 private PesronDao personDao 上面添加了注解 @Autowired,它首先会根据类型去匹配,PersonDao 是一个接口,它的实现类是 PesronDaoImpOne,那么这里的意思就是:

  PersonDao personDao = new PersonDaoImpOne();

  那么问题来了,如果 PersonDao 的实现类有多个呢?我们创建第二个实现类 PersonDaoImpTwo

package com.ys.autowired;
 
import org.springframework.stereotype.Component;
 
@Component("personDaoImplTwo")
public class PersonDaoImplTwo implements PersonDao{
 
    @Override
    public void savePerson() {
        System.out.println("save Person Two");
         
    }
 
}

  如果还是向上面那样写,那么测试就会报错。怎么解决呢?

  第一种方法:更改名称

  

  第二种方法:@Autowired 和 @Qualifier("名称") 配合使用

   

  在使用@Autowired时,首先在容器中查询对应类型的bean

    如果查询结果刚好为一个,就将该bean装配给@Autowired指定的数据

    如果查询的结果不止一个,那么@Autowired会根据名称来查找。

    如果查询的结果为空,那么会抛出异常。解决方法时,使用required=false

5、@Value给基本类型属性赋值:@Value:参数,方法,属性,给对应字段赋值;

6、@Qualifier指定需要装配的组件的名,而不是使用类型

7、@Primary:让Spring默认使用首选的bean进行自动装配;@Qualifier使其失效

原文地址:https://www.cnblogs.com/deityjian/p/11067423.html