MVC3 一些杂七杂八的东西

编程技术  /  houtizong 发布于 3年前   77

要解决的问题:

@RequestHeader annotation for MVC handler methods
@PathVariable annotation for MVC handler methods
@Value annotation for embedded expression support
introduced @CookieValue annotation for Servlet and Portlet MVC handler methods
introduced @ExceptionHandler annotation for used in annotated web controllers

@Transactional supports qualifier value for choosing between multiple transaction managers
猜想使用方法是两个注解一块使用,当有多个事务管理器时,通过qualifier注解来限定,只有bean的id为指定id的才可以。
@Transactional
@Qualifier("bean的名字")
added TxAnnotationDriven, MvcAnnotationDriven, etc. as out-of-the-box FeatureSpecifications
Spring 3.1的缓存相关的注解  @Cacheable、@CachePut、@CacheEvict  <cache:advice>

综合的
@Configuration
@RequestPart
新产物RedirectAttributes
@RequestMapping(value = "/", method = RequestMethod.POST, consumes="application/json")
@RequestMapping(value = "/", method = RequestMethod.POST, produces="application/json")

如果随机随地取得spring容器管理的bena:

Spring容器在启动时,如果被Spring管理的Bean实现了ApplicationContextAware接口,那么会自动调用ApplicationContextAware接口中的

public void setApplicationContext(ApplicationContext context) throws BeansException

方法,向其传递ApplicationContext 对象。有了ApplicationContext 对象就可以取得所有的Spring管理的bean了。

我试过了,在双亲上下文环境上,可OK。这对于spring 3 MVC很重要。

 

配置文件:

<!-- 用于持有ApplicationContext,可以使用SpringContextHolder.getBean('xxxx')的静态方法得到spring bean对象 --><bean class="包名.SpringContextHolder" lazy-init="false" />

 

Demo:

//这段代码转自itEye上一位叫chenxin的朋友的Demoimport org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;/** * 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext. *  */public class SpringContextHolder implements ApplicationContextAware {private static ApplicationContext applicationContext;/** * 实现ApplicationContextAware接口的context注入函数, 将其存入静态变量. */public void setApplicationContext(ApplicationContext applicationContext) {SpringContextHolder.applicationContext = applicationContext; // NOSONAR}/** * 取得存储在静态变量中的ApplicationContext. */public static ApplicationContext getApplicationContext() {checkApplicationContext();return applicationContext;}/** * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型. */@SuppressWarnings("unchecked")public static <T> T getBean(String name) {checkApplicationContext();return (T) applicationContext.getBean(name);}/** * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型. */@SuppressWarnings("unchecked")public static <T> T getBean(Class<T> clazz) {checkApplicationContext();return (T) applicationContext.getBeansOfType(clazz);}/** * 清除applicationContext静态变量. */public static void cleanApplicationContext() {applicationContext = null;}private static void checkApplicationContext() {if (applicationContext == null) {throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder");}}}

 

 

 

 

 

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客