如果本代码有疑问,请访问spring-aop快速入门或者spring-aop动态代理技术(底层分析)

1.导入aop的相关坐标

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>

2.创建目标接口和目标类

public interface TargetInterface {
public void save();
}
public class Target implements TargetInterface{
public void save() {
System.out.println("save running....");
}
}

3.创建切面类

public class MyAspect {

    public void before(){
System.out.println("前置增强...");
}
public void afterReturning(){
System.out.println("后置增强...");
}
//ProceedingJoinPoint://切入点
public Object around(ProceedingJoinPoint point) throws Throwable {
System.out.println("环绕前增强...");
//切点方法,spring封装好的
Object proceed = point.proceed();
System.out.println("环绕后增强...");
return proceed;
}
}

4.将目标和切面类的对象创建权交给spring

<?xml version="1.0" encoding="UTF-8"?>
<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"
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"> <!-- 配置目标对象-->
<bean id="target" class="com.hao.aop.Target"/> <!-- 切面对象-->
<bean id="aspect" class="com.hao.aop.MyAspect"></bean>
</beans>

5.在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"
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/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 配置目标对象-->
<bean id="target" class="com.hao.aop.Target"/> <!-- 切面对象-->
<bean id="aspect" class="com.hao.aop.MyAspect"></bean>
<!-- 配置织入:告诉spring框架哪些方法需要进行哪些增强(前置增强、后置增强)-->
<aop:config>
<!-- 声明切面 切面=切点+通知-->
<aop:aspect ref="aspect">
<aop:before method="before" pointcut="execution(public void com.hao.aop.Target.save())"></aop:before>
<aop:after method="afterReturning" pointcut="execution(public void com.hao.aop.Target.save())"></aop:after>
<aop:around method="around" pointcut="execution(public void com.hao.aop.Target.save())"></aop:around>
</aop:aspect>
</aop:config>
</beans>

结果:


切点表达式的抽取,为了方便以后更改

<?xml version="1.0" encoding="UTF-8"?>
<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"
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"> <!-- 配置目标对象-->
<bean id="target" class="com.hao.aop.Target"/> <!-- 切面对象-->
<bean id="aspect" class="com.hao.aop.MyAspect"></bean>
<!-- 配置织入:告诉spring框架哪些方法需要进行哪些增强(前置增强、后置增强)-->
<aop:config>
<!-- 声明切面 切面=切点+通知-->
<aop:aspect ref="aspect">
<!-- 抽取切入点表达式-->
<aop:pointcut id="myPointcut" expression="execution(public void com.hao.aop.Target.save())"></aop:pointcut>
<aop:before method="before" pointcut-ref="myPointcut"></aop:before>
<aop:after method="afterReturning" pointcut-ref="myPointcut"></aop:after>
<aop:around method="around" pointcut-ref="myPointcut"></aop:around>
</aop:aspect>
</aop:config>
</beans>

spring-xml实现aop-通知的种类的更多相关文章

  1. Spring学习(十五)----- Spring AOP通知实例 – Advice

    Spring AOP(面向方面编程)框架,用于在模块化方面的横切关注点.简单得说,它只是一个拦截器拦截一些过程,例如,当一个方法执行,Spring AOP 可以劫持一个执行的方法,在方法执行之前或之后 ...

  2. Spring AOP通知实例 – Advice

    Spring AOP(面向方面编程)框架,用于在模块化方面的横切关注点.简单得说,它只是一个拦截器拦截一些过程,例如,当一个方法执行,Spring AOP 可以劫持一个执行的方法,在方法执行之前或之后 ...

  3. spring中的AOP 以及各种通知 配置

    理解了前面动态代理对象的原理之后,其实还是有很多不足之处,因为如果在项目中有20多个类,每个类有100多个方法都需要判断是不是要开事务,那么方法调用那里会相当麻烦. spring中的AOP很好地解决了 ...

  4. Spring基础——AOP通知

    spring(AOP通知) 切面 切面是封装通用业务逻辑的组件,可以作用到其他组件上.是spring组件中的某个方法.无返回类型.参数类型与通知类型有关.一个切面 开启数据库 关闭数据库 开启事务 检 ...

  5. Spring实战(十一) 在Spring XML中配置AOP

    如果你没有源码,不能为通知类添加注解,又不想将AspectJ注解放入到你的代码中,必须选择XML配置了. 1.Spring XML配置文件 解析参考:http://www.cnblogs.com/bi ...

  6. spring的基于xml的AOP配置案例和切入点表达式的一些写法

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  7. Spring中基于xml的AOP

    1.Aop 全程是Aspect Oriented Programming 即面向切面编程,通过预编译方式和运行期动态代理实现程序功能的同一维护的一种技术.Aop是oop的延续,是软件开发中的 一个热点 ...

  8. 8 -- 深入使用Spring -- 4...6 AOP代理:基于注解的XML配置文件的管理方式

    8.4.6 基于XML配置文件的管理方式 Spring 2.x 提供一个新的aop:命名空间来定义切面.切入点和增强处理. XML配置方式优点: ⊙ 如果应用没有使用JDK 1.5 以上版本,那么应用 ...

  9. 浅谈Spring AOP 面向切面编程 最通俗易懂的画图理解AOP、AOP通知执行顺序~

    简介 我们都知道,Spring 框架作为后端主流框架之一,最有特点的三部分就是IOC控制反转.依赖注入.以及AOP切面.当然AOP作为一个Spring 的重要组成模块,当然IOC是不依赖于Spring ...

  10. Spring 中基于 AOP 的 XML架构

    Spring 中基于 AOP 的 XML架构 为了使用 aop 命名空间标签,你需要导入 spring-aop j架构,如下所述: <?xml version="1.0" e ...

随机推荐

  1. linux下更改文件字符格式为uft-8

    liunx下发布的.net Core 程序,发现短信签名不错误不能发出.后来检查发现配配文件中的字符为乱码才知道是因为字符格式问题. 因为服务器批较多,还是使用命令来解决比较快.使用iconv来更改. ...

  2. Linux卸载源码编译安装的软件

    使用auto-apt 和 checkinstall,具体命令如下 #安装auto-apt和checkinstall apt install auto-apt checkinstall #在源码目录中 ...

  3. python 程序小练习

    print("Type integers,each followed by Enter; or just Enter to finish") total = 0 count = 0 ...

  4. Linux下查看端口占用进程号,程序名的方法

    Linux下查看端口占用进程号,程序名的方法,方便我们查找什么进程导致系统变慢等需要.linux下查看端口占用情况: 1. 查看哪个进程占用了819端口: case9-sghfofo:/usr/loc ...

  5. 超详细maven的卸载、重新安装与配置

    镜像下载.域名解析.时间同步请点击 阿里巴巴开源镜像站 一.maven的卸载 maven在使用时只是配置了环境变量和本地仓库,我们只需要删除本地仓库,在环境变量中移除maven的环境变量. 1.删除解 ...

  6. Django中ORM对数据库的增删改查

    Django中ORM对数据库数据的增删改查 模板语言 {% for line in press %} {% line.name %} {% endfor %} {% if 条件 %}{% else % ...

  7. 后端跨域问题究极解决 nginx+springboot 解决OPTIONS通过却报CORS的问题

    location /joinus { # 允许跨域请求的"域",有些请求不允许* add_header 'Access-Control-Allow-Origin' $http_or ...

  8. 利用MSSQL getshell

    此次复现使用的sql server 2000 和sql server 2008两个环境进行的 是在已知数据库密码的基础上进行的 0x01 MSSQL连接 连接MSSQL 2000 新建连接: 填写目的 ...

  9. ubuntu开启emqx/nginx/uwsgi自启动服务

    一.emqx开机自启 a.首先在执行如下命令  vi /lib/systemd/system/emqx.service 创建了emqx.service文件然后在文件中写入如下内容 [Unit] Des ...

  10. 解释基于XML Schema方式的切面实现?

    在这种情况下,切面由常规类以及基于XML的配置实现.