基础代码
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
People people = applicationContext.getBean("peo", People.class);
System.out.println(people);
- 构造方法(默认无参构造,可以有参构造)
无参构造
<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>
- 实例工厂
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>
- 静态工厂
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>