Loaded on more than view.properties SpringMVC development issues
June 17, 2010
use SpringMVC the development process, if you use a mixed view of way, and using the ResourceBundleViewResolver way. The only way to read the default classpath following view.properties (of course, be other properties files, but also with Locale, the basename of the properties can be configured).
For a project to develop if more than one person situation, we have to modify this file is clearly not practical, the best way to have each module below view.properties, in the system whether to automatically start loading, which can increase the concurrent development of the efficiency and reduce the cost of the project synthesis.
simply looked ResourceBundleViewResolver, which would like to start from this class to achieve a ViewResolver, so as to achieve automatic load, the following is achieved:
/ / $ Id: AutoResourceBundleViewResolver. java 2010-6-17 morning 11:26:23
package com.jtv.walllee.common.extend;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.NoSuchBeanDefini tionException;
import org.springframework.beans.factory.support. Properti esBeanDefinitionReader;
import org.springframework.context.ConfigurableApplicatio nContext;
import org.springframework.core.Ordered;
import org.springframework . core.io.Resource;
import org.springframework.core.io.support.PathMatchingRe sourcePatternResolver;
import org.springframework.core.io.support.ResourcePatter nResolver ;
import org.springframework.web.context.support.GenericWeb ApplicationContext;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.AbstractCachi ngViewResolver;
/ **
*
*
* @ author Wall.Lee (xiaoshan2242@gmail.com)
* /
public class AutoResourceBundleViewResolver extends AbstractCachingViewResolver implements Ordered, DisposableBean {
private int order = Integer.MAX_VALUE; / / default: same as non-Ordered
private String scanFileName; / / scan view filename
private String defaultParentView;
private Locale [] localesToInitialize;
/ * Locale -> BeanFactory * /
private final Map
/ * List of ResourceBundle -> BeanFactory * /
private final Map
public void setOrder (int order) {
this.order = order;
}
public int getOrder () {
return this.order;
}
public void setDefaultParentView (String defaultParentView) {
< br /> this.defaultParentView = defaultParentView;
}
public void setLocalesToInitialize (Locale [] localesToInitialize) {
this.localesToInitialize = localesToInitialize;
}
protected void initApplicationContext () throws BeansException {
if (this.localesToInitialize! = null) {
for (int i = 0; i
}
}
}
protected View loadView (String viewName, Locale locale) throws Exception {
BeanFactory factory = initFactory (locale) ;
try {
return (View) factory.getBean (viewName, View.class);
}
catch (NoSuchBeanDefinitionException ex) {
return null;
}
}
protected synchronized BeanFactory initFactory (Locale locale) throws BeansException {
if (isCache ()) {
BeanFactory cachedFactory = (BeanFactory) this.localeCache.get (locale);
if (cachedFactory! = null) {
return cachedFactory;
}
}
/ / Build list of ResourceBundle references for Locale.
Resource [] resouces = getAllResources ();
if (isCache ()) {
BeanFactory cachedFactory = ( BeanFactory) this.resourceCache.get (resouces);
if (cachedFactory! = null) {
this.localeCache.put (locale, cachedFactory);
return cachedFactory;
}
}
/ / Create child ApplicationContext for views.
GenericWebApplicationContext factory = new GenericWebApplicationContext ();
factory.setParent (getApplicationContext ());
factory.setServletContext (getServletContext ());
/ / Load bean definitions from resource bundle.
PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader (factory);
reader.setDefaultParentBean (this.defaultParentView);
reader.loadBeanDefinitions (resouces);
factory.refresh ();
if (isCache ()) {
this.localeCache.put ( locale, factory);
this.resourceCache.put (resouces, factory);
}
return factory;
}
@ SuppressWarnings (“unchecked”)
public void destroy () throws BeansException {
for (Iterator it = this.resourceCache . values ??(). iterator (); it.hasNext ();) {
ConfigurableApplicationContext factory = (ConfigurableApplicationContext) it.next ();
factory.close () ;
}
this.localeCache.clear ();
this.resourceCache.clear ();
}
/ **
* need to search for file system resources
* @ return
* @ throws IOException
* /
private Resource [] getAllResources () {
if (scanFileName == null| “”. equals (scanFileName )) {
scanFileName = “view.properties”;
}
String filePattern = “classpath *:/**/*” scanFileName;
ResourcePatternResolver resoler = new PathMatchingResourcePatternResolver ();
try {
return resoler.getResources (filePattern);
} catch (IOException e) {
e.printStackTrace ();
}
return new Resource [0];
}
public String getScanFileName () {
return scanFileName;
}
public void setScanFileName (String scanFileName) {
this.scanFileName = scanFileName;
}
}
– Configuration < br />
in the *- servlet.xml configuration
which identifies the need to scan documents scanFileName , Note: the default scan path is the classpath.
Posted: January 6th, 2012
at 11:19am by admin
Tagged with disposablebean
Categories: Uncategorized
Comments: No comments













