Spring - Bean管理之三种实例化Bean的方式(XML)
发布日期:2021-06-30 23:46:55
浏览次数:2
分类:技术文章
本文共 1695 字,大约阅读时间需要 5 分钟。
package com.imooc.ioc.demo2;/** * Bean的实例化的三种方式:采用无参数的构造方法的方式 */public class Bean1 { public Bean1(){ System.out.println("Bean1被实例化了..."); }}
package com.imooc.ioc.demo2;/** * Bean的实例化三种方式:静态工厂实例化方式 */public class Bean2 {}package com.imooc.ioc.demo2;/** * Bean2的静态工厂 */public class Bean2Factory { public static Bean2 createBean2(){ System.out.println("Bean2Factory的方法已经执行了..."); return new Bean2(); }}
package com.imooc.ioc.demo2;/** * Bean的实例化三种方式:实例工厂实例化 */public class Bean3 {}package com.imooc.ioc.demo2;/** * Bean3的实例工厂 */public class Bean3Factory { public Bean3 createBean3(){ System.out.println("Bean3Factory执行了..."); return new Bean3(); }}
package com.imooc.ioc.demo2;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * Bean的实例化的三种方式 */public class SpringDemo2 { @Test public void demo1(){ // 创建工厂 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); // 通过工厂获得类的实例: Bean1 bean1 = (Bean1)applicationContext.getBean("bean1"); } @Test public void demo2(){ // 创建工厂 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); // 通过工厂获得类的实例: Bean2 bean2 = (Bean2)applicationContext.getBean("bean2"); } @Test public void demo3(){ // 创建工厂 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); // 通过工厂获得类的实例: Bean3 bean3 = (Bean3)applicationContext.getBean("bean3"); }}
转载地址:https://lux-sun.blog.csdn.net/article/details/84108713 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
网站不错 人气很旺了 加油
[***.192.178.218]2024年04月22日 07时02分32秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
软件质量的8个特性
2019-04-30
应届渣渣前端的艰难求职之路
2019-04-30
2021年不可错过的17种JS优化技巧(一)
2019-04-30
月薪15~20k的前端面试问什么?
2019-04-30
在 Vue 中用 Axios 异步请求API
2019-04-30
MySQL进阶查询(SELECT 语句高级用法)
2019-04-30
Mysql 之主从复制
2019-04-30
【工具使用】新版CSDN-markdown编辑器使用指南
2019-04-30
【NLP学习笔记】中文分词(Word Segmentation,WS)
2019-04-30
【超越白皮书7】你需要知道关于ETH2.0的几个事实
2019-04-30
对于时间复杂度的通俗理解
2019-04-30
如何输入多组数据并输出每组数据的和?
2019-04-30
行阶梯型矩阵
2019-04-30
MATLAB指定路径保存图片方法
2019-04-30
JAVA学习笔记6 - 数组
2019-04-30
JAVA学习笔记10 - 继承
2019-04-30
Android 开发学习笔记 00 - Getting Started
2019-04-30
【学习笔记】Android Activity
2019-04-30
Android使用Retrofit_00_Getting Started
2019-04-30
Android使用Retrofit_01_OAuth2 + GitHub
2019-04-30