【Spring-AOP-学习笔记-4】@After后向增强处理简单示例
2024-08-30 02:44:31
说明
After增强处理的作用非常类似于异常处理中的finally块的作用,无论如何,他总会在方法执行结束之后被织入,因此特别适应于垃圾回收。
项目结构
程序
@Component("hello")
public class HelloImpl implements Hello
{
// 定义一个简单方法,模拟应用中的业务逻辑方法
public void foo()
{
System.out.println("执行Hello组件的foo()方法");
}
// 定义一个addUser()方法,模拟应用中的添加用户的方法
public int addUser(String name , String pass)
{
System.out.println("执行Hello组件的addUser添加用户:" + name);
if(name.length() < 3 || name.length() > 10)
{
throw new IllegalArgumentException("name参数的长度必须大于3,小于10!");
}
return 20;
}
}
@Component("world")
public class WorldImpl implements World
{
// 定义一个简单方法,模拟应用中的业务逻辑方法
public void bar()
{
System.out.println("执行World组件的bar()方法");
}
}
定义切面
// 定义一个切面
@Aspect
public class ReleaseAspect
{
// 匹配org.crazyit.app.service包下所有类的、
// 所有方法的执行作为切入点
@After("execution(* org.crazyit.app.service.*.*(..))")
public void release()
{
System.out.println("模拟方法结束后的释放资源...");
}
}
配置
<?xml version="1.0" encoding="GBK"?>
<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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<!-- 指定自动搜索Bean组件、自动搜索切面类 -->
<context:component-scan base-package="org.crazyit.app.service
,org.crazyit.app.aspect">
<context:include-filter type="annotation"
expression="org.aspectj.lang.annotation.Aspect"/>
</context:component-scan>
<!-- 启动@AspectJ支持 -->
<aop:aspectj-autoproxy/>
</beans>
测试
public class BeanTest
{
public static void main(String[] args)
{
// 创建Spring容器
ApplicationContext ctx = new
ClassPathXmlApplicationContext("beans.xml");
Hello hello = ctx.getBean("hello" , Hello.class);
hello.foo();
hello.addUser("孙悟空" , "7788");
World world = ctx.getBean("world" , World.class);
world.bar();
}
}
链接:
《@AfterThrowing增强处理简单示例》http://www.cnblogs.com/ssslinppp/p/4633595.html
《@AfterReturning增强处理简单示例》http://www.cnblogs.com/ssslinppp/p/4633496.html
《@After后向增强处理简单示例》http://www.cnblogs.com/ssslinppp/p/4633427.html
《@Before前向增强处理简单示例》 http://www.cnblogs.com/ssslinppp/default.html?page=7
附件列表
最新文章
- iOS设计模式
- 获取JAVA[WEB]项目相关路径的几种方法
- 8.9 CSS知识点2
- atomic, spinlock and mutex性能比较
- CSS之Document方法的使用
- Swift: 继承
- 输入内容, 列出可选的项: QComboBox
- 教你如何使用Java手写一个基于数组实现的队列
- 以前的博客内容迁至CSDN,博客名不变,以后博客将在两个平台同步更新
- 关于Tomcat启动时,长时间停在Initializing Spring root webApplicationContext处的原因
- Spring Cloud(Dalston.SR5)--Config 集群配置中心-加解密
- Python (time、datetime、random、os、sys、shutil)模块的使用
- 吴恩达课后作业学习2-week1-3梯度校验
- JS 强制类型转化
- 在vs.net 2010,2015 等版本,给JS函数添加代码提示\注释
- SpringMVC(四):什么是HandlerAdapter
- 【bzoj3438】 小M的作物
- POJ 1930 Dead Fraction (循环小数-GCD)
- 预防 app crash 之 unrecognized selector
- [C/C++标准库]_[初级]_[转换UTC时间到local本地时间]
热门文章
- 当前标识没有对“C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files”的写访问权限。
- 猎豹浏览器(chrome内核)屏蔽视频广告
- markdown实例
- postgresql的copy
- DevExpress DateEdit 5 常见问题解决方法
- tp 缓存,前台提速
- .Net Entity Framework Core 用 HasColumnType 配置浮点数精度
- idea中看不到项目结构该怎么办
- torch7 安装 并安装 hdf5模块 torch模块 nn模块 (系统平台为 ubuntu18.04 版本)
- CAN中如何计算波特率并配置波特率