spring aop的注解方式:和xml的配置方式略有区别,详细如下:

1、首先还是建立需要的切面类:切面类里面定义好切点配置,以及所有的需要实现的通知方法。

/**
*
*/
package com.lilin.maven.service.annotationaop; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Service; /**
* @author lilin
*
*/
@Aspect
@Service
public class AspectAop { /**
* 申明切点,同时配置将要被aop过滤的业务类
*/
@Pointcut("execution (* com.lilin.maven.service.annotationaop.UserService.addUser(..))")
public void pointcut() {
} @Before("pointcut()")
public void doBefore() {
System.out.println("doBefore advice");
} @AfterReturning("pointcut()")
public void doAfterReturning() {
System.out.println("doAfterReturning advice");
} @After("pointcut()")
public void doAfter() {
System.out.println("doAfter advice");
} @AfterThrowing("pointcut()")
public void doAfterThrowing() {
System.out.println("doAfterThrowing advice");
} @Around("pointcut()")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("doAround advice start");
Object result = pjp.proceed();
System.out.println("doAround advice end");
return result;
} }

2、在spring的配置文件中,开启注解的扫描:

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置注解扫面路径 -->
<context:component-scan base-package="com.lilin" />
<!-- 开启注解 -->
<context:annotation-config />
<!-- 开启aspectj代理 -->
<aop:aspectj-autoproxy />
</beans>

3、建立业务的接口和类,方便aop的过滤测试。

/**
*
*/
package com.lilin.maven.service.annotationaop; /**
* @author lilin
*
*/
public interface IUserService {
void addUser();
}
/**
*
*/
package com.lilin.maven.service.annotationaop; import org.springframework.stereotype.Service; /**
* @author lilin
*
*/
@Service
public class UserService implements IUserService {
@Override
public void addUser() {
System.out.println("增加用户信息");
     //这个异常信息的抛出,是为了测试after throwing的advice的
throw new RuntimeException("测试异常");
} }

4、还是像xml配置的时候类似,建立testNG的测试类:继承的baseTest 和xml配置的中一样,请参见上一篇xml配置中的baseTest

/**
*
*/
package com.lilin.maven.service.annotationaop; import javax.annotation.Resource; import org.springframework.test.context.ContextConfiguration;
import org.testng.annotations.Test; import com.lilin.maven.service.BaseTest; /**
* @author lilin
*
*/
@ContextConfiguration(locations = { "classpath:/config/spring/spring-aopannotation.xml" })
public class AopAnnotationTest extends BaseTest { @Resource
private IUserService userService; @Test
public void aopAnnotationTest() {
userService.addUser();
} }

5、运行测试方法,可以得到注解方式的aop配置已经起到作用:

正常的测试结果如下:

2016-1-29 15:25:09 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [config/spring/spring-aopannotation.xml]
2016-1-29 15:25:09 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.GenericApplicationContext@10f6d3: startup date [Fri Jan 29 15:25:09 CST 2016]; root of context hierarchy
2016-1-29 15:25:09 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@10721b0: defining beans [aspectAop,userService,light,lightOnCommand,remoteControl,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
doAround advice start
doBefore advice
增加用户信息
doAround advice end
doAfter advice
doAfterReturning advice
PASSED: aopAnnotationTest ===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================

@AfterThrowing 的测试结果如下,测试这个,请手动在业务方法里面抛出异常信息:

2016-1-29 15:26:50 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [config/spring/spring-aopannotation.xml]
2016-1-29 15:26:50 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.GenericApplicationContext@10f6d3: startup date [Fri Jan 29 15:26:50 CST 2016]; root of context hierarchy
2016-1-29 15:26:50 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@10721b0: defining beans [aspectAop,userService,light,lightOnCommand,remoteControl,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
doAround advice start
doBefore advice
增加用户信息
doAfter advice
doAfterThrowing advice
FAILED: aopAnnotationTest
java.lang.RuntimeException: 测试异常
at com.lilin.maven.service.annotationaop.UserService.addUser(UserService.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

好了,到此,spring aop的注解方式的实现,一个简单的demo就o了。

spring aop 使用注解方式总结的更多相关文章

  1. spring AOP自定义注解方式实现日志管理

    今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在appli ...

  2. Spring AOP的注解方式实现

    spring也支持注解方式实现AOP,相对于配置文件方式,注解配置更加的轻量级,配置.修改更加方便. 1.开启AOP的注解配置方式 <!-- 开启aop属性注解 --> <aop:a ...

  3. Spring AOP(二)--注解方式

    本文介绍通过注解@AspectJ实现Spring AOP,这里要重点说明一下这种方式实现时所需的包,因为Aspect是第三方提供的,不包含在spring中,所以不能只导入spring-aop的包,为了 ...

  4. perf4j+spring+aop 配置 注解方式

    今天将perf4j基于spring aop方式进入了接入,接入方法还是比较简单.具体配置如下: logback.xml <!--perf4j配置--> <appender name= ...

  5. Spring AOP(注解方式)

    配置文件: xmlns:aop="http://www.springframework.org/schema/aop" http://www.springframework.org ...

  6. 利用Spring AOP自定义注解解决日志和签名校验

    转载:http://www.cnblogs.com/shipengzhi/articles/2716004.html 一.需解决的问题 部分API有签名参数(signature),Passport首先 ...

  7. 跟着刚哥学习Spring框架--通过注解方式配置Bean(四)

    组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 特定组件包括: 1.@Component:基本注解,识别一个受Spring管理的组件 2.@Resposit ...

  8. spring AOP自定义注解 实现日志管理

    今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在appli ...

  9. (转)利用Spring AOP自定义注解解决日志和签名校验

    一.需解决的问题 部分API有签名参数(signature),Passport首先对签名进行校验,校验通过才会执行实现方法. 第一种实现方式(Origin):在需要签名校验的接口里写校验的代码,例如: ...

随机推荐

  1. MXNet官网案例分析--Train MLP on MNIST

    本文是MXNet的官网案例: Train MLP on MNIST. MXNet所有的模块如下图所示: 第一步: 准备数据 从下面程序可以看出,MXNet里面的数据是一个4维NDArray. impo ...

  2. 慕课网-安卓工程师初养成-4-9 Java循环语句之 for

    来源:http://www.imooc.com/code/1425 Java 的循环结构中除了 while 和 do...while 外,还有 for 循环,三种循环可以相互替换. 语法: 执行过程: ...

  3. 返回顶部js

    backToTop.js: (function () { var $backToTopEle = $('<div class="backToTop"></div& ...

  4. Cnetos7下,已经能访问tomcat

    进入/usr/local/apache-tomcat-8.0.24/bin 中 执行:./startup.sh开启tomcat 再执行如下 systemctl stop firewalld.servi ...

  5. rman--增量备份

    1.rman增量备份级别 级别0和全库备份相同,级别1备份执行的是差异备份. 2.增量备份 使用rman实现增量备份的级别0备份 RMAN database; 使用rman实现增量备份的级别1备份 d ...

  6. ASP.NET运行机制原理 ---浏览器与IIS的交互过程 自己学习 网上查了下别人写的总结的很好 就转过来了 和自己写的还好里嘻嘻

    一.浏览器和服务器的交互原理 (一).浏览器和服务器交互的简单描述: 1.通俗描述:我们平时通过浏览器来访问网站,其实就相当于你通过浏览器去访问一台电脑上访问文件一样,只不过浏览器的访问请求是由被访问 ...

  7. Ossim应用体验视频

    Ossim体验视频 近期,我写的有关Ossim应用的系列文章网友们非常关注,这里对大家提出有一些问题我制作了高清的视频和截图发布到网站,以让更多的人了解这款开源安全平台.在年后出版的教程中会详细讲解o ...

  8. SQL 1:常用SQL语句

    导读:最近写代码,几乎是天天泡在SQL语句里,各种代码各种写.但一直缺少总结,要不就是觉得简单,要不就是觉得大家都知道.想来,我还是没能明白总结的价值在哪里.现在也就写写最近都常写的一些语句. 一,i ...

  9. CSS3 Filter

    Filters主要是运用在图片上,以实现一些特效.(尽管他们也能运用于video上),不过我们在些只来讨论图片上的运用. 语法: elm { filter: none | <filter-fun ...

  10. JS常用的设计模式(16)—— 享元模式

    享元模式主要用来减少程序所需的对象个数. 有一个例子, 我们这边的前端同学几乎人手一本<JavaScript权威指南>. 从省钱的角度讲, 大约三本就够了. 放在部门的书柜里, 谁需要看的 ...