在使用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; } }
相关推荐
Spring Mvc AOP通过注解方式拦截controller等实现日志管理
Spring MVC AOP通过注解方式拦截Controller等实现日志管理demo版本2
有人问 Sping AOP用AspectJ注解的方式拦截不到SpringMVC的controller方法? 我这里提供了一种解决方法,仅供参考
Spring MVC AOP通过自定义注解方式拦截Controller等实现日志管理, springMVC里做添加AOP拦截,用于捕获异常。
spring mvc利用aop日志实现对controller层的拦截
通过注解方式拦截controller等例子,实现日志管理
Spring MVC是Spring框架中的一部分,全称是Spring Web MVC,主要用于实现MVC设计模式的Web框架。它分离了控制器、模型对象、过滤器以及处理程序对象的角色,使得它们更容易进行定制。 Spring MVC的优点包括: 基于...
Java SSM项目是一种使用Java语言和SSM框架(Spring + Spring MVC + MyBatis)开发的Web应用程序。SSM是一种常用的Java开发框架组合,它结合了Spring框架、Spring MVC框架和MyBatis框架的优点,能够快速构建可靠、...
自定义日志输出,加aop切面Controller,使用DRUID访问mysql,spring mvc基本框架搭建,适合初学者,页面适用IE6
三层架构设计模式(MVC): 我们将详细介绍MVC模式的概念,包括模型(Model)、视图(View)、控制器(Controller)之间的关系。您将了解如何将业务逻辑、用户界面和数据分离,以便更好地管理和维护您的应用程序。 ...
背景做项目时碰到 Controller 不能使用 AOP 进行拦截,从网上搜索得知:使用 Spring MVC 启动了两个 context:Applicatio
Java SSM项目是一种使用Java语言和SSM框架(Spring + Spring MVC + MyBatis)开发的Web应用程序。SSM是一种常用的Java开发框架组合,它结合了Spring框架、Spring MVC框架和MyBatis框架的优点,能够快速构建可靠、...
Java SSM项目是一种使用Java语言和SSM框架(Spring + Spring MVC + MyBatis)开发的Web应用程序。SSM是一种常用的Java开发框架组合,它结合了Spring框架、Spring MVC框架和MyBatis框架的优点,能够快速构建可靠、...
Java SSM项目是一种使用Java语言和SSM框架(Spring + Spring MVC + MyBatis)开发的Web应用程序。SSM是一种常用的Java开发框架组合,它结合了Spring框架、Spring MVC框架和MyBatis框架的优点,能够快速构建可靠、...
Java SSM项目是一种使用Java语言和SSM框架(Spring + Spring MVC + MyBatis)开发的Web应用程序。SSM是一种常用的Java开发框架组合,它结合了Spring框架、Spring MVC框架和MyBatis框架的优点,能够快速构建可靠、...
Java SSM项目是一种使用Java语言和SSM框架(Spring + Spring MVC + MyBatis)开发的Web应用程序。SSM是一种常用的Java开发框架组合,它结合了Spring框架、Spring MVC框架和MyBatis框架的优点,能够快速构建可靠、...
Java SSM项目是一种使用Java语言和SSM框架(Spring + Spring MVC + MyBatis)开发的Web应用程序。SSM是一种常用的Java开发框架组合,它结合了Spring框架、Spring MVC框架和MyBatis框架的优点,能够快速构建可靠、...
Java SSM项目是一种使用Java语言和SSM框架(Spring + Spring MVC + MyBatis)开发的Web应用程序。SSM是一种常用的Java开发框架组合,它结合了Spring框架、Spring MVC框架和MyBatis框架的优点,能够快速构建可靠、...
Java SSM项目是一种使用Java语言和SSM框架(Spring + Spring MVC + MyBatis)开发的Web应用程序。SSM是一种常用的Java开发框架组合,它结合了Spring框架、Spring MVC框架和MyBatis框架的优点,能够快速构建可靠、...