spring实例化bean之简单工厂实例方法实例化
2013-08-21 22:25
639 查看
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>
测试类:spring先实例化了工厂类,然后调用工厂类中的方法实例化了dao
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");
}
}
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>
测试类:spring先实例化了工厂类,然后调用工厂类中的方法实例化了dao
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");
}
}
相关文章推荐
- spring实例化bean之简单工厂静态方法实例化
- Spring 使用实例工厂方法实例化Bean
- 从spring容器中得到bean_使用类构造器实例化_使用静态工厂方法实例化_使用实例工厂方法实例化_例子
- Spring使用实例工厂方法实例化Bean
- Spring工厂方法创建(实例化)bean实例代码
- Spring学习之使用静态工厂方法以及实例工厂方法配置bean
- spring创建bean的三种方式-通过构造器,通过静态工厂方法,通过实例工厂方法
- Spring使用教程(二)配置bean:静态工厂方法和实例工厂方法
- [原创]java WEB学习笔记102:Spring学习---Spring Bean配置:bean配置方式(工厂方法(静态工厂方法 & 实例工厂方法)、FactoryBean) 全类名
- spring笔记10---实例工厂方法配置bean
- Spring学习--静态工厂方法、实例工厂方法创建 Bean
- spring中通过静态工厂方法获取bean实例化对象
- Spring中bean的三种实例化方式的简单实例
- spring实例化bean之构造方法实例化
- spring学习小结7_使用工厂方法来配置bean:静态工厂方法和实例工厂方法
- 创建Bean的三种方式 在大多数情况下,Spring容器直接通过new关键字调用构造器来创建Bean实例,而class属性指定Bean实例的实现类,但这不是实例化Bean的唯一方法。实际上,Sprin
- Spring入门学习——调用一个实例工厂方法创建Bean
- Spring中通过工厂方法获取bean实例
- 【spring NOTE】调用实例工厂方法创建Bean
- Spring4 学习系列之——bean的静态工厂方法 和 实例方法