spingAOP在springMVC中的使用(我用在攔截controller中的方法。主要用於登錄控制)


首先截取了網上的一張配置execution的圖片

我在項目中關於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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM