SPING之bean作用域

来源:互联网 发布:程序员个人评价 编辑:程序博客网 时间:2024/04/30 08:10

先在Application里定义两个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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="time1" class="java.util.Date"/>
<bean id="time2" class="java.util.Date" scope="prototype"/>

</beans>



设计Test测试

public class TestSpring {
public static void main(String args[]){
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("Spring容器中的作用域");
System.out.println("单例模式singleton的应用显示");
System.out.println(ctx.getBean("time1")==ctx.getBean(("time1")));
System.out.println(ctx.getBean("time1"));
System.out.println(">-------------------");
System.out.println("双例模式singleton的应用显示");
System.out.println(ctx.getBean("time2")==ctx.getBean(("time2")));
System.out.println(ctx.getBean("time2"));
}
}

ok!,这例子加深了对spring的理解。谢谢



0 0
原创粉丝点击