4、spring使用@Autowired注解实现自动装配

在上一篇spring的五种自动装配方式 教程中,我们了解到spring利用xml方式进行操作的五种自动装配方式,而在这篇文章中我们将学习利用@Autowired注解的方式进行自动装配

@Autowired注解自动装配的三种方式

  • setter方式
  • 构造函数方式
  • 通过字段自动装配方式

例子

第一步:创建bean

Customer.java

package com.main.autowrite.autowired.annotation;

public class Customer {
    //即将自动装配的属性
    private Person person;
    private int type;
    public Person getPerson() {
        return person;
    }
    public void setPerson(Person person) {
        this.person = person;
    }
    public int getType() {
        return type;
    }
    public void setType(int type) {
        this.type = type;
    }
}

Person.java

package com.main.autowrite.autowired.annotation;

public class Person {
    private String name;
    private int age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

第二步:配置beans配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    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.xsd">
  <bean id="customer" 
        class="com.main.autowrite.autowired.annotation.Customer" >
        <property name="type" value="user"></property>
  </bean>
  <bean id="person" class="com.main.autowrite.autowired.annotation.Person">
        <property name="name" value="jack" />
        <property name="age" value="18"/>
  </bean>
</beans>

第三步:注册AutowiredAnnotationBeanPostProcessor

主要有以下两种方式进行注册

  • 在beans配置文件中添加spring上下文和“context:annotation-config”标签
  • 包含 AutowiredAnnotationBeanPostProcessorbean

    第一种:添加 Spring 上下文和context:annotation-config在beans配置文件中。
    更改你的beans配置文件如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    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.xsd">
  <context:annotation-config />
  <bean id="customer" 
        class="com.main.autowrite.autowired.annotation.Customer" >
        <property name="type" value="user"></property>
  </bean>
  <bean id="person" class="com.main.autowrite.autowired.annotation.Person">
        <property name="name" value="jack" />
        <property name="age" value="18"/>
  </bean>
</beans>

第二种:包含AutowiredAnnotationBeanPostProcessor
更改你的beans配置文件如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    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.xsd">
  <bean 
    class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
  <bean id="customer" 
        class="com.main.autowrite.autowired.annotation.Customer" >
        <property name="type" value="user"></property>
  </bean>
  <bean id="person" class="com.main.autowrite.autowired.annotation.Person">
        <property name="name" value="jack" />
        <property name="age" value="18"/>
  </bean>
</beans>

第四步:使用@Autowired注解进行自动装配

当你已经完成上述步骤后,此时你就可以使用@Autowired注解进行自动装配了。
主要有以下三种方式:

  1. 通过setter方法
  2. 通过constructor构造方法
  3. 直接在字段属性声明时使用@Autowired注解进行自动装配

    第一种:通过setter方法
    修改你的Customer类如下:

package com.main.autowrite.autowired.annotation;

import org.springframework.beans.factory.annotation.Autowired;

public class Customer {
    //即将自动装配的属性
    private Person person;
    private int type;
    public Person getPerson() {
        return person;
    }
    @Autowired
    public void setPerson(Person person) {
        this.person = person;
    }
    public int getType() {
        return type;
    }
    public void setType(int type) {
        this.type = type;
    }
}

第二种:通过constructor构造方法
修改你的Customer类如下

package com.main.autowrite.autowired.annotation;

import org.springframework.beans.factory.annotation.Autowired;

public class Customer {
    //即将自动装配的属性
    private Person person;
    private String type;
    @Autowired
    public Customer(Person person) {
        this.person = person;
    }

    //getter and setter methods
    //toString methods
}

第三种:
修改你的Customer类如下

package com.main.autowrite.autowired.annotation;

import org.springframework.beans.factory.annotation.Autowired;

public class Customer {
    //即将自动装配的属性
    @Autowired
    private Person person;
    private String type;
    //getter and setter methods
    //toString methods
}

第五步:测试(上述第四步的三种装配方式均可使用以下方式进行测试)

    @Test
    public void test(){

        ApplicationContext context = 
                new ClassPathXmlApplicationContext("com/main/autowrite/autowired/annotation/beans.xml");

        Customer customer = (Customer)context.getBean("customer");

        System.out.print(customer.toString());
    }

测试结果:

这里写图片描述

结论:使用@Autowired注解实现自动装配非常灵活,而且功能强大。
在这里可以思考一下,如果存在多个person bean,那么customer将会自动装配哪一个??可以参考下一篇关于@Qualifier注解的使用

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值