Spring自动装配Bean

 自动装配Bean可以简化我们的代码,这里以一个例子进行讲解,首先创建三个实体类

package com.zhiying.pojo;

public class Cat {
    public void shout() {
        System.out.println("miao~");
    }
}
package com.zhiying.pojo;

public class Dog {
    public void shout() {
        System.out.println("wang~");
    }
}
package com.zhiying.pojo;

public class People {
    private String name;
    private Dog dog;
    private Cat cat;

    public String getName() {
        return name;
    }

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

    public Dog getDog() {
        return dog;
    }

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

    public Cat getCat() {
        return cat;
    }

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

    @Override
    public String toString() {
        return "People{" +
                "name='" + name + '\'' +
                ", dog=" + dog +
                ", cat=" + cat +
                '}';
    }
}

然后是配置文件,核心

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="dog" class="com.zhiying.pojo.Dog"/>
    <bean id="cat" class="com.zhiying.pojo.Cat"/>

<!--    这是正常的注入方式-->
<!--    <bean id="people" class="com.zhiying.pojo.People">-->
<!--        <property name="name" value="贺志营"/>-->
<!--        <property name="cat" ref="cat"/>-->
<!--        <property name="dog" ref="dog"/>-->
<!--    </bean>-->


<!--
    用autowire实现自动装配
    byName:会自动在容器上下文查找,和自己对象set方法后面的值对应的beanid
    byType:会自动在容器上下文查找,和自己属性类型相同的bean
-->

<!--    用autowire实现自动装配,使用byName-->
<!--    <bean id="people" class="com.zhiying.pojo.People" autowire="byName">-->
<!--        <property name="name" value="贺志营"/>-->
<!--    </bean>-->

<!--    用autowire实现自动装配,使用byType-->
        <bean id="people" class="com.zhiying.pojo.People" autowire="byType">
            <property name="name" value="贺志营"/>
        </bean>

</beans>

最后是测试

import com.zhiying.pojo.People;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        People people = (People) context.getBean("people");
        people.getCat().shout();
        people.getDog().shout();
    }
}

总结:

byName的时候,需要保证所有的bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致

byType的时候,需要保证所有的bean的class唯一,并且这个bean需要和自动注入的属性的类型一致

注解实现自动装配

@Autowired注解

同样的先是实体类

package com.zhiying.pojo;

public class Cat {
    public void shout() {
        System.out.println("miao~");
    }
}
package com.zhiying.pojo;

public class Dog {
    public void shout() {
        System.out.println("wang~");
    }
}
package com.zhiying.pojo;

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

public class People {

    private String name;
    @Autowired
    private Dog dog;
    @Autowired
    private Cat cat;

    public String getName() {
        return name;
    }

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

    public Dog getDog() {
        return dog;
    }

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

    public Cat getCat() {
        return cat;
    }

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

    @Override
    public String toString() {
        return "People{" +
                "name='" + name + '\'' +
                ", dog=" + dog +
                ", cat=" + cat +
                '}';
    }
}

然后是配置文件,需要导入context约束,加入注解的支持

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

<!--    开启注解-->
    <context:annotation-config/>

    <bean id="cat" class="com.zhiying.pojo.Cat"/>
    <bean id="dog" class="com.zhiying.pojo.Dog"/>
    <bean id="people" class="com.zhiying.pojo.People"/>

</beans>

然后是测试

import com.zhiying.pojo.People;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        People people = (People) context.getBean("people");
        people.getCat().shout();
        people.getDog().shout();
    }
}

@Resource注解

这个注解是Java提供的,但是配置文件还是不能少的,只是在People类中导入的是javax包,这里只是修改People类,其他不变

package com.zhiying.pojo;

import javax.annotation.Resource;

public class People {

    private String name;
    @Resource
    private Dog dog;
    @Resource
    private Cat cat;

    public String getName() {
        return name;
    }

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

    public Dog getDog() {
        return dog;
    }

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

    public Cat getCat() {
        return cat;
    }

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

    @Override
    public String toString() {
        return "People{" +
                "name='" + name + '\'' +
                ", dog=" + dog +
                ", cat=" + cat +
                '}';
    }
}

总结:

@Autowired和@Resource的区别:

@Autowired是通过byType的方式实现,且要求这个对象必须存在

@Resource是通过byName的方式实现,如果找不到名字,则通过byType的方式实现,都找不到报错

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贺志营

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值