自动装配Bean

发布时间 2023-09-07 11:39:30作者: 数星观月

一、Bean的作用域

ScopeDescription

singleton

(Default) Scopes a single bean definition to a single object instance for each Spring IoC container.

prototype

Scopes a single bean definition to any number of object instances.

request

Scopes a single bean definition to the lifecycle of a single HTTP request. That is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.

session

Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.

application

Scopes a single bean definition to the lifecycle of a ServletContext. Only valid in the context of a web-aware Spring ApplicationContext.

websocket

Scopes a single bean definition to the lifecycle of a WebSocket. Only valid in the context of a web-aware Spring ApplicationContext.

1、单例模式(Spring默认机制)

 这个模式下当从spring容器中取出对象时,spring容器中只有一个实例对象提供,相当于多个指针指向同一个对象。

<bean id="user1" class="com.along.pojo.User" p:name="阿龙" p:age="18" scope="singleton"/>
@Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
        User user1 = context.getBean("user1", User.class);
        User user2 = context.getBean("user1", User.class);
        System.out.println(user1==user2);

    }

2、原型模式

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

<bean id="user1" class="com.along.pojo.User" p:name="阿龙" p:age="18" scope="prototype"/>
@Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
        User user1 = context.getBean("user1", User.class);
        User user2 = context.getBean("user1", User.class);
        System.out.println(user1==user2);

    }

3、其他的request、session等在web中使用

二、Bean的自动装配

●自动装配是Spring满足bean依赖一种方式!
●Spring会在 上下文中自动寻找,并自动给bean装配属性!
在Spring中有三种装配的方式
1.在xml中显示的配置(手动配置)
2.在java中显示配置
3.隐式的自动装配bean [重要]

搭建测试环境:创建人、猫、狗三个实体类,猫狗类中都有shout这个方法。

1、xml中手动配置

<bean id="cat" class="com.along.pojo.Cat"/>
    <bean id="dog" class="com.along.pojo.Dog"/>
    <bean id="people" class="com.along.pojo.People">
        <property name="name"  value="阿龙"/>
        <property name="cat" ref="cat"/>
        <property name="dog" ref="dog"/>
</bean>
@Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        People people = context.getBean("people", People.class);
        System.out.println(people);
        people.getCat().shout();
        people.getDog().shout();
    }

2、自动装配(autowire,基于容器上下文)

byName自动装配

<bean id="cat" class="com.along.pojo.Cat"/>
    <bean id="dog" class="com.along.pojo.Dog"/>
    <!--
    byname:自动在容器上下文中寻找,和自己对象set方法后面的参数名对应的bean的id
    -->
    <bean id="people" class="com.along.pojo.People" autowire="byName">
        <property name="name"  value="阿龙"/>
</bean>

byType自动装配

<bean id="cat" class="com.along.pojo.Cat"/>
    <bean id="dog" class="com.along.pojo.Dog"/>
    <!--
    bytype:自动在容器上下文中寻找,和自己对象属性类型相同的bean
    -->
    <bean id="people" class="com.along.pojo.People" autowire="byType">
        <property name="name"  value="阿龙"/>
</bean>

小结:
●byname的时候, 需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致!
●bytype的时候,需要保证所有bean的class唯一,并且这个bean需要和自动注入的属性的类型一致!

三、使用注解实现自动装配

jdk1.5支持注解,Spring2.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/>

</beans>

@Autowired

直接在属性上使用即可!也可以在set方式上使用!
使用Autowired我们可以不用编写Set方法了,前提是你这个自动装配的属性在I0C (Spring) 容器中存在,且符合类型bytype!
科普

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

如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解[@Autowired] 完成的时候,我们可以使用@Qualifier(value="xxx")去配和@Autowired的使用,指定一个唯一的bean对象注入! 

@Autowired
    @Qualifier(value = "cat1")
    private Cat cat;
    @Autowired
    @Qualifier(value = "dog2")
    private Dog dog;
    private String name;