PersonDao
public interface PersonDao {
}
PersonDaoImpl
public class PersonDaoImpl implements PersonDao{
public PersonDaoImpl(){
System.out.println("spring通过构造方法来实例化对象");
}
}
DaoFactory
public class DaoFactory {
public DaoFactory(){
System.out.println("工厂类实例化");
}
public PersonDao createPersonDao(){
return new PersonDaoImpl();
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- spring来实例化工厂类 -->
<bean id="beanFactory" class="com.xxc.initBean.three.factory.DaoFactory"></bean>
<!-- spring用已经实例化好的工厂类的指定方法实例化dao -->
<bean id="personDao" factory-bean="beanFactory" factory-method="createPersonDao"></bean>
</beans>
public class App {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("com/xxc/initBean/three/applicationContext.xml");
PersonDao personDao = (PersonDao)ac.getBean("personDao");
}
}