When I did the aop test, I found that pointcut had no effect, and then I checked it and found the problem
If you use spring mvc, if you put <aop:aspectj-autoproxy proxy-target-class="true"/> in the application.xml file, aop may be invalid, it is best to put it in the dispatcher-servlet.xml file middle
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="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.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <!-- http request returns json --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/plain; charset=UTF-8</value> </list> </property> </bean> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </list> </property> </bean> <!-- Automatically scanned package name--> <context:component-scan base-package="com.wonder"/> <!-- Default annotation mapping support --> <mvc:annotation-driven/> <context:component-scan base-package="com.wonder.aspect"></context:component-scan> <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy> <!-- View Interpretation Class--> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/"/> <property name="suffix" value=".html"/> </bean> <bean class="com.wonder.controller.commonUtil.LocalHandlerExceptionResolver"/> <mvc:resources location="/js/" mapping="/js/**"/> <!--Processing static files--> <mvc:default-servlet-handler/> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/> <mvc:exclude-mapping path="/js/**"/> <mvc:exclude-mapping path="/WEB-INF/view/**"/> <bean class="com.wonder.interceptor.LoginInterceptor"/> </mvc:interceptor> </mvc:interceptors> </beans>
aop class
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; /** * Created by Guozhijie on 2016/7/7. */ @Component @Aspect public class HelloWorldAspect { public void beforeAdvance(){ System.out.println("before............."); } public void afterAdvance(){ System.out.println("after............."); } @Pointcut(value="execution(* com.wonder.service..*.testOut(..)) && args(param)", argNames = "param") public void beforePointcut(String param) { } @Before(value = "beforePointcut(param)", argNames = "param") public void beforeAdvice(String param) { System.out.println("===========before advice param:" + param); } }
output
===========before advice param:aaa
service test ###########aaa