問題以下
java
今天發現aop不能在controller裏面不能用,換成service能用,用junit-test也行,就是放到tomcat下面不行,我就納悶了!!!web
package com.tutor.normal.filter; import javax.annotation.Resource; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.tutor.normal.service.ITutorUserService; /** * * @author jinghao.liang * */ @Aspect public class TutorLoginAop { private static Logger log = LoggerFactory.getLogger(TutorLoginAop.class); @Resource(name = "tutorUserService") private ITutorUserService tutorUserService; @Pointcut("execution(* com.tutor.normal.controller..*.*(..))") private void anyMethod() { }// 定義一個切入點 @Before("anyMethod() && args(name)") public void doAccessCheck(String name) { System.out.println(name); System.out.println("前置通知"); } @AfterReturning("anyMethod()") public void doAfter() { System.out.println("後置通知"); } @After("anyMethod()") public void after() { System.out.println("最終通知"); } @AfterThrowing("anyMethod()") public void doAfterThrow() { System.out.println("例外通知"); } @Around("anyMethod()") public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable { System.out.println("進入環繞通知"); Object object = pjp.proceed();// 執行該方法 System.out.println("退出方法"); return object; } }
spring配置,加上這兩個spring
<aop:aspectj-autoproxy proxy-target-class="true"/> <bean id="tutorLoginAop" class="com.tutor.normal.filter.TutorLoginAop" />
尼瑪吖,明明和網上的同樣吖,到底什麼狀況tomcat
通過一天的排查終於找到問題了,原來是spring的配置應該寫到springMVC裏,而不是spring裏面app
什麼意思呢?看我web.xml的配置url
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring/applicationContext.xml</param-value> </context-param> <!-- Spring MVC --> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
原來我把配置寫到了applicationContext.xml裏面固然不行啦TVT,要寫到spring-servlet.xml這個裏面。。。spa