Spring之使用注解实例化Bean并注入属性

1.准备工作

(1)导入jar包

除了上篇文章使用到的基本jar包外,还得加入aop的jar包,所有jar包如下

Spring之使用注解实例化Bean并注入属性
所需jar包

(2)配置xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 开启注解扫描,到包里面扫描类、属性、方法上是否有注解 -->
    <context:component-scan base-package="com.codeliu"/>
</beans>

2.注解实例化Bean

(1)实例化Bean有四个注解

  • @Component
  • @Service:业务层
  • @Controller:WEB层
  • @Repository:持久层

虽然分了层,但目前这四个注解的功能是一样的。

@Service(value = "user") //相当于bean标签中的id,四种方式功能一样
// @Component(value = "user")
// @Controller(value = "user")
// @Repository(value = "user")
@Scope(value = "singleton")
public class User {
    public void print() {
        System.out.println("user......");
    }
}
    @Test
    /**
     * 使用注解实例化User类
     */
    public void testUser() {
        // 读取配置文件
        @SuppressWarnings("resource")
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        User user = (User)context.getBean("user");
        user.print();
    }

bean的作用域配置

  Spring中可以使用scope属性来配置bean的作用域:

  singleton:单例,在初始化配置文件时生成单例bean对象

  prototype:原型的,在初始化配置文件时不生成bean对象,使用时返回不同的bean对象

  request:web环境下每一个request请求都会返回一个不同的bean,只在本次请求中有效

  session:web环境下每一个request请求都会返回一个不同的bean,在session中有效

3.注解为属性赋值

首先来个UserDao

@Component(value = "userDao")
public class UserDao {
    public void print() {
        System.out.println("userdao......");
    }
}

再来个service

@Service(value = "userService")
public class UserService {
    // 注入对象类型的属性,不用提供set方法
    // 1.使用自动装配
    // @Autowired  // 为byType类型,如果有多个相同类型的,得配合@Qualifier注解
    // private UserDao userDao;
    
    // 2.不使用自动装配
    @Resource(name = "userDao")
    private UserDao userDao;
    
    // 注入普通类型的属性
    @Value(value = "CodeTiger")
    private String name;
    
    public void print() {
        System.out.println("UserService......");
        userDao.print();
        System.out.println(name);
    }
}

为属性赋值,我们可以使用自动装配,也可以手动赋值。

    @Test
    /**
     * 使用注解注入属性
     */
    public void testUserService() {
        @SuppressWarnings("resource")
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        UserService service = (UserService)context.getBean("userService");
        service.print();
    }

4.注解和xml结合使用

当然我们也可以同时使用注解和xml,一般我们在xml中实例化Bean,使用注解为属性赋值。


Spring@Autowired注解与自动装配

1   配置文件的方法

我们编写spring 框架的代码时候。一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量。并且要配套写上 get 和 set方法。

Boss 拥有 Office 和 Car 类型的两个属性:   
  
清单 3. Boss.java

 

[java] view plain copy
 
 
 
  1. package com.baobaotao;     
  2.     
  3. public class Boss {     
  4.     private Car car;     
  5.     private Office office;     
  6.     
  7.     // 省略 get/setter     
  8.     
  9.     @Override    
  10.     public String toString() {     
  11.         return "car:" + car + "/n" + "office:" + office;     
  12.     }     
  13. }     
  System.out.println必须实现toString方法
  
我们在 Spring 容器中将 Office 和 Car 声明为 Bean,并注入到 Boss Bean 中:下面是使用传统 XML 完成这个工作的配置文件 beans.xml:   
  
清单 4. beans.xml 将以上三个类配置成 Bean   
                   
[xhtml] view plain copy
 
 
 
  1. <?xml version="1.0" encoding="UTF-8" ?>     
  2. <beans xmlns="http://www.springframework.org/schema/beans"    
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans      
  5. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">     
  6.     <bean id="boss" class="com.baobaotao.Boss">     
  7.         <property name="car" ref="car"/>     
  8.         <property name="office" ref="office" />     
  9.     </bean>     
  10.     <bean id="office" class="com.baobaotao.Office">     
  11.         <property name="officeNo" value="002"/>     
  12.     </bean>     
  13.     <bean id="car" class="com.baobaotao.Car" scope="singleton">     
  14.         <property name="brand" value=" 红旗 CA72"/>     
  15.         <property name="price" value="2000"/>     
  16.     </bean>     
  17. </beans>    
当我们运行以下代码时,控制台将正确打出 boss 的信息:   
  
清单 5. 测试类:AnnoIoCTest.java   
                   
[java] view plain copy
 
 
 
  1. import org.springframework.context.ApplicationContext;     
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;     
  3. public class AnnoIoCTest {     
  4.     
  5.     public static void main(String[] args) {     
  6.         String[] locations = {"beans.xml"};     
  7.         ApplicationContext ctx =      
  8.             new ClassPathXmlApplicationContext(locations);     
  9.         Boss boss = (Boss) ctx.getBean("boss");     
  10.         System.out.println(boss);     
  11.     }     
  12. }     
  13.       

  
这说明 Spring 容器已经正确完成了 Bean 创建和装配的工作。   

 

2   @Autowired 

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

要实现我们要精简程序的目的。需要这样来处理: 
* 在applicationContext.xml中加入: 

[c-sharp] view plain copy
 
 
 
  1. <!-- 该 BeanPostProcessor 将自动对标注 @Autowired 的 Bean 进行注入 -->     
  2.   <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>  

  Spring 通过一个 BeanPostProcessor 对 @Autowired 进行解析,所以要让 @Autowired 起作用必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。   

* 修改在原来注入spirng容器中的bean的方法。 
     在域变量上加上标签@Autowired,并且去掉 相应的get 和set方法

  清单 6. 使用 @Autowired 注释的 Boss.java                   

[java] view plain copy
 
 
 
  1. package com.baobaotao;     
  2. import org.springframework.beans.factory.annotation.Autowired;     
  3.     
  4. public class Boss {     
  5.     
  6.     @Autowired    
  7.     private Car car;     
  8.     
  9.     @Autowired    
  10.     private Office office;     
  11.     
  12.     …     
  13. }     

* 在applicatonContext.xml中 把原来 引用的<porpery >标签也去掉。 

[xhtml] view plain copy
 
 
 
  1.             
  2. <?xml version="1.0" encoding="UTF-8" ?>     
  3. <beans xmlns="http://www.springframework.org/schema/beans"    
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans      
  6. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">     
  7.     
  8.     <!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 -->     
  9.     <bean class="org.springframework.beans.factory.annotation.     
  10.         AutowiredAnnotationBeanPostProcessor"/>     
  11.     
  12.     <!-- 移除 boss Bean 的属性注入配置的信息 -->     
  13.     <bean id="boss" class="com.baobaotao.Boss"/>     
  14.       
  15.     <bean id="office" class="com.baobaotao.Office">     
  16.         <property name="officeNo" value="001"/>     
  17.     </bean>     
  18.     <bean id="car" class="com.baobaotao.Car" scope="singleton">     
  19.         <property name="brand" value=" 红旗 CA72"/>     
  20.         <property name="price" value="2000"/>     
  21.     </bean>     
  22. </beans>    

 这样,当 Spring 容器启动时,AutowiredAnnotationBeanPostProcessor 将扫描 Spring 容器中所有 Bean,当发现 Bean 中拥有 @Autowired 注释时就找到和其匹配(默认按类型匹配)的 Bean,并注入到对应的地方中去。   
  
按照上面的配置,Spring 将直接采用 Java 反射机制对 Boss 中的 car 和 office 这两个私有成员变量进行自动注入。所以对成员变量使用 @Autowired 后,您大可将它们的 setter 方法(setCar() 和 setOffice())从 Boss 中删除。   
  
当然,您也可以通过 @Autowired 对方法或构造函数进行标注,如果构造函数有两个入参,分别是 bean1 和 bean2,@Autowired 将分别寻找和它们类型匹配的 Bean,将它们作为 CountryService (Bean1 bean1 ,Bean2 bean2) 的入参来创建 CountryService Bean。来看下面的代码:  对方法

 

[java] view plain copy
 
 
 
  1. package com.baobaotao;     
  2.     
  3. public class Boss {     
  4.     private Car car;     
  5.     private Office office;     
  6.     
  7.      @Autowired    
  8.     public void setCar(Car car) {     
  9.         this.car = car;     
  10.     }     
  11.       
  12.     @Autowired    
  13.     public void setOffice(Office office) {     
  14.         this.office = office;     
  15.     }     
  16.     …     
  17. }     

 

这时,@Autowired 将查找被标注的方法的入参类型的 Bean,并调用方法自动注入这些 Bean。而下面的使用方法则对构造函数进行标注:   

[java] view plain copy
 
 
 
  1. package com.baobaotao;     
  2.     
  3. public class Boss {     
  4.     private Car car;     
  5.     private Office office;     
  6.       
  7.     @Autowired    
  8.     public Boss(Car car ,Office office){     
  9.         this.car = car;     
  10.         this.office = office ;     
  11.     }     
  12.       
  13.     …     
  14. }     

  由于 Boss() 构造函数有两个入参,分别是 car 和 office,@Autowired 将分别寻找和它们类型匹配的 Bean,将它们作为 Boss(Car car ,Office office) 的入参来创建 Boss Bean。  

 

Spring注解:自动注入属性(@Resource、@Autowired、@Qualifier )

先写一个场景,举例说明:

1、接口:IAnimal

public Interface IAnimal{
    ......
}


2、实现类:DogImpl ,实现了IAnimal接口。

@Service("dogImpl")
public class DaoImpl impliments IAnimal{
    ...
}


3、业务类:AnimalController

public class AnimalController {
    @Autowired
    private IAnimal dogImpl;
    ......
}

  假如有一个“动物”的接口 IAnimal, DogImpl类实现了接口 IAnimal, 且该接口只有 DogImpl这一个实现类,那么在引用实现类的时候,我们使用的是实现类的接口(像上面程序展示的那样)。Spring会按 byType的方式寻找接口的实现类,将其注入。  
  假如有另一个实现类 CatImpl 也实现了接口 IAnimal, 这时候再按上面的方式去引用, 在同时存在两个实现类的情况下,会出现什么情况呢?

  答:会报错。 这是由于 @Autowired 的特性决定的: @Autowired 的注入方式是 byType 注入, 当要注入的类型在容器中存在多个时,Spring是不知道要引入哪个实现类的,所以会报错。

  那么在同一类型拥有多个实现类的时候,如何注入呢?

  答:这种场景下,只能通过 byName 注入的方式。可以使用 @Resource 或 @Qualifier 注解。

       @Resource 默认是按照 byName 的方式注入的, 如果通过 byName 的方式匹配不到,再按 byType 的方式去匹配。所以上面的引用可以替换为:

public class AnimalController {
  @Resource(name="dogImpl") //实现类1中 @Service注解中标定的名称
  private IAnimal dogImpl;
  ......
}


       @Qualifier 注解也是 byName的方式,但是与@Resource 有区别,@Qualifier 使用的是 类名。

public class AnimalController {
    @Qualifier("DaoImpl") //实现类1的类名。注意区分与@Resource(name="dogImpl") 的区别。
    private IAnimal dogImpl;
    ......
}


总结:
  1、@Autowired 是通过 byType 的方式去注入的, 使用该注解,要求接口只能有一个实现类。
  2、@Resource 可以通过 byName 和 byType的方式注入, 默认先按 byName的方式进行匹配,如果匹配不到,再按 byType的方式进行匹配。
  3、@Qualifier 注解可以按名称注入, 但是注意是 类名。

 

附:https://www.jianshu.com/p/80df0a6daf65

  https://blog.csdn.net/l1212xiao/article/details/80424064