Spring学习 -- bean自动装配

自动装配是spring满足bean依赖的一种方式

spring 会在上下文自动寻找,并自动给bean装配属性

在spring中有三种装配方式:

  1. 在xml中显示的配置
  2. 在java中显示的配置
  3. 隐式的自动装配bean【重要】

例子:
环境搭建一个人有两个宠物

public class Cat {
    public void shout(){
        System.out.println("苗");
    }
}
public class Dog {
    public void shout(){
        System.out.println("你的狗叫什么?");
    }
}

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

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

    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 getName() {
        return name;
    }

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

public class MyTest {

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

    }

}
<?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="dog" class="com.xh.Dog">

    </bean>
   <bean id="cat" class="com.xh.Cat">

   </bean>
    <bean id="people" class="com.xh.People">
        <property name="name" value="胡小黑"/>
        <property name="cat"  ref="cat"/>
        <property name="dog"  ref="dog"/>

    </bean>

</beans>

ByName自动装配

<!--   ByName:会自动 在容器上下文查找,和自己对象set方法后边对应的 bean id -->
    <bean id="people" class="com.xh.People" autowire="byName">
        <property name="name" value="是小胡呀"/>
    </bean>
    <bean id="dog" class="com.xh.Dog">

    </bean>
   <bean id="cat" class="com.xh.Cat">
  public void setCat(Cat cat) {
        this.cat = cat;
    }
    public void setDog(Dog dog) {
        this.dog = dog;
    }
    public void setName(String name) {
        this.name = name;
    }

ByType

<!--    ByType 不会根据id进行查找,和自己对象属性类型相同的bean-->
    <bean id="people" class="com.xh.People" autowire="byType">
    <property name="name" value="是小胡呀"/>
    </bean>
 <bean id="dog" class="com.xh.Dog">

    </bean>
   <bean id="11cat" class="com.xh.Cat">

小结:

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

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

注解实现自动配置

JDk1.5以后支持注解,spring 2.5支持注解‘

使用注解需要注意的是:

  1. 导入约束context约束
  2. 配置注解的支持 < context:annotation-config />
<?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 class="example.SimpleMovieCatalog" primary="true">
        <!-- inject any dependencies required by this bean -->
    </bean>

    <bean class="example.SimpleMovieCatalog">
        <!-- inject any dependencies required by this bean -->
    </bean>

    <bean id="movieRecommender" class="example.MovieRecommender"/>

</beans>

@Autowired


<?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"
      xmlns:aop="http://www.springframework.org/schema/aop"
      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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
   <!--开启注解的支持-->
   <context:annotation-config/>

   <bean id="dog" class="com.xh.Dog">
   </bean>
   <bean id="cat" class="com.xh.Cat">
   </bean>
   <bean id="people" class="com.xh.People"></bean>
   <!-- <bean id="people" class="com.xh.People">
        <property name="name" value="胡小黑"/>
        <property name="cat"  ref="cat"/>
        <property name="dog"  ref="dog"/>

    </bean>
    -->
   <!--   ByName:会自动 在容器上下文查找,和自己对象set方法后边对应的 beanid-->
   <!--    <bean id="people" class="com.xh.People" autowire="byName">
           <property name="name" value="是小胡呀"/>
       </bean>-->

   <!--    ByType 不会根据id进行查找,和自己对象属性类型相同的bean-->
   <!--
   <bean id="people" class="com.xh.People" autowire="byType">
       <property name="name" value="是小胡呀"/>
   </bean>
   -->
</beans>
import org.springframework.beans.factory.annotation.Autowired;

public class People {
    @Autowired
    private  Cat cat;
    @Autowired  //使用之后get set方法都不需要了。
    private  Dog dog;
    private  String name;

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

    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 getName() {
        return name;
    }

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

直接在属性上使用,也可以在set方式上使用

使用Autowired我们连set方法都不需要使用了,前提条件是你这个自动装配的属性在IOC(Spring)容器中存在,且符合byName;

注意:

@Nullable 字段标记了这个注解,说明字段可以为null

public @interface Autowired {
    boolean required() default true;
}

@Autowired(required = false)
//如果显示的定义了Autowired的required为false标志这个值可以为空否则不允许为空

如果@Autowired自动装配环境比较复杂的时候自动装配无法通过一个注解@Autowired完成的时候,使用@Qualifier(value=“***”)去配置@Autowired的使用,指定一个唯一的bean对象注入

java自带@Resource

@Resource注解,首先去spring容器中查找id名字,找不到查找类型,在找不到报错。唯一,不能查找重复的.

@Autowired//使用之后get set方法都不需要了。
    @Qualifier(value = "dog") //如果存在多对相同的,需要精准查找到一个,就需要这个注解指定了
    private  Dog dog;

@Resource

@Resource(name="cat")
    private  Cat cat;
@Resource
private  Dog dog;

@Resource 和 @Autowired区别

  1. 都是用来自动装配的,都可以放到属性字段上

  2. 提供方不同
    ​ @Autowired 是Spring提供的,@Resource 是J2EE提供的。

  3. 装配时默认类型不同
    ​ @Autowired只按type装配,@Resource默认是按name装配。

使用区别
(1)@Autowired与@Resource都可以用来装配bean,都可以写在字段或setter方法上

(2)@Autowired默认按类型装配,默认情况下必须要求依赖对象存在,如果要允许null值,可以设置它的required属性为false。如果想使用名称装配可以结合@Qualifier注解进行使用。

(3)@Resource,默认按照名称进行装配,名称可以通过name属性进行指定,如果没有指定name属性,当注解写在字段上时,默认取字段名进行名称查找。如果注解写在setter方法上默认取属性名进行装配。当找不到与名称匹配的bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Miaow.Y.Hu

赏一口饭吃吧,大大们

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

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

打赏作者

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

抵扣说明:

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

余额充值