1. 程式人生 > >SSM 環境下的 AOP 配置與簡單應用

SSM 環境下的 AOP 配置與簡單應用

一、需要匯入的 jar 包

<!-- Spring AOP 日誌管理需要匯入的包 -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.13</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.13</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.3.18.RELEASE</version>
        </dependency>

二、無註解方式的簡單應用

1.新建測試的 service 層

package com.yyzheng.service;

public interface SayService {
    public void saying();
    public void update();
}

2.新建測試層的實現 serviceImpl 

package com.yyzheng.service.impl;

import com.yyzheng.service.SayService;
import org.springframework.stereotype.Service;

@Service
public class SayServiceImpl implements SayService {

    @Override
    public void saying() {
        System.out.println("我正在說話啊``````");
    }

    @Override
    public void update() {
        System.out.println("我是更新內容的");
    }
}

3.aop 的配置(在spring的配置檔案中配置即可)

pointcut 需要配置到服務層的具體實現類

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd" >
<!-- 掃描 aop 包 -->
    <context:component-scan base-package="com.yyzheng.aop" />

    <aop:config>
        <aop:aspect ref="myAspect">
        <aop:before method="hello" pointcut="execution(public void com.yyzheng.service.impl.SayServiceImpl.saying())"/>
        </aop:aspect>
    </aop:config>

</beans>

4.在 com.yyzheng.aop 包中編寫切面類

package com.yyzheng.aop;

import org.springframework.stereotype.Component;

@Component(value = "myAspect")
public class MyAspect {
    public void hello() {
        System.out.println("我是切面的內容..");
    }
}

5.新建一個測試類 Demo.java

由於我的 spring 配置檔案是分開寫的。所以匯入的比較多,建議:除了 springmvc 和 web.xml 的配置,aop 、service 和 dao 層的配置都要導進來,不然會報錯。

package aop;

import com.yyzheng.service.AdminService;
import com.yyzheng.service.SayService;
import com.yyzheng.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring/spring-aop.xml","classpath:spring/spring-service.xml","classpath:spring/spring-dao.xml"})

public class Demo {

    @Autowired
    private SayService sayService;

    @Test
    public void testA(){
        sayService.saying();
        sayService.update();
    }
}

跑了測試類的結果如下:

三、註解方式的 AOP 應用

1.直接使用上面新建好的服務層

2.aop 註解方式的配置

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd" >

    <!-- 掃描 aop 包 -->
    <context:component-scan base-package="com.yyzheng.aop" />
    <!-- 開啟註解掃描 -->
    <aop:aspectj-autoproxy proxy-target-class="true" />

    <!--<aop:config>-->
        <!--<aop:aspect ref="myAspect">-->
        <!--<aop:before method="hello" pointcut="execution(public void com.yyzheng.service.impl.SayServiceImpl.saying())"/>-->
        <!--</aop:aspect>-->
    <!--</aop:config>-->

</beans>

3.在 com.yyzheng.aop 包中編寫切面類

package com.yyzheng.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class YouAspect {

    @Before("update()")
    public void beforecontext() {
        System.out.println("我是 update 增強內容");
    }

    @Pointcut(value = "execution(public void com.yyzheng.service.impl.SayServiceImpl.update())")
    public void update() {
    }

}

4.編寫測試方法

@Test
public void testB(){
    sayService.saying();
    sayService.update();
}

跑一下測試方法:

更多AOP相關的資料學習

參考另外一篇部落格:

spring 學習筆記