面向切面编程 spring AOP简单项目实例配置演示

项目准备:

在这里插入图片描述
pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>net.xdclass</groupId>
    <artifactId>xd_spring</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>


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


    </dependencies>

    <repositories>

        <repository>
            <id>maven-ali</id>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
                <checksumPolicy>fail</checksumPolicy>
            </snapshots>

        </repository>
    </repositories>

</project>

重点:maven仓库可以换成阿里云的,国内快
(可以直接在pom中添加,也可整体更换)

 <repositories>

        <repository>
            <id>maven-ali</id>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
                <checksumPolicy>fail</checksumPolicy>
            </snapshots>

        </repository>
    </repositories>

SpringAOP配置日志打印,配置切面和织⼊

1.添加schema

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

2.配置bean和aop
在这里插入图片描述

applicationContext.XML:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"
        xmlns:aop="http://www.springframework.org/schema/aop">

  
    <bean id="timeHandler" class="net.xdclass.sp.aop.TimeHandler"/>
    <bean id="videoService" class="net.xdclass.sp.service.VideoServiceImpl"/>

    <!--aop配置-->
    <aop:config>

        <!--横切关注点-->
        <aop:aspect id="timeAspect" ref="timeHandler">

            <!--定义切入点表达式-->
            <!--<aop:pointcut id="allMethodLogPointCut" expression="execution(* net.xdclass.sp.service.VideoService.sav*(..))"/>-->
                <aop:pointcut id="allMethodLogPointCut" expression="execution(* net.xdclass.sp.service.VideoService.*(..))"/>

            <!--配置前置通知和后置通知-->
                <aop:before method="printBefore" pointcut-ref="allMethodLogPointCut"/>
                <aop:after method="printAfter" pointcut-ref="allMethodLogPointCut"/>
        </aop:aspect>

        <!--横切关注点-->
        <!--<aop:aspect id="timeAspect" ref="timeHandler">-->

            <!--&lt;!&ndash;定义切入点表达式&ndash;&gt;-->
            <!--&lt;!&ndash;<aop:pointcut id="allMethodLogPointCut" expression="execution(* net.xdclass.sp.service.VideoService.sav*(..))"/>&ndash;&gt;-->
            <!--<aop:pointcut id="allMethodLogPointCut" expression="execution(* net.xdclass.sp.service.VideoService.*(..))"/>-->

            <!--&lt;!&ndash;配置前置通知和后置通知&ndash;&gt;-->
            <!--<aop:before method="printBefore" pointcut-ref="allMethodLogPointCut"/>-->
            <!--<aop:after method="printAfter" pointcut-ref="allMethodLogPointCut"/>-->
        <!--</aop:aspect>-->

    </aop:config>



</beans>

Timehandle(横切关注点):

package net.xdclass.sp.aop;

import java.time.LocalDateTime;

//横切关注点
public class TimeHandler {

    public void printBefore(){
        System.out.println("printBefore 日志  time = "+ LocalDateTime.now().toString());
    }


    public void printAfter(){
        System.out.println("printAfter 日志  time = "+ LocalDateTime.now().toString());
    }

}

VideoService:

package net.xdclass.sp.service;

import net.xdclass.sp.domain.Video;

public interface VideoService {

    int save(Video video);

    Video findById(int id);

}

VideoServiceImpl

package net.xdclass.sp.service;

import net.xdclass.sp.domain.Video;

public class VideoServiceImpl implements VideoService {

    public int save(Video video) {

        System.out.println("保存video");

        return 1;
    }

    public Video findById(int id) {

        System.out.println("根据id找视频");

        return new Video();
    }
}

在这里插入图片描述
在这里插入图片描述
重点:
如果这里的切入点表达式路径我换成了sav开头,那么就是save方法会被切入追踪通知,而根据id找视频方法就不会有前后置通知
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值