Bean的作用域、Bean的自动装配

1.Bean的作用域

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

<bean id="user" class="com.yl.User" scope="singleton"/>

测试类

public class TestBeanEquals {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("user.xml");
        User user = context.getBean("user",User.class);
        User user1 = context.getBean("user", User.class);
        System.out.println(user == user1);
    }
}

输出: 

true

2.原型模式:每次从容器中get的时候都会产生一个新对象。

<bean id="user" class="com.yl.User" scope="prototype"/>

测试类输出:

false

3.其余的request、session、application这些只能在web开发中使用到

2、Bean的自动装配

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

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

在spring中有三种装配方式:

1.在xml中显示的配置

2.在java中显示配置

3.隐式的自动装配bean

1.测试

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

手动装配:

<?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-2.5.xsd">
    <bean id="cat" class="com.yl.Cat"/>
    <bean id="dog" class="com.yl.Dog"/>
    <bean id="people" class="com.yl.People">
        <property name="name" value="雨露"/>
        <property name="cat" ref="cat"/>
        <property name="dog" ref="dog"/>
    </bean>
</beans>

byName自动装配

byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的beanid

<?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-2.5.xsd">
    <bean id="cat" class="com.yl.Cat"/>
    <bean id="dog" class="com.yl.Dog"/>
    <bean id="people" class="com.yl.People" autowire="byName">
<!--        byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的beanid-->
        <property name="name" value="雨露"/>
    </bean>
</beans>

byType自动装配

<?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-2.5.xsd">
    <bean id="cat2" class="com.yl.Cat"/>
    <bean id="dog2" class="com.yl.Dog"/>
    <bean id="people" class="com.yl.People" autowire="byType">
<!-- byType:会自动在容器上下文中查找,和自己对象属性类型相同的beanid,必须保证类型全局唯一
-->
        <property name="name" value="雨露"/>
    </bean>
</beans>

小结:

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

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

使用注解实现自动装配:

使用注解须知:

1.导入约束,context约束,bean约束需要在context上面(在下面context:annotation-config爆红)

2.配置注解的支持context:annotation-config

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    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
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.2.xsd">
    <context:annotation-config>

</beans>

@Autowired

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

使用Autowired可以不用编写set方法,前提是自动装配的属性在IOC容器中存在,且符合名字byName

@Nullable   字段标记了这个注解,说明这个字段可以为null
public @interface Autowired {
    boolean required() default true;
}

测试代码

public class People {
    @Autowired
    private Cat cat;
    //如果定义了required属性为false,说明这个对象可以为null,否则不允许为空
    @Autowired(required = false)
    private Dog dog;

如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解@Autowired完成的时候,我们可以使用@Qualifier(value=“xxx”)去配置@Autowired的使用,制定一个唯一的bean对象注入(简言之@Autowired默认bytype装配,没有匹配时用byname装配,@Qualifier使得@Autowired通过byName装配)

@Resource

    @Resource(name = "cat22")
    private Cat cat;

@Resource不加括号默认先byName,再byType查找,当名字或者类型唯一时运行

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值