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);
     
  • 相关阅读:
    iOS MVC、MVVM、MVP详解
    消息队列(mq)是什么?
    使用 tail 结合 grep 查找日志关键字并高亮及显示所在行上下文
    Linux下实现高可用软件-Keepalived基础知识梳理
    手动编译部署LNMP环境(CentOS7.5+Nginx-1.18.0+MySQL-5.7.30+PHP-7.4.14)
    linux 查看硬盘使用情况
    解决vue3.0新建项目无法选中Manually select features
    运行vue项目时报错:You may use special comments to disable some warnings. Use // eslint-disable-next-line to ignore the next line. Use /* eslint-disable */ to ignore all warnings in a file.
    git pull 报错remote: HTTP Basic: Access denied fatal: Authentication failed for 的解决方法
    Excel文档导出——后端返回文件流,前端实现下载功能
  • 原文地址:https://www.cnblogs.com/chengfengchi/p/14420094.html
Copyright © 2011-2022 走看看