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

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

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

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

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

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

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

     若是扫描多个包,则上面的代码书写多行,改变 base-package 里面的内容便可!ide

第三步:在 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">ui

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

二、@Repository    @Service   @Controller

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

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

三、注解 @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进行匹配,匹配成功,则赋值,不成功则报错

四、注解 @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

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

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

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

相关文章
相关标签/搜索