Spring基于XML实现Aop

网友投稿 641 2022-12-27

Spring基于XML实现Aop

Spring基于XML实现Aop

目录项目结构具体步骤1、创建maven 项目 导入依赖 创建好项目结构2、写一个TestDao接口 及实现类3、编写切面类测试总结

项目结构

具体步骤

1、创建maven 项目 导入依赖 创建好项目结构

org.projectlombok

lombok

1.18.18

junit

junit

4.12

test

org.springframework

spring-context

5.3.4

org.springframework

spring-beans

5.3.4

org.springframework

spring-core

5.3.4

org.springframework

spring-expression

5.3.4

org.springframework

spring-aop

5.3.4

org.aspectj

aspectjweaver

1.9.6

org.aspectj

aspectjrt

1.9.6

2、写一个TestDao接口 及实现类

/**

* @version 1.0

* @author: crush

* @date: 2021-03-05 10:26

*/

public interface TestDao {

public void sayHello();

public void save();

public void modify();

public void delete();

}

/**

* @version 1.0

* @author: crush

* @date: 2021-03-05 10:27

*/

public class TestDaoImpl implements TestDao {

public void sayHello() {

System.out.println("正在执行的方法-->武汉加油zBBwNLSS!中国加油!");

}

public void save() {

System.out.println("正在执行的方法-->保存");

}

public void modify() {

System.out.println("正在执行的方法-->修改");

}

public void delete() {

System.out.println("正在执行的方法-->删除");

}

}

3、编写切面类

/**

* @version 1.0

* @author: crush

* @date: 2021-03-10 17:12

*/

public class MyAspectXml {

/**

* 前置通知 使用JoinPoint 接口作为参数获得目标对象的信息

**/

public void before(JoinPoint jp){

System.out.print("前置通知:模拟权限控制 ");

System.out.println("目标对象:"+jp.getTarget()+",被增强的方法:"+jp.getSignature().getName());

}

public void afterReturning(JoinPoint jp){

System.out.print("后置返回通知:"+"模拟删除临时文件" );

System.out.println(",被增强的方法"+jp.getSignature().getName());

}

public Object around(ProceedingJoinPoint pjp) throws Throwable {

System.out.println("环绕开始:执行目标方法前,模拟开启事务");

Object obj = pjp.prohttp://ceed();

System.out.println("环绕结束:执行目标方法后,模拟关闭事务");

return obj;

}

public void except(Throwable throwable){

System.out.println("异常通知:"+"程序执行异常"+throwable.getMessage());

}

public void after(){

System.out.println("最终通知:释放资源");

}

}```

### 4、application.xml文件

```xml

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/aop

https://springframework.org/schema/aop/spring-aop.xsd

http://springframework.org/schema/context

https://springframework.org/schema/context/spring-context.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/aop

https://springframework.org/schema/aop/spring-aop.xsd

http://springframework.org/schema/context

https://springframework.org/schema/context/spring-context.xsd">

测试

@Test

public void Test(){

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

TestDao testDao = applicationContext.getBean("testDaoImpl", TestDaoImpl.class);

testDao.save();

/**

* 输出:

* 前置通知:模拟权限控制 目标对象:com.dao.TestDaoImpl@704f1591,被增强的方法:save

* 环绕开始:执行目标方法前,模拟开启事务

* 正在执行的方法-->保存

* 环绕结束:执行目标方法后,模拟关闭事务

* 后置返回通知:模拟删除临时文件,被增强的方法save

* 最终通知:释放资源

*/

}

总结

本篇文章就到这里了,希望能给你带来帮助,也希望能够您能够关注我们的更多内容!

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:flutter占位(float占位符)
下一篇:Spring Cloud 如何保证微服务内安全
相关文章

 发表评论

暂时没有评论,来抢沙发吧~