面向切面是什么我就不说了.

上代码:

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的更多相关文章

  1. spring aop 的一个demo(未完,待完善)

    假设我们有这样的一个场景 : 对于一个类的众多方法,有些方法需要从缓存读取数据,有些则需要直接从数据库读取数据.怎样实现呢? 实现方案有多种.下面我说下常见的几种实现方案 : 1.直接采用spring ...

  2. 运用Spring Aop,一个注解实现日志记录

    运用Spring Aop,一个注解实现日志记录 1. 介绍 我们都知道Spring框架的两大特性分别是 IOC (控制反转)和 AOP (面向切面),这个是每一个Spring学习视频里面一开始都会提到 ...

  3. Spring AOP应用实例demo

    AOP(Aspect-Oriented Programming.面向方面编程).能够说是OOP(Object-OrientedPrograming.面向对象编程)的补充和完好.OOP引入封装.继承和多 ...

  4. spring aop 的一个思考

    问题: spring  aop 默认使用jdk代理织入. 也就是我们常这样配置:<aop:aspectj-autoproxy /> 通过aop命名空间的<aop:aspectj-au ...

  5. SSH框架系列:Spring AOP应用记录日志Demo

    分类: [java]2013-12-10 18:53 724人阅读 评论(0) 收藏 举报 1.简介 Spring 中的AOP为Aspect Oriented Programming的缩写,面向切面编 ...

  6. Spring aop 小例子demo

    由于最近的服务项目提供接口有一个需求,所有操作都必须检查操作的服务可用,所以感觉Aop特别适合实施.完成学习的小例子. 关于spring-Aop原理:http://m.oschina.net/blog ...

  7. Spring AOP的一个简单实现

    针对学习笔记(六)中的购买以及退货代码,我们加入AOP框架,实现同样一个功能. 首先配置XML:service采用和之前一样的代码,只是没有通过实现接口来实现,而是直接一个实现类.transactio ...

  8. Spring Boot 第一个demo

    Sring boot  一直没有使用过,跳槽来到新公司,暂时没有事情就学习一下. Spring boot  这里采用的是maven 来创建的 maven项目的pom.xml 文件 <?xml v ...

  9. Spring AOP的一个比喻和IOC的作用

    aop切面编程就是在常规的执行java类中方法前或执行后加入自定义的方法.比如你本来每天都去打酱油,去,打酱油,回.现在我每天在你打酱油路上等着,你去打酱油的时候我打你一顿,回来的时候给你点糖果吃.你 ...

随机推荐

  1. http://tedhacker.top/2016/08/05/Spring%E7%BA%BF%E7%A8%8B%E6%B1%A0%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95/

    http://tedhacker.top/2016/08/05/Spring%E7%BA%BF%E7%A8%8B%E6%B1%A0%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%9 ...

  2. html5头部说明

    <!DOCTYPE html> <!-- 使用 HTML5 doctype,不区分大小写 --><html lang="zh-cmn-Hans"> ...

  3. 多台CentOS服务器时间同步(NTP时间同步)(转)

    1.用ntpdate从时间服务器更新时间 如果你的linux系统根本没有ntpdate这个命令 yum install ntp 安装完了之后,你不要做什么配置,也不需要,直接测试一下 [root@lo ...

  4. ASP.NET IIS设置 Session时间

    1.打开IIS需设置的网站主页 2.打开主页IIS--ASP项目,如下图: 3.设置 会话属性---超时 的值,如下图:

  5. [转载]ArcGIS Engine 中的多线程使用

    ArcGIS Engine 中的多线程使用 原文链接 http://anshien.blog.163.com/blog/static/169966308201082441114173/   一直都想写 ...

  6. redis tcp-backlog配置

    在redis2.8版本中有一个tcp-backlog配置, 说明如下: # TCP listen() backlog.## In high requests-per-second environmen ...

  7. iOS网络协议 HTTP/TCP/IP浅析

    一.TCP/IP协议       话说两台电脑要通讯就必须遵守共同的规则,就好比两个人要沟通就必须使用共同的语言一样.一个只懂英语的人,和一个只懂中文的人由于没有共同的语言(规则)就没办法沟通.两台电 ...

  8. Struts2 用 s:if test 判断属性和字符串相等时 注意双引号和单引号的使用

    字符串N一定要用“”双引号包含,从test的包含则用单引号 ‘ ’,如果相反,则不能正确判断该属性是否与该字符串相等. 正确:<s:if test='activityBean.searchFor ...

  9. linux:问题

    1>.Xshell远程连接linux闲置时间过长会自动中断连接: 2>.在linux环境下乱码: 3>.在linux下面执行mv /bin/ls /root/bin之后执行ls就不能 ...

  10. 不等高cell的tableView界面搭建

    一.搭建界面 1.界面分析 分析界面的层次结构,分析界面应该用什么控件来搭建 2.界面层次结构 分析之后,我们可以把这个界面分为四个模块(topView middleView commentView ...