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);
     
  • 相关阅读:
    操作系统进程
    Lowest Common Ancestor of a Binary Search Tree
    Java并发编程实践之对象的组合
    字典序排序-求全排列(元素有重复)
    计算机网络基础知识
    多线程的基础知识
    多线程编程题
    Flask安装
    appium使用实例
    调用Excel或Oracle数据,数据加载,selenium等使用实例
  • 原文地址:https://www.cnblogs.com/chengfengchi/p/14420094.html
Copyright © 2011-2022 走看看