Spring 5 Core Technologies Leaning:(二)singleton bean

参考:https://docs.spring.io/spring/docs/5.0.5.RELEASE/spring-framework-reference/core.html#beans-factory-scopes

对于spring中单例模型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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="hello" class="com.tutorialspoint.HelloWorld" depends-on="application">
        <property name="message" value="XmlMessage"/>
    </bean>
    <bean id="application" class="com.tutorialspoint.Application" autowire="byName">
    </bean>
    
</beans>
public static void main(String[] args)
	{		
		ApplicationContext context=new ClassPathXmlApplicationContext("Beans.xml");
		Application app1=context.getBean("application",Application.class);
		Application app2=context.getBean("application",Application.class);
		System.out.println(app1==app2);
	}

输出的结果:true

在Beans.xml中添加一行: <bean id="application1" class="com.tutorialspoint.Application" autowire="byName"></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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="hello" class="com.tutorialspoint.HelloWorld" depends-on="application">
        <property name="message" value="XmlMessage"/>
    </bean>
    <bean id="application" class="com.tutorialspoint.Application" autowire="byName">
    </bean>
    <bean id="application1" class="com.tutorialspoint.Application" autowire="byName">
    </bean>
</beans>

在main函数中将app2的引用修改成:context.getBean("application1",Application.class)

public static void main(String[] args)
	{		
		ApplicationContext context=new ClassPathXmlApplicationContext("Beans.xml");
		Application app1=context.getBean("application",Application.class);
		Application app2=context.getBean("application1",Application.class);
		System.out.println(app1==app2);
	}

运行结果:false

结论:从上面的结果可以看出,spring在创建ClassPathApplicationContext对象就初始化了容器,并且容器中的内容是通过xml文件创建单例的Bean,通过一一对应的id引用容器中的对象。

假设上面的结论是正确的:那如果创建两个ClassPathApplicationContext对象,对同一个id就应该得到不同的引用。

public static void main(String[] args)
	{		
		ApplicationContext context1=new ClassPathXmlApplicationContext("Beans.xml");
		ApplicationContext context2=new ClassPathXmlApplicationContext("Beans.xml");
		Application app1=context1.getBean("application",Application.class);
		Application app2=context2.getBean("application",Application.class);
		System.out.println(app1==app2);
	}

结果:flase

说明假设正确。及单例的Bean的生命周期取决于创建容器的对象的生命周期。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值