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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Spring - Bean管理之配置(XML)
下一篇:Spring - IOC(控制反转) & DI(依赖注入)

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月22日 07时02分32秒