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 学习笔记


版权声明:本文为weidong_y原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。