其他分享
首页 > 其他分享> > Spring-Bean的作用域

Spring-Bean的作用域

作者:互联网

1.在spring里面,设置创建bean实例是单实例还是多实例

 2.在spring里面,默认情况下是单实例对象

一、验证:默认情况下是单实例对象

(1)创建类

package com.company.spring5;

/**
 * @author orz
 * @create 2020-08-12 18:11
 */
public class Book {
    private String bname;
    private String bauthor;

    public void setBname(String bname) {
        this.bname = bname;
    }

    public void setBauthor(String bauthor) {
        this.bauthor = bauthor;
    }
    public void getBookInfo()
    {
        System.out.println(bname+"::"+bauthor);
    }
}

(2)配置文件

<?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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean name="book" class="com.company.spring5.Book" >
        <property name="bname" value="jiang"></property>
        <property name="bauthor" value="&lt;&lt;南京&gt;&gt;"></property>
    </bean>
           
</beans>

(3)测试

@Test
    public void booktest()
    {
        //1.加载Spring配置文件
        ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");

        //2.获取配置的创建对象
        Book book1 = context.getBean("book", Book.class);
        Book book2 = context.getBean("book", Book.class);

        //在spring里面,默认情况下是单实例对象
        System.out.println(book1);
        System.out.println(book2);
        //book.getBookInfo();


    }

(4)结果

com.company.spring5.Book@72cc7e6f
com.company.spring5.Book@72cc7e6f

二、如何设置多实例

在spring配置文件bean标签里面有scope属性用于设置单例还是多实例

Scope属性值

默认值:singleton表示单实例对象

 

第二个值:prototype表示多实例对象

(1)创建对象

package com.company.spring5;

/**
 * @author orz
 * @create 2020-08-12 18:11
 */
public class Book {
    private String bname;
    private String bauthor;

    public void setBname(String bname) {
        this.bname = bname;
    }

    public void setBauthor(String bauthor) {
        this.bauthor = bauthor;
    }
    public void getBookInfo()
    {
        System.out.println(bname+"::"+bauthor);
    }
}

(2)配置文件

<?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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean name="book2" class="com.company.spring5.Book" scope="prototype">
        <property name="bname" value="jiang"></property>
        <property name="bauthor" value="&lt;&lt;南京&gt;&gt;"></property>
    </bean>
</beans>

(3)测试

@Test
    public void test2()
    {
        //1.加载Spring配置文件
        ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");

        //2.获取配置的创建对象
        Book book1 = context.getBean("book2", Book.class);
        Book book2 = context.getBean("book2", Book.class);

        
        System.out.println(book1);
        System.out.println(book2);
        //book.getBookInfo();


    }

(4)结果

com.company.spring5.Book@7161d8d1
com.company.spring5.Book@74e28667

三、补充

Singleton和prototype 区别

一、singleton:单实例,prototype:多实例

二、设置scope值为singleton时,加载spring配置文件时就会创建对象

 

设置scope值为prototype时,不是在加载spring配置文件时创建对象,而是在调用getBean方法时创建多实例对象

<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置service
<bean> 配置需要创建的对象
id :用于之后从spring容器获得实例时使用的
class :需要创建实例的全限定类名
-->
<!--1.配置User对象创建-->

<!-- <bean id="user" class="com.company.spring5.User"></bean>
-->

<!-- 2.set方法注入属性 -->
<!-- <bean id="book" class="com.company.spring5.Book" >-->
<!--&lt;!&ndash; 使用property完成属性注入-->
<!-- name:类里面的属性名称-->
<!-- value:向属性注入的值-->
<!--&ndash;&gt;-->
<!-- <property name="bauthor" value="达摩"></property>-->
<!-- <property name="bname" value="易筋经"></property>-->
<!-- </bean>-->


<!-- 3.有参数构造注入属性-->
<!-- <bean id="order" class="com.company.spring5.Orders">-->
<!--&lt;!&ndash; <constructor-arg name="address" value="China"></constructor-arg>&ndash;&gt;-->
<!--&lt;!&ndash; <constructor-arg name="oname" value="abc"></constructor-arg>&ndash;&gt;-->

<!-- <constructor-arg index="0" value="Jack"></constructor-arg>-->
<!-- <constructor-arg index="1" value="China"></constructor-arg>-->
<!-- </bean>-->



<!-- 4.p名称空间注入-->
<!-- <bean name="book" class="com.company.spring5.Book" p:bauthor="金庸" p:bname="射雕英雄传"></bean>-->


<!--&lt;!&ndash; 5. null值&ndash;&gt;-->
<!-- <bean name="book" class="com.company.spring5.Book">-->
<!-- <property name="bname" value="降龙十八掌"></property>-->
<!-- <property name="bauthor">-->
<!-- <null/>-->
<!-- </property>-->
<!-- </bean>-->

<!-- 6.包含特殊字符
1.把<>进行转义
2.使用CDATA
-->
<!-- <bean name="book" class="com.company.spring5.Book" >-->
<!-- <property name="bauthor" value="金庸"></property>-->
<!-- <property name="bname">-->
<!-- <value><![CDATA[<<南京>>]]></value>-->
<!-- </property>-->
<!-- </bean>-->

<bean name="book" class="com.company.spring5.Book" >
<property name="bname" value="jiang"></property>
<property name="bauthor" value="&lt;&lt;南京&gt;&gt;"></property>
</bean>
<bean name="book2" class="com.company.spring5.Book" scope="prototype">
<property name="bname" value="jiang"></property>
<property name="bauthor" value="&lt;&lt;南京&gt;&gt;"></property>
</bean>
</beans>

标签:实例,作用域,Spring,bauthor,bname,--,Bean,Book,public
来源: https://www.cnblogs.com/orzjiangxiaoyu/p/13510401.html