Spring自动装配深入理解

一、使用配置实现自动装配

三个类 Cat 、Dog、Pepeole

public class Cat {
    public void shout() {
        System.out.println("miao~");
    }
}
public class Dog {
    public void shout() {
        System.out.println("wang~");
    }
}
public class People {

    private Cat cat;
    private Dog dog;
    private String str ;

    public People() {
    }

    public People(Cat cat, Dog dog, String str) {
        this.cat = cat;
        this.dog = dog;
        this.str = str;
    }

    public Cat getCat() {
        return cat;
    }

    public void setCat(Cat cat) {
        this.cat = cat;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }
}

最原始使用XML注入容器的用法是:

 <bean id="people" class="com.guo.pojo.People">
        <property name="cat" ref="cat"/>
        <property name="dog" ref="dog"/>
        <property name="str" value="guo"/>
 </bean>

以后需要添加的类都要在标签的中间写,这样导致配置耦合越来越繁重.

随后就有了使用XML实现自动装配

<bean id="dog" class="com.guo.pojo.Dog"/>
<bean id="cat" class="com.guo.pojo.Cat"/>
<bean id="people" class="com.guo.pojo.People" autowire="byName">
        <property name="str" value="qinjiang"/>
</bean>

可以看出类型自动装配和id 一点关系都没有,它是和后面的Class 绑定的类密切相关!并且上下文中只能有且唯一这个类。

   @Test
    public void testByType(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        People people = context.getBean("people", People.class);
        people.getDog().shout();
        people.getCat().shout();
    }

名称自动装配

<bean id="dog" class="com.guo.pojo.Dog"/>
<bean id="cat" class="com.guo.pojo.Cat"/>
<bean id="cat12" class="com.guo.pojo.Cat"/>
 
 <bean id="people" class="com.guo.pojo.People" autowire="byName">
        <property name="str" value="guo"/>
 </bean>

可以看出类型名称装配和id 关系密切,它会getCat()这对象,证明在set的时候,找到这个值对应的bean的id ,。跟这个class关系不大,跟People set方法后面的Cat关系很重大!!!!

测试

   @Test
    public void testByType(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        People people = context.getBean("people", People.class);
        people.getDog().shout();
        people.getCat().shout();
    }

2. 使用注解实现自动装配

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.xsd
       http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.guo.pojo"/>
</beans>

Cat类

import org.springframework.stereotype.Component;

@Component
public class Dog {
    public void shout() {
        System.out.println("wang~");
    }
}

Dog类

import org.springframework.stereotype.Component;

@Component
public class Dog {
    public void shout() {
        System.out.println("wang~");
    }
}

people类

@Component("people")
// 相当于配置文件中 <bean id="people" class="当前注解的类"/>
public class People {

    @Autowired
    private Cat cat;
    @Autowired
    private Dog dog;
    @Value("会桑故乡的樱花开了")// 相当于配置文件中 <property str="str" value="会桑故乡的樱花开了"/>
    private String str ;

    public People() {
    }

    public People(Cat cat, Dog dog, String str) {
        this.cat = cat;
        this.dog = dog;
        this.str = str;
    }

    public Cat getCat() {
        return cat;
    }

    public void setCat(Cat cat) {
        this.cat = cat;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }
}

测试

  @Test
    public void testAutowire() {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        People people = context.getBean("people", People.class);
        people.getDog().shout();
        people.getCat().shout();
        System.out.println(people.getStr());
    }

打印结果:

wang~
miao~
会桑故乡的樱花开了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值