spring Aop的一个demo
面向切面是什么我就不说了.
上代码:
package com.foreveross.service.weixin.test; import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; @Documented
@Retention(value=RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Action {
String name();
}
package com.foreveross.service.weixin.test; import org.springframework.stereotype.Service; @Service
public class DemoService { @Action(name="注解拦截操作...,这个是add操作...")
public void addDemo(){ }
}
package com.foreveross.service.weixin.test; import org.springframework.stereotype.Service; @Service
public class Demo1Service {
@Action(name="addDemo1的日志")
public void addDemo1(){ }
}
package com.foreveross.service.weixin.test; import java.lang.reflect.Method; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component; @Aspect
@Component
public class LogAspect { @Pointcut("@annotation(com.foreveross.service.weixin.test.Action)")
public void annotationPointCut(){} @Before("execution(* com.foreveross.service.weixin.test.*.*(..))")
public void before(JoinPoint joinPoint){
MethodSignature signature=(MethodSignature)joinPoint.getSignature();
Method method=signature.getMethod();
System.out.println("方法规则式拦截:"+method.getName());
}
@After("annotationPointCut()")
public void after(JoinPoint joinPoint){
MethodSignature signature=(MethodSignature)joinPoint.getSignature();
Method method=signature.getMethod();
Action action=method.getAnnotation(Action.class);
System.out.println("注解式拦截..."+action.name());
}
}
package com.foreveross.service.weixin.test; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy; @Configuration
@ComponentScan("com.foreveross.service.weixin.test")
@EnableAspectJAutoProxy//注解开启Spring对AspectJ的支持
public class AppConfig { }
package com.foreveross.service.weixin.test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Test { public static void main(String[] args) {
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(AppConfig.class);
DemoService demo=context.getBean(DemoService.class);
Demo1Service demo1=context.getBean(Demo1Service.class);
demo.addDemo();
demo1.addDemo1();
context.close();
}
}
spring Aop的一个demo的更多相关文章
- spring aop 的一个demo(未完,待完善)
假设我们有这样的一个场景 : 对于一个类的众多方法,有些方法需要从缓存读取数据,有些则需要直接从数据库读取数据.怎样实现呢? 实现方案有多种.下面我说下常见的几种实现方案 : 1.直接采用spring ...
- 运用Spring Aop,一个注解实现日志记录
运用Spring Aop,一个注解实现日志记录 1. 介绍 我们都知道Spring框架的两大特性分别是 IOC (控制反转)和 AOP (面向切面),这个是每一个Spring学习视频里面一开始都会提到 ...
- Spring AOP应用实例demo
AOP(Aspect-Oriented Programming.面向方面编程).能够说是OOP(Object-OrientedPrograming.面向对象编程)的补充和完好.OOP引入封装.继承和多 ...
- spring aop 的一个思考
问题: spring aop 默认使用jdk代理织入. 也就是我们常这样配置:<aop:aspectj-autoproxy /> 通过aop命名空间的<aop:aspectj-au ...
- SSH框架系列:Spring AOP应用记录日志Demo
分类: [java]2013-12-10 18:53 724人阅读 评论(0) 收藏 举报 1.简介 Spring 中的AOP为Aspect Oriented Programming的缩写,面向切面编 ...
- Spring aop 小例子demo
由于最近的服务项目提供接口有一个需求,所有操作都必须检查操作的服务可用,所以感觉Aop特别适合实施.完成学习的小例子. 关于spring-Aop原理:http://m.oschina.net/blog ...
- Spring AOP的一个简单实现
针对学习笔记(六)中的购买以及退货代码,我们加入AOP框架,实现同样一个功能. 首先配置XML:service采用和之前一样的代码,只是没有通过实现接口来实现,而是直接一个实现类.transactio ...
- Spring Boot 第一个demo
Sring boot 一直没有使用过,跳槽来到新公司,暂时没有事情就学习一下. Spring boot 这里采用的是maven 来创建的 maven项目的pom.xml 文件 <?xml v ...
- Spring AOP的一个比喻和IOC的作用
aop切面编程就是在常规的执行java类中方法前或执行后加入自定义的方法.比如你本来每天都去打酱油,去,打酱油,回.现在我每天在你打酱油路上等着,你去打酱油的时候我打你一顿,回来的时候给你点糖果吃.你 ...
随机推荐
- window.location.href和window.open的几种用法和区别
使用js的同学一定知道js的location.href的作用是什么,但是在js中关于location.href的用法究竟有哪几种,究竟有哪些区别,估计很多人都不知道了. 一.location.href ...
- 第六篇 SQL Server代理深入作业步骤工作流
本篇文章是SQL Server代理系列的第六篇,详细内容请参考原文. 正如这一系列的前几篇所述,SQL Server代理作业是由一系列的作业步骤组成,每个步骤由一个独立的类型去执行.每个作业步骤在技术 ...
- python_递归
1. 递归示例 #coding:utf-8 #递归进行阶乘 def mm(num): if(num == 1): return 1 else: return mm(num-1) * num prin ...
- 电量显示Binding Converter MVVM
用一个ProcessBar显示电量,低于20%时候,ForeGround为红色,否则为绿色, 页面使用了MVVM绑定到了ViewModel, ProcessBar XAML为 <Progress ...
- CAD出图
今天帮着客户输出图纸,用打印命令,设置打印参数,选择dwg到pdf打印机.设置图纸大小等参数 CAD满图纸输出 按照比例尺输出 plot,一般图纸绘制是已经有一个比例尺,所以按照1:1输出,如果图纸是 ...
- java web工程读取及修改配置文件
这篇博客比自己讲解的详细: http://blog.sina.com.cn/s/blog_69398ed9010191jg.html 使用方法: 1)配置文件在web-info的class目录下,或者 ...
- iOS自动偏移64个像素
自从iOS7开始,如果添加的scrollview是uiviewController第一个视图,系统会默认自动添加-64的偏移量,所以规避的方案就添加一个UIView之后再添加你的scrollview.
- weka特征选择(IG、chi-square)
一.说明 IG是information gain 的缩写,中文名称是信息增益,是选择特征的一个很有效的方法(特别是在使用svm分类时).这里不做详细介绍,有兴趣的可以googling一下. chi-s ...
- C++Primer 第十一章
//1.关键容器支持高效的关键字查找和访问. map 关联数组:保存关键字-值对.通过关键字来查找值. set 关键字即值,即只保存关键字的容器. multimap 关键字可重复出现的map mult ...
- IOS Suppot Font 苹果默认支持的字体一览2(普通,加粗,倾斜)