zoukankan      html  css  js  c++  java
  • spring 创建三种创建对象的方法

    spring 创建三种创建对象的方法

    User类

     
     
     
    x
     
     
     
     
    public class User {
        private String name;
        private Integer age;
        private TestObj testObj;
        public TestObj getTestObj(){
            return new TestObj();
        }
    }
     

    getTestObj类

     
     
     
     
     
     
     
     
    public class TestObj {
        private String Test;
    }
     

    xml配置

     
     
     
    xxxxxxxxxx
     
     
     
     
    <!--使用默认方法构建-->
    <bean id="user" class="com.test.User"/>
    <!--使用工厂内的普通方法创建-->
    <bean id="testObj" factory-bean="user" factory-method="getTestObj"/>
    <!--通过静态方法-->
    <bean id="staticGetTestObj" class="com.chexd.User" factory-method="getStaticTestObj"/>
     

    @Test(比如用第三方的jar包 放到spring工厂内)

     
     
     
    x
     
     
     
     
            ApplicationContext context = new ClassPathXmlApplicationContext( "spring.xml" );
            TestObj testObj = context.getBean( "testObj", TestObj.class );
            System.out.println(testObj);
            TestObj testStaticObj = context.getBean( "staticGetTestObj", TestObj.class );
            System.out.println(testStaticObj);
     
  • 相关阅读:
    移动端疫情展示
    第五周学习进度
    第四周学习进度
    结队开发-四则运算
    第三周学习进度
    全球疫情可视化第一阶段
    第二周学习进度
    面试题 02.07. 链表相交 做题小结
    剑指 Offer 35. 复杂链表的复制 做题小结
    LeetCode 452. 用最少数量的箭引爆气球 做题小结
  • 原文地址:https://www.cnblogs.com/chengfengchi/p/14420094.html
Copyright © 2011-2022 走看看