spingAOP在springMVC中的使用(我用在拦截controller中的方法。主要用于登录控制)

首先截取了网上的一张配置execution的图片

spingAOP在springMVC中的使用(我用在拦截controller中的方法。主要用于登录控制)

我在项目中关于aop的配置:如果拦截controller的方法,需要在spring-mvc.xml文件中加入(如果在spring.xml中加入则无法拦截controller层方法)

使用 and not 来排除对某些方法的拦截

<!--let spring know to use cglib not jdk  -->
<aop:aspectj-autoproxy proxy-target-class="true" /> <bean id="loginAOP" class="com.shop.aop.LoginAOP" />
<aop:config>
<aop:aspect id="aspect" ref="loginAOP">
<aop:pointcut expression="execution(* com.shop.controller..*.*(..)) and not execution(* com.shop.controller.LoginController.*(..))"
id="controller" />
<aop:before method="beforeExec" pointcut-ref="controller" />
</aop:aspect>
</aop:config>

<!--let spring know to use cglib not jdk有了这个Spring就能够自动扫描被@Aspect标注的切面了 -->

<aop:aspectj-autoproxy proxy-target-class="true" />

其实普通的xml配置不需要上面这行(如果使用基于AspectJ的就需要)

<bean id="loginAOP" class="com.shop.aop.LoginAOP" />
<aop:config>
<aop:aspect id="aspect" ref="loginAOP">
<aop:pointcut expression="execution(* com.shop.controller..*.*(..))"
id="controller" />
<aop:before method="beforeExec" pointcut-ref="controller" />
</aop:aspect>
</aop:config>

下面的是截取自 http://blog.csdn.net/udbnny/article/details/5870076 的一部分(博主没有说能不能转)。大神就是大神,轻松的写出了springAOP实现方法。完全懂了怎么用了。

一种方式是使用AspectJ提供的注解:

package test.mine.spring.bean;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class SleepHelper { public SleepHelper(){ } @Pointcut("execution(* *.sleep())")
public void sleeppoint(){} @Before("sleeppoint()")
public void beforeSleep(){
System.out.println("睡觉前要脱衣服!");
} @AfterReturning("sleeppoint()")
public void afterSleep(){
System.out.println("睡醒了要穿衣服!");
} } 用@Aspect的注解来标识切面,注意不要把它漏了,否则Spring创建代理的时候会找不到它,@Pointcut注解指定了切点,@Before和@AfterReturning指定了运行时的通知,注 意的是要在注解中传入切点的名称 然后我们在Spring配置文件上下点功夫,首先是增加AOP的XML命名空间和声明相关schema
命名空间:
xmlns:aop="http://www.springframework.org/schema/aop"
schema声明:
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd 然后加上这个标签:
<aop:aspectj-autoproxy/> 有了这个Spring就能够自动扫描被@Aspect标注的切面了 最后是运行,很简单方便了:
public class Test { public static void main(String[] args){
ApplicationContext appCtx = new ClassPathXmlApplicationContext("applicationContext.xml");
Sleepable human = (Sleepable)appCtx.getBean("human");
human.sleep();
}
} 下面我们来看最后一种常用的实现AOP的方式:使用Spring来定义纯粹的POJO切面 前面我们用到了<aop:aspectj-autoproxy/>标签,Spring在aop的命名空间里面还提供了其他的配置元素:
<aop:advisor> 定义一个AOP通知者
<aop:after> 后通知
<aop:after-returning> 返回后通知
<aop:after-throwing> 抛出后通知
<aop:around> 周围通知
<aop:aspect>定义一个切面
<aop:before>前通知
<aop:config>*配置元素,类似于<beans>这种东西
<aop:pointcut>定义一个切点 我们用AOP标签来实现睡觉这个过程:
代码不变,只是修改配置文件,加入AOP配置即可:
<aop:config>
<aop:aspect ref="sleepHelper">
<aop:before method="beforeSleep" pointcut="execution(* *.sleep(..))"/>
<aop:after method="afterSleep" pointcut="execution(* *.sleep(..))"/>
</aop:aspect>
</aop:config>
上一篇:BZOJ4519——[cqoi2016]不同的最小割


下一篇:身份证校验(c++实现)