1、Bean Wrapper
q个BeanWrappercd单,提供了一套Bean的操作方法,如apache -BeanUtils一栗通过BeanWrapperQ我们可以无需在编码时指定JavaBean的实现类和属性|通过在配|文?br />加以讑֮Q就可以在运行期动态创建对象ƈ讑֮其属性(依赖关系Q?一个简单的例子Q?br /> public static void testBeanWrapper(){
try {
Object obj=Class.forName("com.spring.UpperAction").newInstance();
BeanWrapper bw=new BeanWrapperImpl(obj);
bw.setPropertyValue("message","peidw");
System.out.println(bw.getPropertyValue("message"));
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
2、Bean Factory
BeanFactory负责Ҏ配置配置文g创徏Bean实例Q可以配|的目有:
1 、Bean属性值及依赖关系
2、Bean创徏模式(是否单态模?
3、Bean初始化及销?br /> 4、Bean的依赖关p?br /><beans>
<description>Spring Bean Configuration Sample</description>
<bean
id="TheAction" ?br />class="net.xiaxin.spring.qs.UpperAction" ?br />singleton="true" ?br />init-method="init" ?br />destroy-method="cleanup" ?br />depends-on="ActionManager" ?br />>
<property name="message">
<value>HeLLo</value> ?br /></property>
<property name="desc">
<null/>
</property>
<property name="dataSource">
<ref local="dataSource"/> ?br /></property>
</bean>
<bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/sample</value>
</property>
</bean>
q是一个比较完整的Bean配置Qid表示一个类在BeanFactory中的唯一标识
class 是javacdQsingleton 为true表示使用单态,init-method初始化方法在BeanFactory创徏Bean后执行的初始化方法。destroy-method销毁方法,在bean销毁时执行Q一般用于资源释放。depends-on Bean的依懒关pR?lt;value>节点用于指定一个属性|ref节点用于引用另一个bean的属性?br />
联合上面关于BeanWrapper的内容,我们可以看到QBeanWrapper实现了针对单个Bean的属性设定操作。而BeanFactory则是针对多个Bean的管理容器,Ҏl定的配|文ӞBeanFactory从中dcd、属性名/|然后通过Reflection机制q行Bean加蝲和属性设定?br />
3、ApplicationContext
BeanFactory提供了针对Java Bean的管理功能,而ApplicationContext提供了一个更为框架化?br />实现Q从上面的示例中可以看出QBeanFactory的用方式更加类g个APIQ而非Framework styleQ?br />ApplicationContext覆盖了BeanFactory的所有功能,q提供了更多的特性。此外,
ApplicationContextZ现有应用框架相整合,提供了更为开攑ּ的实玎ͼ如对于Web应用Q我们可以在
web.xml中对ApplicationContextq行配置Q?br />相对BeanFactory而言QApplicationContext提供了以下扩展功能:
1Q?国际化支?br />我们可以在Beans.xml文g中,对程序中的语a信息Q如提示信息Q进行定义,程序中的提C?br />信息抽取到配|文件中加以定义Qؓ我们q行应用的各语言版本转换提供了极大的灉|性?br />2Q?资源讉K
支持Ҏ件和URL的访问?br />3Q?事g传播
事g传播Ҏؓpȝ中状态改变时的检提供了良好支持?br />4Q?多实例加?br />可以在同一个应用中加蝲多个Context实例?br />
一个国际化支持例子
public static void testLanguage(){
ApplicationContext ac=new FileSystemXmlApplicationContext("src/bean.xml");
Object[] arg=new Object[]{"kkui",Calendar.getInstance().getTime()};
//以系l默认的Locale加蝲信息Q对winxp而言默认的是zh_CNQ?/span>
String zhmsg=ac.getMessage("userinfo",arg, Locale.CHINA);
String usmsg=ac.getMessage("userinfo",arg, Locale.US);
System.out.println(zhmsg);
System.out.println(usmsg);
}
需要在beans.xml文g配置使用多资?br /> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>messages</value>
</list>
</property>
</bean>
q里声明了一个名为messageSource的BeanQ注意对于Message定义QBean ID必须?br />messageSourceQ这是目前Spring的编码规U)Q对应类为ResourceBundleMessageSourceQ?br />目前Spring中提供了两个MessageSource接口的实玎ͼ?br />ResourceBundleMessageSource和ReloadableResourceBundleMessageSourceQ后
者提供了无需重启卛_重新加蝲配置信息的特性?br />资源文gmessage_zh.properties
userinfo=登陆用户:[{0}] 登陆旉[{1}]
国际化支持在实际开发中可能是最常用的特性。对于一个需要支持不同语a环境的应用而言Q我们所采取的最常用的策略一般是通过一个独立的资源文gQ如一个properties文gQ完成所有语a信息Q如界面上的提示信息Q的配置QSpring对这U传l的方式q行了封装,q提供了更加强大的功?
资源讉K例子
Resource rs = ctx.getResource("classpath:config.properties");
File file = rs.getFile();
事g传播机制例子
ApplicationContextZObserver模式Qjava.util包中有对应实玎ͼQ提供了针对Bean的事件传播功能。通过Application. publishEventҎQ我们可以将事g通知pȝ内所有的ApplicationListener。下面是一个例?
package com.spring;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
public class LoginAction implements ApplicationContextAware{
private ApplicationContext ac;
public void setApplicationContext(ApplicationContext arg0) throws BeansException {
// TODO Auto-generated method stub
this.ac=arg0;
}
public int login(String username,String password){
ActionEvent ae=new ActionEvent(username);
this.ac.publishEvent(ae);
return 0;
}
}
package com.spring;
import org.springframework.context.ApplicationEvent;
public class ActionEvent extends ApplicationEvent {
public ActionEvent(Object source) {
super(source);
}
}
package com.spring;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
public class ActionListener implements ApplicationListener{
public void onApplicationEvent(ApplicationEvent event) {
if(event instanceof ActionEvent){
System.out.println(event.toString());
}
}
}
需要在beans.xml文g里添加以下配|?br /><bean id="loginaction" class="net.xiaxin.beans.LoginAction"/>
<bean id="listener" class="net.xiaxin.beans.ActionListener"/>
试ҎQ?br /> public static void testListener(){
ApplicationContext ac=new FileSystemXmlApplicationContext("src/bean.xml");
LoginAction la=(LoginAction)ac.getBean("loginaction");
la.login("peidw", "123456");
}
Struts in Spring
Struts和Spring是如何整?Z在struts加加载Spring Context;在struts-config.xml文g中添加以下配|?br /><struts-config>
<plug-in
className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml" />
</plug-in>
</struts-config>
通过plug-in我们实现了Spring Context的加载,不过仅仅加蝲Contextq没有什么实际意义,我们q需要修攚w|,Struts Action交给Spring容器q行理Q?br />
]]>