spring实例化bean之简单工厂静态方法实例化
2013-08-21 22:18
639 查看
PersonDao
public interface PersonDao {
}
PersonDaoImpl
public class PersonDaoImpl implements PersonDao{
public PersonDaoImpl(){
System.out.println("spring通过构造方法来实例化对象");
}
}
DaoFactory
public class DaoFactory {
//利用工厂类的static方法来创建dao实例
public static 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 -->
<bean id="personDao" class="com.xxc.initBean.two.factory.DaoFactory" factory-method="createPersonDao"></bean>
</beans>
测试类
public class App {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("com/xxc/initBean/two/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 {
//利用工厂类的static方法来创建dao实例
public static 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 -->
<bean id="personDao" class="com.xxc.initBean.two.factory.DaoFactory" factory-method="createPersonDao"></bean>
</beans>
测试类
public class App {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("com/xxc/initBean/two/applicationContext.xml");
PersonDao personDao = (PersonDao) ac.getBean("personDao");
}
}
相关文章推荐
- spring实例化bean之简单工厂实例方法实例化
- 从spring容器中得到bean_使用类构造器实例化_使用静态工厂方法实例化_使用实例工厂方法实例化_例子
- Spring使用实例工厂方法实例化Bean
- spring中通过静态工厂方法获取bean实例化对象
- Spring4 学习系列之——bean的静态工厂方法 和 实例方法
- [原创]java WEB学习笔记102:Spring学习---Spring Bean配置:bean配置方式(工厂方法(静态工厂方法 & 实例工厂方法)、FactoryBean) 全类名
- Spring学习--静态工厂方法、实例工厂方法创建 Bean
- Spring使用教程(二)配置bean:静态工厂方法和实例工厂方法
- spring创建bean的三种方式-通过构造器,通过静态工厂方法,通过实例工厂方法
- Spring工厂方法创建(实例化)bean实例代码
- Spring学习之使用静态工厂方法以及实例工厂方法配置bean
- spring 9 bean配置--通过工厂方法(静态工厂方法&实例工厂方法
- spring学习小结7_使用工厂方法来配置bean:静态工厂方法和实例工厂方法
- Spring 使用实例工厂方法实例化Bean
- spring实例化bean之构造方法实例化
- spring Bean使用案例 静态方法方式 和工厂模式
- Spring使用静态工厂方法创建Bean(一)
- 静态工厂方法和实例工厂方法注入bean
- Spring使用静态工厂方法创建Bean
- Spring学习笔记九--静态工厂方法和实例工厂方法