`
mengqingyu
  • 浏览: 328614 次
  • 性别: Icon_minigender_1
  • 来自: 天津
社区版块
存档分类
最新评论

S2SH读取XML扩展点

阅读更多
由于struts2、hibernate通过默认配置文件不支持通配符配置,所以下面介绍一下servlet基于spring通配符读取器读取xml的扩展功能。
<!-- 自定义配置文件包路径,可配置多个不同的参数和路径 -->
<context-param>
	<param-name>projectPackageName</param-name>
	<param-value>com/test/web/*/</param-value>
</context-param>


public class ApplicationListener implements ServletContextListener{
	public void contextInitialized(ServletContextEvent servletContext) {
		String projectPackageName = servletContext.getInitParameter("projectPackageName");
		String classpath = "classpath*:" + projectPackageName + "**" + configName;
		Resource[] resources = this.resolver.getResources(classpath);
		List configs = new ArrayList();
		for (Resource resource : resources){
			String file = resource.getURL().getPath();
			configs.add(config);
		}
		//configs可放在全局静态变量中
	}
}


1.struts2解析xml扩展点
<!-- configProviders:该参数表示自定义的ConfigurationProvider类,用户可以提供一个或多个实现了ConfigurationProvider接口的类,并将这些类名设置成configProviders属性值。若果有多个ConfigurationProvider类,中间用逗号(,)分隔。-->
<filter>
	<filter-name>struts2</filter-name>
	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	<init-param>
		<param-name>configProviders</param-name>
		<param-value>
			com.test.web.ActionConfigProvider
		</param-value>
	</init-param>
</filter>

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.util.ResourceUtils;
import com.opensymphony.xwork2.config.providers.XmlConfigurationProvider;

public class ActionConfigProvider extends XmlConfigurationProvider {

	private static Logger logger = Logger.getLogger(ActionConfigProvider.class);

	public ActionConfigProvider() {
		super();
		Map<String, String> mappings = new HashMap<String, String>();
		mappings.put("-//OpenSymphony Group//XWork 2.1.3//EN", "xwork-2.3.dtd");
		mappings.put("-//Apache Software Foundation//DTD Struts Configuration 2.3//EN", "struts-2.3.dtd");
		setDtdMappings(mappings);
	}

	@Override
	protected Iterator<URL> getConfigurationUrls(String fileName) throws IOException {
		List<URL> urls = new ArrayList<URL>();

		//假设moduleConfigs是静态变量configs中取出已经被扫描到的struts相关的配置文件List
		for (String s : moduleConfigs) {
			urls.add(ResourceUtils.getURL("classpath:" + s));
		}
		return urls.iterator();
	}
}


2.hibernate解析xml扩展点
<!-- 自定义的xml处理器 -->
<bean id="mappingAutowiring" class="com.test.web.framework.context.MappingAutowiring">
	<property name="mappingResources" ref="mappingResources" />
</bean>
<bean id="mappingResources" class="com.test.web.framework.context.MappingFactoryBean" />


import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.orm.hibernate3.LocalSessionFactoryBean;

public class MappingAutowiring implements BeanPostProcessor {

	private static Logger log = Logger.getLogger(MappingAutowiring.class);
	
	private String[] mappingResources;

	public void setMappingResources(String[] mappingResources) {
		this.mappingResources = mappingResources;
	}

	//IOC实例化每个对象时都会经过此方法
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		if (bean instanceof LocalSessionFactoryBean) {
			((LocalSessionFactoryBean) bean).setMappingResources(this.mappingResources);
		}
		return bean;
	}

	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}
}

import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.FactoryBean;

@SuppressWarnings("rawtypes")
public class MappingFactoryBean implements FactoryBean {

	private static Logger log = Logger.getLogger(MappingFactoryBean.class);

	@SuppressWarnings("unchecked")
	public Object getObject() throws Exception {
		//假设moduleConfigs是静态变量configs中取出已经被扫描到的hibernate相关的配置文件List
		//将List moduleConfigs转为数组返回
		return array;
	}

	public Class<?> getObjectType() {
		return String[].class;
	}

	public boolean isSingleton() {
		return true;
	}
}


<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
	<property name="dataSource" ref="dataSource"/>
	<!--<property name="configLocation" value="classpath:hibernate.cfg.xml"/> 这个属性值可不写,被上述类处理过程结果取代-->		
</bean>


3.spring解析xml扩展点
<!--返回在web.xml 中参数contextClass自定义类对应的对象这个类,这个类实现了XmlWebApplicationContext。-->
<!--XmlWebApplicationContext继承了AbstractRefreshableWebApplicationContext类中定义的方法protected String[] getConfigLocations(),这个方法默认可以加载contextConfigLocation中定义的xml 文件,如果你重写了这个方法还可以在任意地方加载你想要的xml文件。-->
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:conf/applicationContext.xml</param-value><!-- 可直接写通配符 -->
</context-param>

<context-param>
	<param-name>contextClass</param-name>
	<param-value>
		com.test.web.framework.context.BeansContext
	</param-value>
</context-param>


import java.net.URL;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.web.context.support.XmlWebApplicationContext;

public class BeansContext extends XmlWebApplicationContext {

	private static Logger log = Logger.getLogger(BeansContext.class);

	private static final String CLASS_PATH_PREFIX = "classpath:";

	public String[] getConfigLocations() {
		//假设moduleConfigs是静态变量configs中取出已经被扫描到的spring相关的配置文件List
		String[] defaultConfigs = super.getConfigLocations();//XmlWebApplicationContext读取默认的contextConfigLocation配置文件
		//将moduleConfigs和defaultConfigs数组合并返回。
		return locations;
	}

	protected String[] getDefaultConfigLocations() {
		return new String[0];
	}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics