03_Spring Bean的装配模式_基于Annotation配置方式

前言

Spring中尽管使用XML配置文件可以实现Bean的装配工作,但如果应用中Bean的数量较多,会导致XML配置文件过于臃肿,从而给维护和升级带来一定的困难。从JDK 5开始提供了名为Annotation(注解)的功能,Spring正是利用这一特性,Spring逐步完善对Annotation注解技术的全面支持,使XML配置文件不再臃肿。   

  常用注解:

    (1) @Component注解:是一个泛化的概念,仅仅表示一个组件(Bean),可以作用在任何层次。

    (2) @Repository注解:用于将数据访问层(DAO 层)的类标识为SpringBean

    (3) @Service注解:通常作用在业务层,但是目前该功能与@Component相同。

    (4) @Controller注解:标识表示层组件,但是目前该功能与@Component相同

    (5) @Resource注解:作用相当于@Autowired,配置对应的注解处理器完成Bean的自动配置工作。

    (6) @Autowired注解:默认按照Bean类型进行装配。@Autowired注解加上@Qualifier注解,可直接指定一个Bean实例名称来进行装配。

    (7) @Qualifier注解:与@Autowired注解配合,将默认按Bean类型装配修改为按Bean实例名称进行装配,Bean的实例名称由@Qualifier注解的参数指定。

1、applicationContext.xml配置

             1.引入context命名空间 、   2.配置 <context:component-scan base-package="xxx"></context:component-scan>  

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.2.xsd ">
    <!-- xml配置方式 -->
     <bean name="addr" class="com.wann.bean.Address">
        <property name="city" value="兰州"></property>
        <property name="street" value="西固西路"></property>
    </bean>
    <!-- 可以通过注解的方式 -->
    <context:component-scan base-package="com.wann"></context:component-scan>
</beans>

2.Bean的属性注入

1)普通属性

@Value(value="提醒")

private String info;

2)对象属性

@Autowired
@Qualifier("userDao")		
private UserDao userDao;
等价于
@Resource(name="userDao")
private UserDao userDao;

代码实现:

@Component("user")
@Scope(scopeName = "singleton")
public class User {
    //  @Value(value = "tom")
    private String name;
    // @Value(value = "19")
    private Integer age;
    private Address address;

    public String getName() {
        return name;
    }
    @Value(value = "tom")
    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }
    @Value(value = "19")
    public void setAge(Integer age) {
        this.age = age;
    }

    public Address getAddress() {
        return address;
    }
   // @Resource(name = "addr")
    @Autowired
    @Qualifier("addr")
    public void setAddress(Address address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User{" + "name='" + name + '\'' + ", age=" + age + ", city=" + address.getCity() + ", street=" + address.getStreet() +'}';
    }
}

注意:也可以将注解写在set方法

使用注解配置的Bean<bean>配置的一样,默认作用范围都是singleton

@Scope注解用于指定Bean的作用范围

Sping集成测试

@RunWith(SpringJUnit4ClassRunner.class) //帮我们创建容器
@ContextConfiguration("classpath:applicationContext.xml")//引入配置文件
public class TestJunit {
    @Resource(name="user")
    private User user;
    @Test
    public void test(){

        System.out.println(user);
    }

}

多种装配Bean方式比较

 

基于XML配置

基于注解配置

基于Java类配置

Bean定义

<bean id="..." class="..." />

@Component

衍生类@Repository

@Service @Controller

@Configuration标注类,@Bean标注提供Bean方法

Bean名称

通过 idname指定

@Component("person")

@Bean("person")

Bean注入

<property> 或者 通过p命名空间

@Autowired 按类型注入

@Qualifier按名称注入

在方法内部编写注入代码逻辑

适合场景

Bean来自第三方,使用其它命名空间

Bean的实现类由用户自己开发

实例化Bean的逻辑比较复杂

posted @ 2019-08-29 18:08  杰醍奋  阅读(411)  评论(0)    收藏  举报