码迷,mamicode.com
首页 > 编程语言 > 详细

spring创建工厂的三种方式

时间:2020-02-29 00:57:05      阅读:72      评论:0      收藏:0      [点我收藏+]

标签:bean   sys   方法   return   set   cat   struct   str   code   

基础代码
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        People people = applicationContext.getBean("peo", People.class);
        System.out.println(people);
  1. 构造方法(默认无参构造,可以有参构造)
无参构造
<bean id="peo" class="com.run.People"></bean>
有参构造
    <bean id="peo" class="com.run.People">
        <constructor-arg name="name" value="ych"></constructor-arg>
        <constructor-arg name="age" value="30"></constructor-arg>
    </bean>
  1. 实例工厂
public class PeopleFactory {
    public People getPeople() {
        return new People();
    }
}

    <bean id="fac1" class="com.run.PeopleFactory"></bean>
    <bean id="peo" factory-bean="fac1" factory-method="getPeople"></bean>
  1. 静态工厂
public class PeopleFactory {
    public static People getPeople1() {
        People people = new People();
        people.setName("ych");
        return people;
    }
}

<bean id="peo" class="com.run.PeopleFactory" factory-method="getPeople1"></bean>

spring创建工厂的三种方式

标签:bean   sys   方法   return   set   cat   struct   str   code   

原文地址:https://www.cnblogs.com/yinchh/p/12381097.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!