SpringAop的注解形式
来源:互联网 发布:linux ntp客户端 编辑:程序博客网 时间:2024/05/18 23:56
1.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: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-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
<!-- 开启自动注解,自动代理1.切面 2.切点expression=""-->
<aop:aspectj-autoproxy/>
<!-- 实例化 -->
<bean name="userinfoService" class="test.UserinfoService"></bean>
<bean name="userinfoProxy" class="test.UserinfoProxy"></bean>
</beans>
----------------------------------------------------------
2.UserinfoService.java:
package test;
public class UserinfoService {
// 一个添加方法
public void save(){
System.out.println("这是一个ORM框架做的添加方法 ");
}
//一个修改方法
public void update(){
System.out.println("这是一个ORM框架做的修改方法 ");
}
}
---------------------------------------------------------------
UserinfoProxy.java:
package test;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
//切面aspect
@Aspect
public class UserinfoProxy {
//通知=方法
//before通知
// @Before("execution(* test.UserinfoService.save(..))")
// public void before(){
// System.out.println("我会在调用方法前");
// }
//after通知
// @After("execution(* test.UserinfoService.save(..))")
// public void after(){
// System.out.println("我会在调用方法后");
// }
//环绕通知
@Around("execution(* test.UserinfoService.*(..))")
public Object around(ProceedingJoinPoint point){
if(point.getSignature().getName().equals("save")){//放行
try {
return point.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
}
return null;//阻止
}
}
-----------------------------------------------------------------
3.测试文件:Test.java:
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
UserinfoService service = (UserinfoService) ctx.getBean("userinfoService");
//service.save();
service.update();
}
}
<?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: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-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
<!-- 开启自动注解,自动代理1.切面 2.切点expression=""-->
<aop:aspectj-autoproxy/>
<!-- 实例化 -->
<bean name="userinfoService" class="test.UserinfoService"></bean>
<bean name="userinfoProxy" class="test.UserinfoProxy"></bean>
</beans>
----------------------------------------------------------
2.UserinfoService.java:
package test;
public class UserinfoService {
// 一个添加方法
public void save(){
System.out.println("这是一个ORM框架做的添加方法 ");
}
//一个修改方法
public void update(){
System.out.println("这是一个ORM框架做的修改方法 ");
}
}
---------------------------------------------------------------
UserinfoProxy.java:
package test;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
//切面aspect
@Aspect
public class UserinfoProxy {
//通知=方法
//before通知
// @Before("execution(* test.UserinfoService.save(..))")
// public void before(){
// System.out.println("我会在调用方法前");
// }
//after通知
// @After("execution(* test.UserinfoService.save(..))")
// public void after(){
// System.out.println("我会在调用方法后");
// }
//环绕通知
@Around("execution(* test.UserinfoService.*(..))")
public Object around(ProceedingJoinPoint point){
if(point.getSignature().getName().equals("save")){//放行
try {
return point.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
}
return null;//阻止
}
}
-----------------------------------------------------------------
3.测试文件:Test.java:
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
UserinfoService service = (UserinfoService) ctx.getBean("userinfoService");
//service.save();
service.update();
}
}
0 0
- SpringAop的注解形式
- SpringAop事务注解形式
- 基于注解的SpringAOP
- springAOP的注解
- springAOP的注解版
- SpringAop的xml文件形式
- 基于注解的SpringAOP Redis缓存技术
- SpringAOP +log4J+注解的 日志管理
- Spring的注解形式
- Spring的注解形式
- 注解形式的定时器
- springAOP (注解方式)
- springAOP 注解实现
- SpringAOP实现自定义注解
- SpringAop注解实现
- SpringAOP注解应用
- springAop基于注解配置
- SpringAop事务xml文件形式
- Redis 持久化(persistence)
- 国王的烦恼 蓝桥杯(最小生成树 kru)
- leetcode第7题,Reverse Integer
- [LeetCode]452. Minimum Number of Arrows to Burst Balloons
- SpringAop的xml文件形式
- SpringAop的注解形式
- Redis 命令参考
- SpringAop事务xml文件形式
- [机器学习]详解分类算法--决策树算法
- codevs 4511 信息传递 NOIP2015 day1 T2
- 面试总结之23种Java设计模式
- pat 1112. Stucked Keyboard (20)
- NYOJ168 第三届acm省赛 房间安排
- SpringAop事务注解形式