【Spring】自动装配(autowire)

本文主要介绍从手动装配到自动装配、自动装配解决什么问题、自动装配的缺点。

Car、Address类实例需要装配到Person类实例中,接下来将创建Person、Car、Address及测试类,并通过手动装配和自动装配的方式解释自动装配解决的问题,存在的缺点,并说明可能会出现的异常。

Person类

package com.wusuiwei.spring.beans.autowire;

public class Person {
    private String name;
    private Address address;
    private Car car;

    public String getName() {
        return name;
    }

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

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public Car getCar() {
        return car;
    }

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

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", address=" + address +
                ", car=" + car +
                '}';
    }
}

Address类

package com.wusuiwei.spring.beans.autowire;

public class Address {
    private String city;
    private String street;

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    @Override
    public String toString() {
        return "Address{" +
                "city='" + city + '\'' +
                ", street='" + street + '\'' +
                '}';
    }
}

Car类

package com.wusuiwei.spring.beans.autowire;

public class Car {
    private String brand;
    private double price;

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Car{" +
                "brand='" + brand + '\'' +
                ", price=" + price +
                '}';
    }
}

Main类(测试类)

package com.wusuiwei.spring.beans.autowire;

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

public class Main {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans-autowire.xml");
        Person person = (Person) context.getBean("person");
        System.out.println(person);
    }
}

手动装配

beans-autowire.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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="address" class="com.wusuiwei.spring.beans.autowire.Address" p:city="Beijing" p:street="HuiLongGuan"></bean>
    <bean id="car" class="com.wusuiwei.spring.beans.autowire.Car" p:brand="Audi" p:price="30000" ></bean>
    <bean id="person" class="com.wusuiwei.spring.beans.autowire.Person" p:name = "jack" p:address-ref="address" p:car-ref="car"></bean>

</beans>

自动装配

Spring引入Autowire(自动装配)机制就是为了解决标签下标签过多的问题,标签过多会引发两个问题:

  • 如果一个Bean中要注入的对象过多,比如十几二十个(这是很正常的),那将导致Spring配置文件非常冗长,可读性与维护性差
  • 如果一个Bean中要注入的对象过多,配置麻烦且一不小心就容易出错

因此,为了解决使用标签注入对象过多的问题,Spring引入自动装配机制,简化开发者配置难度,降低xml文件配置大小。

beans-autowire.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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="address" class="com.wusuiwei.spring.beans.autowire.Address" p:city="Beijing" p:street="HuiLongGuan"></bean>
    <bean id="car" class="com.wusuiwei.spring.beans.autowire.Car" p:brand="Audi" p:price="30000" ></bean>
    <bean id="person" class="com.wusuiwei.spring.beans.autowire.Person" p:name = "jack" autowire="byName"></bean>
</beans>

Spring支持4种自动装配方式:

常用byName与byType:

1.byName ---根据bean的名字和当前bean的setter风格的属性名进行自动装配,若有匹配的,则自动装配,若无则不装配

2.byType ---根据bean的类型和当前bean值的属性类型进行自动装配,若IOC容器中有1个以上的类型匹配的bean,则抛异常(org.springframework.beans.factory.NoUniqueBeanDefinitionException)。

beans-autowire.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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="address" class="com.wusuiwei.spring.beans.autowire.Address" p:city="Beijing" p:street="HuiLongGuan"></bean>
    <bean id="car" class="com.wusuiwei.spring.beans.autowire.Car" p:brand="Audi" p:price="30000" ></bean>
    <bean id="car2" class="com.wusuiwei.spring.beans.autowire.Car" p:brand="Audi" p:price="30000" ></bean>
    <bean id="person" class="com.wusuiwei.spring.beans.autowire.Person" p:name = "jack" autowire="byType"></bean>
</beans>

执行结果:

自动装配的缺点:

1.在Bean配置文件中设置autowire属性进行自动装配时会装配所有属性,然而,若只希望自动装配个别属性时,autowire属性就不够灵活了。

2.autowire属性要么根据类型自动装配,要么根据名称自动装配,不能兼而有之。

因此,一般情况下,在实际项目中,很少使用自动装配功能,因为自动装配功能所带来的好处比起来,明确清晰的配置文档更有说服力一些。

转载于:https://my.oschina.net/u/3867381/blog/1827275

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值