在spring中,对Bean的管理主要有3种方式,分别是使用BeanWrapper管理Bean,使用BeanFactory管理Bean,和使用ApplicationContext管理Bean。 A:使用BeanWrapper管理Bean: 在org.springframework.beans包中,还有两个非常重要的类:BeanWrapper接口及它的实现BeanWrapperImpl。BeanWrapper封装了一个Bean的行为。提供了设置和获得属性值的功能。通过BeanWrapper可以获取Bean的属性描述,查询只读或者可写属性。 B:使用BeanFactory管理Bean BeanFactory实际上是实例化,配置和管理众多Bean的容器,这些Bean通常会彼此合作,因而它们之间会产生依赖。 一个BeanFactory可以用接口org.springframework.beans.factory.BeanFactory表示,这个接口有多个实现。最常用的简单的BeanFactory实现是org.springframework.beans.factory.xml.XmlBeanFactory。 C:使用ApplicationContext管理Bean ApplicationContext建立在BeanFactory之上,并增加了其他功能,例如:对国际化的支持,获取资源,事件传递等。BeanFactory提供了配置框架和基本功能,而ApplicationContext为它增加了更强的功能。一般来说,ApplicationContext是BeanFactory的完全超集,任何BeanFactory功能同样也适用于ApplicationContext。 3种管理Bean的方式的比较: 3种管理Bean的方式实现了同样的功能。只是对于BeanWrapper来说,其实没有用到Spring的配置文件,而且只能对单个Bean进行设定,所以一般来说,在实际编程中,并不适用。 常用的是BeanFactory和ApplicationContext,因为目前使用spring主要是进行B/S结构的编程,而且可以把ApplicationContext看做是BeanFactory的超集,所以ApplicationContext更常用,而且提供的功能更多。 ====================练习一:BeanWrapper管理Bean============ ----------------------------HelloWord 类--------------------- package com.gc.action; import java.util.Date; public class HelloWord { private String msg; private Date date; public HelloWord() { super(); } public HelloWord(String msg, Date date) { super(); this.msg = msg; this.date = date; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } } --------------------------------TestHelloWord 测试类------------------ package com.gc.test; import java.util.Date; import org.springframework.beans.BeanWrapper; import org.springframework.beans.BeanWrapperImpl; public class TestHelloWord { public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException { Object obj = Class.forName("com.gc.action.HelloWord").newInstance(); BeanWrapper bw =new BeanWrapperImpl(obj); bw.setPropertyValue("msg","aaa"); bw.setPropertyValue("date", new Date()); System.out.println(bw.getPropertyValue("msg")+"---"+bw.getPropertyValue("date")); } } -------------------结果------------- aaa---Tue Nov 29 16:41:19 CST 2016 ======================练习二 使用BeanFactory管理Bean================ ----------------HelloWord类------------------ package com.gc.action; import java.util.Date; public class HelloWord { private String msg; private Date date; public HelloWord() { super(); } public HelloWord(String msg, Date date) { super(); this.msg = msg; this.date = date; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } } -------------------config.xml------------------------- <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www./dtd/spring-beans.dtd"> <beans> <bean id="HelloWord" class="com.gc.action.HelloWord" depends-on="date"> <property name="msg"> <value>zx</value> </property> <property name="date"> <ref bean="date"/> </property> </bean> <bean id="date" class="java.util.Date"></bean> </beans> ----------------------------TestHelloWord 测试类-------------------- package com.gc.test; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import com.gc.action.HelloWord; public class TestHelloWord { public static void main(String[] args) { ClassPathResource res = new ClassPathResource("/config.xml");//注意此处是绝对路径 XmlBeanFactory factory =new XmlBeanFactory(res); HelloWord hw =(HelloWord)factory.getBean("HelloWord"); System.out.println(hw.getMsg()+"---"+hw.getDate()); } } ------------结果------------- zx---Tue Nov 29 17:28:00 CST 2016 ==================练习三:使用ApplicationContext管理Bean========== ------------------HelloWord 类------------------ package com.gc.action; import java.util.Date; public class HelloWord { private String msg; private Date date; public HelloWord() { super(); } public HelloWord(String msg, Date date) { super(); this.msg = msg; this.date = date; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } } -------------------------config.xml------------------------ <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www./dtd/spring-beans.dtd"> <beans> <bean id="HelloWord" class="com.gc.action.HelloWord" depends-on="date"> <property name="msg"> <value>zx</value> </property> <property name="date"> <ref bean="date"/> </property> </bean> <bean id="date" class="java.util.Date"></bean> </beans> ----------------------------TestHelloWord 测试类------------------------ package com.gc.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import com.gc.action.HelloWord; public class TestHelloWord { public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException { ApplicationContext ac =new FileSystemXmlApplicationContext("classpath:/config.xml"); HelloWord hw =(HelloWord) ac.getBean("HelloWord"); System.out.println(hw.getMsg()+"----"+hw.getDate()); } } --------------------结果----------------- zx----Tue Nov 29 17:53:35 CST 2016 |
|
来自: 樱花梦_张艺馨 > 《Spring从入门到精通》