十七、如何取得Spring管理的bean(请用第3种方法)
1、servlet方式加载时,
【web.xml】
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/springMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
spring容器放在ServletContext中的key是org.springframework.web.servlet.FrameworkServlet.CONTEXT.springMVC
注意后面的springMVC,是你的servlet-name配置的值,注意适时修改。
ServletContextsc=略
WebApplicationContextattr=(WebApplicationContext)sc.getAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.springMVC");
2、listener方式加载时:
【web.xml】
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
【jsp/servlet】可以这样取得
ServletContextcontext=getServletContext();
WebApplicationContextapplicationContext=WebApplicationContextUtils.getWebApplicationContext(context);
3、通用的方法来了,神器啊,前的1、2两种方法并不通用,可以抛弃了。
在配置文件中加入:
<!--用于持有ApplicationContext,可以使用SpringContextHolder.getBean('xxxx')的静态方法得到springbean对象-->
<beanclass="com.xxxxx.SpringContextHolder"lazy-init="false"/>
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.ApplicationContextAware;
/**
*以静态变量保存SpringApplicationContext,可在任何代码任何地方任何时候中取出ApplicaitonContext.
*
*/
publicclassSpringContextHolderimplementsApplicationContextAware{
privatestaticApplicationContextapplicationContext;
/**
*实现ApplicationContextAware接口的context注入函数,将其存入静态变量.
*/
publicvoidsetApplicationContext(ApplicationContextapplicationContext){
SpringContextHolder.applicationContext=applicationContext;//NOSONAR
}
/**
*取得存储在静态变量中的ApplicationContext.
*/
publicstaticApplicationContextgetApplicationContext(){
checkApplicationContext();
returnapplicationContext;
}
/**
*从静态变量ApplicationContext中取得Bean,自动转型为所赋值对象的类型.
*/
@SuppressWarnings("unchecked")
publicstatic<T>TgetBean(Stringname){
checkApplicationContext();
return(T)applicationContext.getBean(name);
}
/**
*从静态变量ApplicationContext中取得Bean,自动转型为所赋值对象的类型.
*/
@SuppressWarnings("unchecked")
publicstatic<T>TgetBean(Class<T>clazz){
checkApplicationContext();
return(T)applicationContext.getBeansOfType(clazz);
}
/**
*清除applicationContext静态变量.
*/
publicstaticvoidcleanApplicationContext(){
applicationContext=null;
}
privatestaticvoidcheckApplicationContext(){
if(applicationContext==null){
thrownewIllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder");
}
}
}
转载请注明出处:原文地址:http://elf8848.iteye.com/blog/875830
十八、多视图控制器
当有jsp,flt(模板)等多种页面生成展示方式时,spring默认使用的是“视图解析器链”。真是一个链,所以性能不好,spring会在“视图解析器链”中顺序的查找,直到找到对应的“视图解析器”。jsp视图解析器一定要写在最后面,因为一旦调用jsp,就向浏览器发出数据了,Spring就没有机会再尝试下一个了。
所以自己写一个"多视图解析器",依靠扩展名来区分,可一次准确的选中一个视图解析器,提高性能(会有多少提高呢?没测试过).
下面的例子支持jsp,flt(模板)两种页面生成展示方式,你中以自己添加,支持更多。
<!--多视图处理器-->
<beanclass="com.xxx.core.web.MixedViewResolver">
<propertyname="resolvers">
<map>
<entrykey="jsp">
<beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver">
<propertyname="prefix"value="/WEB-INF/jsp/"/>
<propertyname="viewClass"value="org.springframework.web.servlet.view.JstlView"></property>
</bean>
</entry>
<entrykey="ftl">
<beanclass="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<propertyname="cache"value="true"/>
<propertyname="contentType"value="text/html;charset=UTF-8"></property>
<!--宏命令的支持-->
<propertyname="exposeSpringMacroHelpers"value="true"/>
<propertyname="viewClass"value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/>
<propertyname="requestContextAttribute"value="rc"></property>
</bean>
</entry>
</map>
</property>
</bean>
<!--freemarkerconfig-->
<beanid="freeMarkerConfigurer"class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<propertyname="templateLoaderPath"value="/WEB-INF/ftl/"/>
<propertyname="freemarkerSettings">
<props>
<propkey="template_update_delay">5</prop>
<propkey="default_encoding">UTF-8</prop>
<propkey="locale">zh_CN</prop>
</props>
</property>
</bean>
importjava.util.Locale;
importjava.util.Map;
importorg.springframework.web.servlet.View;
importorg.springframework.web.servlet.ViewResolver;
/**
*说明:多视图处理器
*
*@author赵磊
*@version创建时间:2011-8-19上午09:41:09
*/
publicclassMixedViewResolverimplementsViewResolver{
privateMap<String,ViewResolver>resolvers;
publicvoidsetResolvers(Map<String,ViewResolver>resolvers){
this.resolvers=resolvers;
}
publicViewresolveViewName(StringviewName,Localelocale)throwsException{
intn=viewName.lastIndexOf(".");
if(n!=-1){
//取出扩展名
Stringsuffix=viewName.substring(n+1);
//取出对应的ViewResolver
ViewResolverresolver=resolvers.get(suffix);
if(resolver==null){
thrownewRuntimeException("NoViewResolverfor"+suffix);
}
returnresolver.resolveViewName(viewName,locale);
}else{
ViewResolverresolver=resolvers.get("jsp");
returnresolver.resolveViewName(viewName,locale);
}
}
}
转载请注明出处:原文地址:http://elf8848.iteye.com/blog/875830
十九、<mvc:annotation-driven/>到底做了什么工作
一句<mvc:annotation-driven/>实际做了以下工作:(不包括添加自己定义的拦截器)
我们了解这些之后,对Spring3MVC的控制力就更强大了,想改哪就改哪里。
<!--注解请求映射-->
<beanclass="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<propertyname="interceptors">
<list>
<refbean="logNDCInteceptor"/><!--日志拦截器,这是你自定义的拦截器-->
<refbean="myRequestHelperInteceptor"/><!--RequestHelper拦截器,这是你自定义的拦截器-->
<refbean="myPermissionsInteceptor"/><!--权限拦截器,这是你自定义的拦截器-->
<refbean="myUserInfoInteceptor"/><!--用户信息拦截器,这是你自定义的拦截器-->
</list>
</property>
</bean>
<beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<propertyname="messageConverters">
<list>
<refbean="byteArray_hmc"/>
<refbean="string_hmc"/>
<refbean="resource_hmc"/>
<refbean="source_hmc"/>
<refbean="xmlAwareForm_hmc"/>
<refbean="jaxb2RootElement_hmc"/>
<refbean="jackson_hmc"/>
</list>
</property>
</bean>
<beanid="byteArray_hmc"class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/><!--处理..-->
<beanid="string_hmc"class="org.springframework.http.converter.StringHttpMessageConverter"/><!--处理..-->
<beanid="resource_hmc"class="org.springframework.http.converter.ResourceHttpMessageConverter"/><!--处理..-->
<beanid="source_hmc"class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/><!--处理..-->
<beanid="xmlAwareForm_hmc"class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter"/><!--处理..-->
<beanid="jaxb2RootElement_hmc"class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"/><!--处理..-->
<beanid="jackson_hmc"class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/><!--处理json-->
转载请注明出处:原文地址:http://elf8848.iteye.com/blog/875830
二十、本文中springMVC.xml配置文件是核心,这里给一个下载地址
要在http://www.iteye.com/网站有注册帐号才能下载(这不能怪我)
Spring_MVC_教程_快速入门_深入分析V1.1.pdf
SpringMVC核心配置文件示例.rar