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

上代码:

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://www.cnblogs.com/littlemonk/p/5705476.html

    http://www.cnblogs.com/littlemonk/p/5705476.html

  2. Java Main Differences between Java and C++

    转载自:http://www.cnblogs.com/springfor/p/4036739.html C++ supports pointers whereas Java does not. But ...

  3. Interview Return Products of All Other Elements in Array

    这是一道面试亚马逊时的题目,要求Time O(n). 我刚开始想的是算出所有的数的总product,再去除以对应位置的元素,但这种做法的问题是若该位置为0,就会报错. 到网上搜了下,才知道,原来有这种 ...

  4. 官方教程:教你用好微信JS-SDK接口

    微信开放JS-SDK接口,开发者和行业用户可谓是欢欣鼓舞.奔走相告,目测将激起一大波第三方开发商的创新产品!真真是H5开发者的利好!但也有用户表示,还不了解JS-SDK接口到底是啥,究竟怎么用.现在, ...

  5. SQL Server中的SQL语句优化与效率问题

    很多人不知道SQL语句在SQL SERVER中是如何执行的,他们担心自己所写的SQL语句会被SQL SERVER误解.比如: select * from table1 where name='zhan ...

  6. 第五篇 Integration Services:增量加载-Deleting Rows

    本篇文章是Integration Services系列的第五篇,详细内容请参考原文. 在上一篇你学习了如何将更新从源传送到目标.你同样学习了使用基于集合的更新优化这项功能.回顾增量加载记住,在SSIS ...

  7. 关于StdAfx.h和StdAfx.cpp

    http://www.cplusplus.com/articles/2z86b7Xj/ https://msdn.microsoft.com/en-us/library/h552b3ca.aspx h ...

  8. cocos2dx 3.x(精灵的碰撞检测,点击移动与拖动精灵)

    // // MainScene.hpp // helloworld // // Created by apple on 16/9/19. // // #ifndef MainScene_hpp #de ...

  9. Silverlight验证相关

    asp.net mvc中有验证,当然在silverlight中也包含有验证规则的但这个就离不开mvvm(其实就是实体层,页面这些东西的组成,没有用过呀,悲哀!连这个概念都理解不了). 关于MVVM验证 ...

  10. M面经Prepare: Delete Words Starting With One Character

    给定一个char array, 这个array是一个句子,然后给定一个字母,把这个array里面带有这个字母开头的单次删掉,操作是要求in place. 检测   array[i]==' ' & ...