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

上代码:

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. Nginx反向代理的模拟

    CentOS起两台tomcat,端口分别是8080和8081! 1. nginx配置文件:nginx.conf upstream tomcats{ server 192.168.198.128:808 ...

  2. eclipse中的Console控制台视图脱离主窗口解决办法

    问题:Console控制台视图由于操作不当,跑出来了,脱离了主窗口 解决:在eclipse主窗口最上面的工具条选项中,找到Window,点击里面的Reset Perspective,即可,这样视图就重 ...

  3. centos shell编程6一些工作中实践脚本 nagios监控脚本 自定义zabbix脚本 mysql备份脚本 zabbix错误日志 直接送给bc做计算 gzip innobackupex/Xtrabackup 第四十节课

    centos   shell编程6一些工作中实践脚本   nagios监控脚本 自定义zabbix脚本 mysql备份脚本 zabbix错误日志  直接送给bc做计算  gzip  innobacku ...

  4. MPI简介

    什么是MPI: MPI是一个库,而不是一门语言.但是按照并行语言的分类,可以把FORTRAN+MPI或者C+MPI看作是一种在原来串行语言基础上扩展后得到的并行语言.MPI库可以被FORTRAN77/ ...

  5. [BS-22] Objective-C中nil、Nil、NULL、NSNull的区别

    Objective-C中nil.Nil.NULL.NSNull的区别 1.定义: nil:      OC语言定义:#define nil __DARWIN_NULL   /  #define __D ...

  6. [BS-16] 尽量将View的Opaque属性设置为YES(默认就是YES)

    尽量将View的Opaque属性设置为YES(默认就是YES) UIView控件都有一个Opaque属性,如果不会更改view的透明度,那么应该将其opaque属性设置为YES.为什么要这样做呢?其实 ...

  7. android PopupWindow实现从底部弹出或滑出选择菜单或窗口

    本实例弹出窗口主要是继承PopupWindow类来实现的弹出窗体,布局可以根据自己定义设计.弹出效果主要使用了translate和alpha样式实现,具体实习如下: 第一步:设计弹出窗口xml: &l ...

  8. Android --设置Toast消失时间

    参考博客:Android开发,Toast重复显示(显示时间过长)解决方法 package com.dr.app.drseamoniter.toast; import android.content.C ...

  9. Java Collections的排序之二

    package test.list; import java.util.ArrayList; import java.util.Collections; import java.util.HashSe ...

  10. vmware vcenter appliance dhcp 改为 静态IP导致web service认证失败

    参考 http://www.davidhill.co/2012/09/failed-to-connect-to-vmware-lookup-service/ Failed to connect to ...