• 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);
     
  • 相关阅读:
    RedisDump安装报错
    安装mysql解压版时遇到的错误
    Docker 私有仓库 Harbor搭建与使用
    最好的6个Go语言Web框架
    安裝 drone CLI和使用drone
    使用 Kubernetes Helm 安装 Drone
    从ELK到EFK演进
    搭建helm私服ChartMuseum
    Helm3的使用
    Helm3部署安装
  • 原文地址:https://www.cnblogs.com/chengfengchi/p/14420094.html
Copyright © 2020-2023  润新知