Spring 设置Bean的作用域及懒加载

本文介绍了如何在Spring中配置Bean的作用域,包括singleton单例和prototype多例模式,并通过XML和注解两种方式展示了设置过程。此外,还详细讲解了singleton模式下的懒加载配置及其效果。

一、准备工作

1、导入spring-context依赖

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.2.22.RELEASE</version>
</dependency>

2、创建实体类Person

public class Person {

    private int age;
    private String name;

    public Person() {
        System.out.println("创建Person完成");
    }

    public Person(int age, String name) {
        this();
        this.age = age;
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "Person{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
    
}

二、scope参数

1、通过xml配置

<?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">

    <!--  scope:singleton单例模式,在spring初始化时创建,只会存在一个实例,默认值  -->
    <!--  scope:prototype多例模式,每次获取bean时创建,存在多个实例  -->
    <bean id="person" class="Person" scope="singleton">
        <property name="age" value="20"/>
        <property name="name" value="zhangsan"/>
    </bean>

</beans>
    public static void main(String[] args) {
        testXml();
    }

    public static void testXml(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Person p1 = (Person) context.getBean("person");
        Person p2 = (Person) context.getBean("person");
        System.out.println(p1==p2);
        // scope:singleton,单例模式下,结果为true
        // scope:prototype,多例模式下,结果为false
    }

2、通过注解配置

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration
public class BeansConfig {

    /*
    *  @Scope("singleton"),单例模式,默认值
    *  @Scope("prototype"),多例模式
    * */
    @Scope("singleton")
    @Bean
    public Person person(){
        return new Person(20,"zhangsan");
    }

}
public static void main(String[] args) {
    testAnnotation();
}

public static void testAnnotation(){
    ApplicationContext context = new AnnotationConfigApplicationContext(BeansConfig.class);
    Person p1 = (Person) context.getBean("person");
    Person p2 = (Person) context.getBean("person");
    System.out.println(p1==p2);
    // @Scope("singleton"),单例模式下,结果为true
    // @Scope("prototype"),多例模式下,结果为false
}

三、scope:singleton模式下设置懒加载

1、通过xml配置

<?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">

    <!-- lazy-init="true" 表示开启懒加载 -->
    <bean id="person" class="Person" lazy-init="true" >
        <property name="age" value="20"/>
        <property name="name" value="zhangsan"/>
    </bean>

</beans>
    public static void main(String[] args) {
        testXml();
    }

    public static void testXml(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        System.out.println("创建Spring完成");
        Person person = (Person) context.getBean("person");
        /**
         * 未开启懒加载,打印顺序:
         * 创建Person完成
         * 创建Spring完成
         *
         * 开启懒加载,打印顺序:
         * 创建Spring完成
         * 创建Person完成
         */
    }

2、通过注解配置

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

@Configuration
public class BeansConfig {

    @Lazy
    @Bean
    public Person person(){
        return new Person(20,"zhangsan");
    }

}
    public static void main(String[] args) {
        testAnnotation();
    }

    public static void testAnnotation(){
                ApplicationContext context = new AnnotationConfigApplicationContext(BeansConfig.class);
        System.out.println("创建Spring完成");
        Person person = (Person) context.getBean("person");
        /**
         * 未开启懒加载,打印顺序:
         * 创建Person对象
         * 创建Spring完成
         *
         * 开启懒加载,打印顺序:
         * 创建Spring完成
         * 创建Person对象
         */
    }

四、总结分析

本章主要学习了,如何设置Spring Bean作用域及懒加载。《参考资料》

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值