关闭

[Spring学习笔记]@Autowired

标签: springjava
78人阅读 评论(0) 收藏 举报
分类:

Spring中@Autowired的用法

首先确定一下需求

一共有三个对象:Car、Person、Company
Car有name和code两个属性;Person有name和age两个属性;Company有Car和Person两个属性,作为测试输出类。
需要实现的输出为:

Car [name=Car_A, code=10]
Person [name=Person_A, age=20]

Car和Person两个类的代码如下:
Car.class

package com.Spring;
public class Car {
    private String name;
    private int code;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String toString() {
        return "Car [name=" + name + ", code=" + code + "]";
    }

}

Person.class

package com.Spring;

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;
    }

    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }

}

不使用Spring框架的情况

在不使用Spring的时候,在不更改Car和Person两个类的情况下,我们需要这样写Company的代码:

package com.Spring;

public class Company {

    private Car car;
    private Person person;

    public Car getCar() {
        return car;
    }

    public void setCar(Car car) {
        this.car = car;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    public static void main(String[] args) {
        Company company = new Company();
        Car car = new Car();
        car.setCode(10);
        car.setName("Car_A");
        company.setCar(car);
        Person person = new Person();
        person.setAge(20);
        person.setName("Person_A");
        company.setPerson(person);
        System.out.println(company.car.toString());
        System.out.println(company.person.toString());
    }

}

这样既可完成上面说到的输出。

使用 Spring但不使用@Autowired

首先我们需要先写明需要注册的类,创建Beans.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-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:annotation-config />

    <bean id="CarBean" class="com.Spring.Car">
        <property name="name" value="Car_A" />
        <property name="code" value="10" />
    </bean>
    <bean id="PersonBean" class="com.Spring.Person">
        <property name="name" value="Person_A" />
        <property name="age" value="20" />
    </bean>
    <bean id="CompanyBean" class="com.Spring.Company">
        <property name="Car" ref="CarBean" />
        <property name="Person" ref="PersonBean" />
    </bean>
</beans>

更改Company类如下:

package com.Spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Company {

    private Car car;
    private Person person;

    public Car getCar() {
        return car;
    }

    public void setCar(Car car) {
        this.car = car;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "Beans.xml");
        Company company = (Company) context.getBean("CompanyBean");
        System.out.println(company.car.toString());
        System.out.println(company.person.toString());
    }

}

即可完成需求。、

使用@Autowired的情况

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

(就是为了少写配置文件的代码)

修改Beans.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-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:annotation-config />

    <bean id="CarBean" class="com.Spring.Car">
        <property name="name" value="Car_A" />
        <property name="code" value="10" />
    </bean>
    <bean id="PersonBean" class="com.Spring.Person">
        <property name="name" value="Person_A" />
        <property name="age" value="20" />
    </bean>
    <bean id="CompanyBean" class="com.Spring.Company">
    </bean>
</beans>

修改Company.java如下:

package com.Spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Company {
    @Autowired
    private Car car;
    @Autowired
    private Person person;

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "Beans.xml");
        Company company = (Company) context.getBean("CompanyBean");
        System.out.println(company.car.toString());
        System.out.println(company.person.toString());
    }

}

即可完成规定输出。这里需要注意@Autowired有两种查bean的方式:
首先bytype即按类型查找有没有符合的Bean,如果有且唯一即返回。
如果有相同type的则byname即查找符合的name,如果有则返回。
当然也可以使用@Qualifier进行指定byName方式注入

总结

@Autowired可以减少配置文件的代码,而且可以拥有更低的耦合度。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:24445次
    • 积分:1269
    • 等级:
    • 排名:千里之外
    • 原创:107篇
    • 转载:3篇
    • 译文:0篇
    • 评论:4条
    文章分类
    最新评论