MVC3 一些杂七杂八的东西

要解决的问题:

@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的朋友的Demo

import 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");
		}
	}

}

 

 

 

 

 

你可能感兴趣的:(mvc)