spring学习笔记整理--03(Spring的三种实例化Bean的方式)
2011-02-06 16:06
786 查看
Spring有三种实例化Bean的方式
一.构造器注入实例化
前面一个帖子的实现就是用构造器实现,也是最普遍用到的一种,在此不过多的描述。
<bean id="personService" class="cn.service.impl.PersonServiceImpl"></bean>
二.使用静态工厂方法实例化
今天学的不多,偷个懒!
一.构造器注入实例化
前面一个帖子的实现就是用构造器实现,也是最普遍用到的一种,在此不过多的描述。
<bean id="personService" class="cn.service.impl.PersonServiceImpl"></bean>
二.使用静态工厂方法实例化
三.使用实例工厂方法实例化 PersonServiceFactory : package cn.service.impl; public class PersonServiceFactory { public PersonServiceImpl createPersonServiceImpl(){ return new PersonServiceImpl(); } } ------------------------------------------------------------------------- <?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"> <bean id="personServiceFactory" class="cn.service.impl.PersonServiceFactory"></bean> <bean id="personService" factory-bean="personServiceFactory" factory-method="createPersonServiceImpl"></bean> </beans> ------------------------------------------------------------------------- package junit.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import commons.tz.TzClassPathXmlApplicationContext; import cn.service.PersonService; public class Spring02Test { @Test public void instanceSping() { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); PersonService personService = (PersonService) ctx.getBean("personService"); personService.save(); } }
今天学的不多,偷个懒!
相关文章推荐
- Spring学习笔记(二)Spring的三种实例化Bean的方式
- spring中三种实例化bean的方式
- spring基础----->实例化bean的三种方式
- spring创建bean的三种方式-通过构造器,通过静态工厂方法,通过实例工厂方法
- Spring实例化Bean的三种方式
- 传智播客Spring2.5视频教程_Spring的三种实例化Bean的方式 2
- ITCAST视频-Spring学习笔记(Spring的三种实例化Bean的方式)
- spring实例化bean的三种方式
- Spring入门之实例化Bean的三种方式
- Spring的三种实例化Bean的方式
- Spring实例化Bean的三种方式及Bean的类型(activiti实战)
- Spring第四弹—–Spring的三种实例化bean的方式
- spring的bean管理之bean实例化三种方式
- Spring的三种实例化Bean的方式
- Spring实例化Bean的三种方式
- spring实例化bean的三种方式
- Spring学习笔记——Spring实例化Bean的三种方式
- spring中三种实例化bean的方式
- spring2.5.6学习笔记五:Spring的三种实例化Bean的方式
- Spring中bean的三种实例化方式的简单实例