spring aop 使用注解方式总结
2016-01-29 15:30
513 查看
spring aop的注解方式:和xml的配置方式略有区别,详细如下:
1、首先还是建立需要的切面类:切面类里面定义好切点配置,以及所有的需要实现的通知方法。
2、在spring的配置文件中,开启注解的扫描:
3、建立业务的接口和类,方便aop的过滤测试。
4、还是像xml配置的时候类似,建立testNG的测试类:继承的baseTest 和xml配置的中一样,请参见上一篇xml配置中的baseTest
5、运行测试方法,可以得到注解方式的aop配置已经起到作用:
正常的测试结果如下:
@AfterThrowing 的测试结果如下,测试这个,请手动在业务方法里面抛出异常信息:
好了,到此,spring aop的注解方式的实现,一个简单的demo就o了。
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了。
相关文章推荐
- java spring(1) HelloWorld
- java处理Date类型的方向
- Eclipse 打开时“发现了以元素'd:skin'”开头的无效内容。此处不应含有子元素
- java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive
- Java 序列化 (Serializable) 的作用
- 从头认识Spring-1.3 Spring的容器与Bean
- Java面向对象的基本特征
- 非典型 JavaMail 邮件附件名乱码问题
- 使用Spring,Mabatis框架update返回值问题?
- mac下安装mysql,myeclipse for retina
- Java 字节流、字符流转换
- eclipse创建service服务
- MyEclipse 中各种 libraries 的含义
- MyEclipse2015破解版_MyEclipse 2015 stable 2.0 稳定版 破解日志
- JBoss7.1.1+Spring4+Hibernate4分布式系统的配置
- java websocket
- spring项目执行dao.update等更新操作失败
- netbeans 8.0.2 问题或做不到的事
- java websocket网页端客户端实现
- java、js之间使用AES加密通信数据