Spring AOP

AOP概念

  • 通知(Advice)

需要扩展的功能,类似上一例程中的审计功能

  • 连接点(JoinPoint)

使用通知的地方,比如方法前后,异常发生时。

  • 切入点(Pointcut)

符合要求的连接点集合是切入点,根据表达式筛选连接点。

  • 切面(Aspect)

切面是通知和切入点的结合

  • 目标对象 (Target)

代理的目标对象

  • 织入 (Weaving)

将切面应用到目标对象来创建新的代理对象过程。

  • 引入(introduction)

向现有的类添加新方法属性




UserMng.java

java 复制代码
package com.hk.eshop.aop;

import com.hk.eshop.java.IUserMng;

public class UserMng implements IUserMng {
    public void save(String id, String name) {
        System.out.println("save() id="+id+",name="+name);
    }

    public void find(String id) {
        System.out.println("find() id="+id);
    }
}

AuditAspect.java

java 复制代码
package com.hk.eshop.aop;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;

public class AuditAspect {
    public void audit(JoinPoint joinPoint)
    {
        Object[] args = joinPoint.getArgs();
        Signature sig = joinPoint.getSignature();
        System.out.println("触发方法......"+sig.getName());
        System.out.println("方法审计开始......");
        for(int i=1;i<=args.length;i++)
            System.out.println("第"+i+"个参数="+args[i-1]);
        System.out.println("方法审计结束......");
        System.out.println("切入点表达式......"+joinPoint.toString());
    }
}

Main.java

java 复制代码
package com.hk.eshop.aop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main( String[] args )
    {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
        UserMng umng = (UserMng)ctx.getBean("userMng");
        umng.save("1","Java");
        umng.find("2");
    }
}

beans.xml

xml 复制代码
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.0.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <context:component-scan base-package="com.hk.eshop"/>

    <bean id="userMng" class="com.hk.eshop.aop.UserMng"/>
    <bean id="auditAspect" class="com.hk.eshop.aop.AuditAspect"/>

    <aop:config proxy-target-class="true">
        <aop:aspect id="aspect1" ref="auditAspect">
            <aop:pointcut id="save" expression="execution(* com.hk.eshop.aop.UserMng.save(..))"/>
            <aop:pointcut id="find" expression="execution(* com.hk.eshop.aop.UserMng.find(..))"/>
            <aop:before method="audit" pointcut-ref="save"></aop:before>
            <aop:before method="audit" pointcut-ref="find"></aop:before>
        </aop:aspect>
    </aop:config>
</beans>

POM.xml

xml 复制代码
<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>com.hk.eshop</groupId>
  <artifactId>SpringOne</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>SpringOne</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>3.2.18.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aspects</artifactId>
      <version>3.2.18.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>cglib</groupId>
      <artifactId>cglib-full</artifactId>
      <version>2.0.2</version>
    </dependency>
    <dependency>
      <groupId>aspectj</groupId>
      <artifactId>aspectj-tools</artifactId>
      <version>1.0.6</version>
    </dependency>
  </dependencies>
</project>




相关推荐
171320330计算机毕设编程2 分钟前
计算机毕业设计 基于Hadoop的新能源汽车发展前景分析 LW PPT
java·大数据·hadoop·python·算法·随机森林·线性回归
技术小结-李爽4 分钟前
【工具】怎么在终端中启动springboot项目?
java·spring boot·后端·maven
Shaoshing11 分钟前
消息发送
java·开发语言
Code_Solitude29 分钟前
c语言:指针(3)+冒泡排序
java·c语言·算法
乌暮40 分钟前
从 `println` 到生产级日志:Spring Boot 日志体系完全指南
java·开发语言·jvm·spring boot·后端·学习·面试
脉动数据行情1 小时前
Java OkHttp 对接国际期货批量行情 SpringBoot 定时落库实践
java·spring boot·okhttp
Wang's Blog1 小时前
Java 项目实战: 交友项目 - 百度人脸识别:介绍
java·开发语言·交友
王中阳Go1 小时前
第一单交付完,我等 3 个月没复购:不是交付差,是这 3 件事没做
人工智能·后端·面试
打工仔折腾 AI1 小时前
260+异构同步链路实战:如何用语义化自动翻译破解CDC DDL同步痛点
人工智能·后端·python·自然语言处理·性能优化·langchain·机器翻译
松就是我902981 小时前
Agent工具调用实现原理
后端