小弟对spring aop不熟,现在有个项目需要记录日志,不想每个controller中去写,就想使用AOP,结果呢,配置后启动没错,但不出来
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd "> <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> <!-- Scans within the base package of the application for @Components to configure as beans --> <!-- @Controller, @Service, @Configuration, etc. --> <aop:aspectj-autoproxy proxy-target-class="true" /> <mvc:resources location="/css/" mapping="/css/**" /> <mvc:resources location="/img/" mapping="/img/**" /> <mvc:resources location="/js/" mapping="/js/**" /> <mvc:resources location="/json/" mapping="/json/**" /> <mvc:resources location="/jquery-easyui-1.3.3/" mapping="/jquery-easyui-1.3.3/**" /> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="100000000" /> </bean> </beans>然后呢,AOP类
package com.*.windrunner.aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; @Aspect @Component public class ControllerLogAspect { @Before(value = "execution(* com.*.windrunner.controller.*(..))") public void beforeMethod(JoinPoint point) { System.out.println("------test aop before"); } }
这个里面因为有公司项目,所以把包名用*号代替了,见谅!!现在不输,求帮忙
controller里面无法直接这样切入的。需要切入
execution(* org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(..))
因为你controller注解的类,都被这个org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter给代理了。 ######谢谢,我想请问下,还有没有其它的配置需要做呢?谢谢###### 我的功能和你类似,是这么写的
/** * 异常日志处理 * * @param joinPoint * @param throwable */ // 拦截语法 // AbstractAction的子类被@RequestMapping注解的方法 @Around("within(cn.org.sysu.cems.utils.superclass.AbstractAction+) && @annotation(org.springframework.web.bind.annotation.RequestMapping)") public ModelAndView handleError(ProceedingJoinPoint joinPoint) {主要是那个@annotation 如果光用execution的话可能会把你Controller本身的getter setter等非请求处理方法一并给拦截了 ######请问我这个配置对吗?我没有配appalicationContext.xml,上面的XML是spring-servlet.xml######
<!-- 扫描的时候过滤掉Service层,aop要在service进行切入! --> <context:component-scan base-package="com.xxx.infoaudit"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/> </context:component-scan>在spring里面扫描service 层
<context:component-scan base-package="com.**.service" />
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。