问题如标题所示
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" />