Spring自动装配【Bean的作用域、@Autowried、@Resource】

简介: Spring自动装配【Bean的作用域、@Autowried、@Resource】

Bean 作用域

1、singleton(单例模式)

默认就是单例模式,不需要单独设置。

    <bean id="user" class="com.study.pojo.User" p:name="燕双鹰" p:age="25" scope="prototype"/>
ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
Person person = context.getBean("person", Person.class);
Person person1 = context.getBean("person", Person.class);
System.out.println(person==person1);    //true

2、prototype

    <bean id="user" class="com.study.pojo.User" p:name="燕双鹰" p:age="25" scope="singleton"/>

每次从容器中 get ,都会产生一个新对象!

ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
Person person = context.getBean("person", Person.class);
Person person1 = context.getBean("person", Person.class);
System.out.println(person==person1);    //false

3、其它

request、session、application 这些只在 web 开发中使用到!

Bean 的自动装配

  • 自动装配是Spring满足Bean依赖的一种方式!
  • Spring会在上下文中自动寻找,并自动给Bean装配属性!

1、测试

编写三个类Cat、Dog、People。

public class Dog {
    public void shout(){
        System.out.println("woof~");
    }
}
public class Cat {
    public void shout(){
        System.out.println("miu~");
    }
}
public class People {
    private Cat cat;
    private Dog dog;
    private String 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;
    }
    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}

在Spring的beans.xml中注册,这也是我们普通的装配方式。

    <bean id="cat" class="com.study.autowried.Cat"/>
    <bean id="dog" class="com.study.autowried.Dog"/>
    <bean id="people" class="com.study.autowried.People" autowire="byName">
        <property name="dog" ref="dog"/>
        <property name="cat" ref="cat"/>
        <property name="name" value="李元芳"/>
    </bean>

byName 自动装配

byName需要bean标签的id属性和set方法对应!setDog方法必须对应 bean标签属性id为 "dog"。

    <bean id="cat" class="com.study.autowried.Cat"/>
    <bean id="dog" class="com.study.autowried.Dog"/>
    <!--
    byName:会自动在容器上下文自动查找和自己对象set方法对应的beanid
    -->
    <bean id="people" class="com.study.autowried.People" autowire="byName">
        <property name="name" value="李元芳"/>
    </bean>

byType 自动装配

byType不需要设置bean标签的id属性,因为它是根据bean的类去查找的,所以必须保证所以bean标签的 class 属性唯一。

    <bean class="com.study.autowried.Cat"/>
    <bean class="com.study.autowried.Dog"/>
    <!--
    byType:会自动在容器上下文自动查找和自己对象类型相同的bean
    -->
    <bean id="people" class="com.study.autowried.People" autowire="byName">
        <property name="name" value="李元芳"/>
    </bean>

使用注解实现自动装配@Autowired

使用注解需要在Spring的beans.xml中

  • 导入约束:  xmlns:context="http://www.springframework.org/schema/context"
  • 配置注解的支持:<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
       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:annotation-config/>
</beans>

      默认情况下,@Autowired注解是通过byType方式实现自动装配的,如果希望通过byName方式进行自动装配,可以在@Autowired注解上结合@Qualifier注解使用。

测试

<?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:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       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:annotation-config/>
    <bean id="cat" class="com.study.autowried.Cat"/>
    <bean id="dog" class="com.study.autowried.Dog"/>
    <bean id="people" class="com.study.autowried.People"/>
</beans>

使用Autowried注解我们可以不用编写Set方法。

import org.springframework.beans.factory.annotation.Autowired;
public class People {
    //直接加载属性上即可实现自动装配
    @Autowired
    private Cat cat;
    @Autowired
    private Dog dog;
    private String name;
    public Cat getCat() {
        return cat;
    }
    public Dog getDog() {
        return dog;
    }
    public String getName() {
        return name;
    }
    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}

其它

@Autowried的required属性

如果Autowried的属性required的值为false,则这个对象可以为null,否则不可以为空(默认为true,不可为空)。

public class People {
    @Autowired(required = false)
    private Cat cat;
    @Autowired
    private Dog dog;
    private String name;
}

@Qualifier

如果@Autowried自动装配的环境比较复杂,我们可以使用@Qualifier(value="beanid")来指定唯一的bean对象注入!

<?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:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       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:annotation-config/>
    <bean id="cat" class="com.study.autowried.Cat"/>
    <bean id="cat1" class="com.study.autowried.Cat"/>
    <bean id="dog" class="com.study.autowried.Dog"/>
    <bean id="dog1" class="com.study.autowried.Dog"/>
    <bean id="people" class="com.study.autowried.People"/>
</beans>

多个Dog对象和Cat对象,我们需要通过@Qualifier 指定具体的bean的id。

public class People {
    @Autowired
    @Qualifier(value = "cat1")
    private Cat cat;
    @Autowired
    @Qualifier(value = "dog1")
    private Dog dog;
    private String name;
}

@Resource

上面的都是spring中的注解,而这个@Resource是我们java原生的注解,它同样可以实现自动装配。它会先通过byName的方式通过set方法查找对应beanid的bean;如果找不到,就会通过byType的方式通过class找唯一类型的bean;如果我们有多个相同类型的bean,我们可以通过@Resource(name="")的形式来指定对应的bean。

<?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:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       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:annotation-config/>
    <bean id="cat" class="com.study.autowried.Cat"/>
    <bean id="cat1" class="com.study.autowried.Cat"/>
    <bean id="dog" class="com.study.autowried.Dog"/>
    <bean id="dog1" class="com.study.autowried.Dog"/>
    <bean id="people" class="com.study.autowried.People"/>
</beans>
public class People {
    @Resource
    private Cat cat;    //匹配到bean id=cat的bean对象
    @Resource(name="dog1")
    private Dog dog;    //匹配到bean id=dog1的bean对象
    private String name;
}

小结

@Resource和@Autowried的区别:

  • @Autowried 默认通过 byType实现,如果有多个bean可以搭配Qualifier(value="beanid")来实现。
  • @Resource 默认通过 byName实现,如果找不到,会使用 byType实现,同样可以通过@Resource(name="beanid")来匹配指定id的bean。
相关文章
|
17天前
|
XML Java 数据格式
Spring5系列学习文章分享---第一篇(概述+特点+IOC原理+IOC并操作之bean的XML管理操作)
Spring5系列学习文章分享---第一篇(概述+特点+IOC原理+IOC并操作之bean的XML管理操作)
19 1
|
17天前
|
XML druid Java
Spring5系列学习文章分享---第二篇(IOC的bean管理factory+Bean作用域与生命周期+自动装配+基于注解管理+外部属性管理之druid)
Spring5系列学习文章分享---第二篇(IOC的bean管理factory+Bean作用域与生命周期+自动装配+基于注解管理+外部属性管理之druid)
25 0
|
19天前
|
Java Spring 容器
解读spring5源码中实例化单例bean的调用链
解读spring5源码中实例化单例bean的调用链
|
10天前
|
Java 微服务 Spring
微服务04---服务远程调用,根据订单id查询订单功能,根据id查询订单的同时,把订单所属的用户信息一起返回,Spring提供了一个工具RestTemplate,Bean写在对象前面,以后可以在任何地
微服务04---服务远程调用,根据订单id查询订单功能,根据id查询订单的同时,把订单所属的用户信息一起返回,Spring提供了一个工具RestTemplate,Bean写在对象前面,以后可以在任何地
|
13天前
|
Java 开发者 Spring
自动装配在Spring框架中的原理与实现方式
自动装配在Spring框架中的原理与实现方式
|
17天前
|
Java Spring
Spring注解内容----用来替代Bean
Spring注解内容----用来替代Bean
|
18天前
|
Java Linux 程序员
技术笔记:Spring生态研习【五】:Springboot中bean的条件注入
技术笔记:Spring生态研习【五】:Springboot中bean的条件注入
|
19天前
|
Java Spring
聊聊Spring中两种创建Bean的方式:BeanDefinition.setInstanceSupplier() 和 FactoryBean
聊聊Spring中两种创建Bean的方式:BeanDefinition.setInstanceSupplier() 和 FactoryBean
|
XML Java 数据格式
[Spring实战系列](13)使用注解自动装配
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SunnyYoona/article/details/50644820 1. 简介 从Spring2.5开始,我们就可以使用注解的自动装配方式装配Spring Bean的属性。
863 0