加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

spring开发_spring中Bean的作用域_singleton_prototype

发布时间:2020-12-15 01:52:31 所属栏目:大数据 来源:网络整理
导读:这里需要设置环境: 添加如下jar包 commons-logging.jar spring.jar com.b510.bean.dao; PrototypeBeanDao { prototype(); } com.b510.bean.dao; SingletonBeanDao { singleton(); } com.b510.bean; com.b510.bean.dao.PrototypeBeanDao; PrototypeBean Prot

这里需要设置环境:

添加如下jar包

commons-logging.jar

spring.jar

com.b510.bean.dao; PrototypeBeanDao { prototype(); }

com.b510.bean.dao; SingletonBeanDao { singleton(); }

com.b510.bean; com.b510.bean.dao.PrototypeBeanDao; PrototypeBean PrototypeBeanDao { prototype() { System.out .println("原型模式,每次通过容器的getBean方法获取prototype定义的Bean,都将产生一个新的Bean实例"); } }

com.b510.bean; com.b510.bean.dao.SingletonBeanDao; SingletonBean SingletonBeanDao { singleton() { System.out.println("单例模式,在整个spring IoC容器中,使用singleton定义Bean将只有一个实例"); } }

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http: ="com.b510.bean.SingletonBean"> ="com.b510.bean.PrototypeBean" scope="prototype">

/

com.b510.test; org.springframework.context.ApplicationContext; org.springframework.context.support.ClassPathXmlApplicationContext; com.b510.bean.dao.PrototypeBeanDao; com.b510.bean.dao.SingletonBeanDao; BeanTest { main(String[] args) { BeanTest().instanceContext(); } instanceContext() { ApplicationContext ctx = ClassPathXmlApplicationContext("beans.xml"); SingletonBeanDao singletonBeanDao = (SingletonBeanDao) ctx .getBean("single"); singletonBeanDao.singleton(); SingletonBeanDao singletonBeanDao1 = (SingletonBeanDao) ctx .getBean("single"); singletonBeanDao1.singleton(); System.out.print("singletonBeanDao与singletonBeanDao1是否是同一个:"); System.out.println(singletonBeanDao1==singletonBeanDao); PrototypeBeanDao prototypeBeanDao = (PrototypeBeanDao) ctx .getBean("proto"); prototypeBeanDao.prototype(); PrototypeBeanDao prototypeBeanDao1 = (PrototypeBeanDao) ctx .getBean("proto"); prototypeBeanDao1.prototype(); System.out.print("prototypeBeanDao与prototypeBeanDao1是否是同一个:"); System.out.println(prototypeBeanDao==prototypeBeanDao1); } }

2012-3-6 18:19:34 org.springframework.context.support.AbstractApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@c1b531: display name [org.springframework.context.support.ClassPathXmlApplicationContext@c1b531]; startup date [Tue Mar 06 18:19:34 CST 2012]; root of context hierarchy 2012-3-6 18:19:34 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from path resource [beans.xml] 2012-3-6 18:19:34 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory 信息: Bean factory application context [org.springframework.context.support.ClassPathXmlApplicationContext@c1b531]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1507fb2 2012-3-6 18:19:34 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1507fb2: defining beans [single,proto]; root of factory hierarchy 单例模式,在整个spring IoC容器中,使用singleton定义Bean将只有一个实例 单例模式,在整个spring IoC容器中,使用singleton定义Bean将只有一个实例 singletonBeanDao与singletonBeanDao1是否是同一个: 原型模式,每次通过容器的getBean方法获取prototype定义的Bean,都将产生一个新的Bean实例 原型模式,每次通过容器的getBean方法获取prototype定义的Bean,都将产生一个新的Bean实例 prototypeBeanDao与prototypeBeanDao1是否是同一个:

我们看到:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读