• Spring:Spring-IOC实例化bean的常用三种方式


    Spring容器提供了三种对bean的实例化方式:


    1)构造器实例化

    public class Demo {
        private String name;
        //getter和setter方法略
    }
    <?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="demo1" class="com.oak.junit.day01.Demo"></bean>
    </beans>

    测试:

    @Test
        public void testCtx() {
            //实例化Spring容器
            ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml"); 
            //取出demo1
            Demo demo1=ctx.getBean("demo1",Demo.class);
            System.out.println(demo1);
        }

    2)静态工厂方法实例化

    public class DemoBeanFactory {
        //静态工厂方法-返回一个Demo实例
        public static Demo staticDemoBean(){
            return new Demo();
        }
    }
    <?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="demo1" class="com.oak.junit.day01.Demo"></bean>
        
        <!-- 静态工厂实例化 class为工厂类 factory-method为静态工厂方法  -->
        <bean id="demo2" class="com.oak.junit.day01.DemoBeanFactory" 
        factory-method="staticDemoBean"></bean>
    </beans>

    测试:

        @Test
        public void testCtx() {
            //实例化Spring容器
            ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml"); 
            //取出demo1
            Demo demo1=ctx.getBean("demo1",Demo.class);
            System.out.println(demo1);
            
            //取出demo2
            Demo demo2=ctx.getBean("demo2",Demo.class);
            System.out.println(demo2);
        }

    3)实例工厂方法是实例化

    工厂类中加入实例工厂方法:

    public class Demo {
        private String name;
        //getter和setter方法略
    }
    public class DemoBeanFactory {
        //静态工厂方法-返回一个Demo实例
        public static Demo staticDemoBean(){
            return new Demo();
        }
        //实例工厂方法-返回一个Demo实例
        public Demo constructorDemoBean(){
            return new Demo();
        }
    }
    <?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="demo1" class="com.oak.junit.day01.Demo"></bean>
        
        <!-- 静态工厂实例化 class为工厂类 factory-method为静态工厂方法  -->
        <bean id="demo2" class="com.oak.junit.day01.DemoBeanFactory" 
        factory-method="staticDemoBean"></bean>
        
        <!-- 先实例化工厂类 -->
        <bean id="demoBeanFactory" class="com.oak.junit.day01.DemoBeanFactory"></bean>
        <!-- 实例工厂实例化 class为工厂类 factory-bean指定工厂类  factory-method为实例工厂方法  -->
        <bean id="demo3" factory-bean="demoBeanFactory" 
        factory-method="constructorDemoBean"></bean>
    </beans>

    测试:

    @Test
            public void testCtx() {
                //实例化Spring容器
                ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml"); 
                //取出demo1
                Demo demo1=ctx.getBean("demo1",Demo.class);
                System.out.println(demo1);
                
                //取出demo2
                Demo demo2=ctx.getBean("demo2",Demo.class);
                System.out.println(demo2);
                
                //取出demo2
                Demo demo3=ctx.getBean("demo3",Demo.class);
                System.out.println(demo3);
            }

    文章转载至:https://blog.csdn.net/qq_34598667/article/details/83246492

  • 相关阅读:
    python 面向对象之路(1) 类和对象 三大特性 封装 继承 多态
    python 模块之路 异常处理基础 异常处理其他 报错处理
    python 模块之路 序列化 json pickle
    python 模块之路 random模块 时间模块 sys模块 os模块
    python 模块之路 re模块运用
    python -模块之路 正则表达式
    【Unity】粒子特效优化实践
    快速实现Low Poly地形
    【Unity】屏幕空间位置变换到世界空间位置的方法
    Substance Designer 使用手册
  • 原文地址:https://www.cnblogs.com/nhdlb/p/12427276.html
Copyright © 2020-2023  润新知