在使用Spring MVC的时候,一般会有至少2个Spring的XML。一个用来配置一般的Spring的Bean(如:spring-ctx-application.xml),一个用来声明Spring Controller对于的view的内容(如:spring-mvc-servlet.xml)。
很多人发现在spring-ctx-application.xml里面声明AOP对Controller不起作用,这是为什么呢?因为Spring MVC里面的一般性配置spring-ctx-application.xml是一个上下文,而管理Spring MVC的spring-mvc-servlet.xml又是一个上下文。对Controller的配置都在spring-mvc-servlet.xml这个里面。也就是说存在2个不同的Spring上下文。你配置在spring-ctx-application.xml的堆Controller的AOP当然不起作用了。如何修改呢?在spring-mvc-servlet.xml里面再添加一份AOP的配置即可。同样的道理,在Controller里面配置@Value,也需要在spring-mvc-servlet.xml里面指定.properties的配置文件,才能正确的读取内容。
spring-mvc-servlet.xml如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:jms="http://www.springframework.org/schema/jms" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:oxm="http://www.springframework.org/schema/oxm" xmlns:p="http://www.springframework.org/schema/p" xmlns:sec="http://www.springframework.org/schema/security" xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.0.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-4.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:console-configure.properties</value> </list> </property> </bean> <mvc:annotation-driven /> <context:component-scan base-package="com.kanpiaoxue.rigel.dmap.console.controller" /> <aop:aspectj-autoproxy /> <bean id="performanceCalculate" class="com.kanpiaoxue.rigel.dmap.console.utils.PerformanceCalculateController"> <property name="order" value="100" /> </bean> <bean class="com.kanpiaoxue.rigel.dmap.console.utils.DmapExceptionResolver" /> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" > <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter"/> </list> </property> </bean> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>text/javascript;charset=UTF-8</value> </list> </property> </bean> <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="mediaTypes"> <map> <entry key="html" value="text/html" /> <entry key="json" value="application/json" /> <entry key="xml" value="application/xml" /> </map> </property> <property name="viewResolvers"> <list> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> </list> </property> <!-- <property name="defaultViews"> <list> <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" /> </list> </property> --> </bean> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="UTF-8"> <property name="resolveLazily" value="true" /> <property name="maxUploadSize" value="${file.upload.max.size}" /> <property name="maxInMemorySize" value="${file.upload.max.in.memory.size}" /> </bean> <!-- cache start --> <!-- <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcache" /> EhCache library setup <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:${dmap.console.cache.file}" /> </bean> --> <!-- cache end --> </beans>
AOP范例类如下:
package com.kanpiaoxue.rigel.dmap.console.utils; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.Ordered; import com.google.common.base.Stopwatch; /** * <pre> * PerformanceCalculateController .java
* @author kanpiaoxue<br> * @version 1.0 * Create Time 2014年8月29日 下午3:47:10<br> * Description : AOP性能切面类 * </pre> */ @Aspect public class PerformanceCalculateController implements Ordered { private static final Logger LOGGER = LoggerFactory .getLogger(PerformanceCalculate.class); private int order = 1; /** * <pre> * 统计指定切面的方法的耗时,单位:毫秒 * @param point * @return * @throws Throwable * </pre> */ @Around("execution(* com.kanpiaoxue.rigel.dmap.console.controller.*.*(..))") public Object calculateConsume(ProceedingJoinPoint point) throws Throwable { Object[] args = point.getArgs(); Stopwatch watch = Stopwatch.createStarted(); Object rs = null; if (null != args) { rs = point.proceed(args); } else { rs = point.proceed(); } String className = point.getTarget().getClass().getName(); String methodName = point.getSignature().getName(); if (LOGGER.isDebugEnabled()) { LOGGER.debug(String.format("%s.%s consumes %s.", className, methodName, watch.toString())); } return rs; } @Override public int getOrder() { return this.order; } public void setOrder(int order) { this.order = order; } }