問題如標題所示
applicationContext.xml
<!--開啓註解掃描,之掃描spring,mybatis,不掃描springmvc-->
<context:component-scan base-package="com.nextStep">
<!--配置那些註解不掃描-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--配置aop-aspectj攔截註解生效-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
springmvc.xml
<!--開啓註解掃描,之掃描Controller註解-->
<context:component-scan base-package="com.nextStep">
<!--配置之掃描該註解,與applicationContext相反-->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
aspectTest.java
@Component
@Aspect
public class UserAspect {
@Before("execution(* com.nextStep.service..*(..))")
public void befor(JoinPoint joinPoint) throws IOException {
joinPoint.getSignature().getName(); //獲得調用方法名
System.out.println("前置通知生效");
}
@AfterReturning("execution(* com.nextStep.service..*(..))")
public void after(JoinPoint joinPoint){
}
@AfterThrowing(value = "execution(* com.nextStep.service..*(..))" ,throwing = "e")
public void aftherThread(Exception e){
System.out.println("異常通知"+e.getMessage());
}
}
嘗試攔截service層的方法時,發現好像,攔截不成功,然後把@Before註解中的service改爲dao,發現攔截成功了
那麼說明這個寫法,是沒有問題的
幾經糾結,發現在springmvc.xml中需要開啓代理
至於爲什麼要這麼做,我也 I don't no ,只管會用就可以了。
springmvc.xml
<!--開啓註解掃描,之掃描Controller註解-->
<context:component-scan base-package="com.nextStep">
<!--配置之掃描該註解,與applicationContext相反-->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--開啓動態代理-->
<aop:aspectj-autoproxy proxy-target-class="true" />