Spring AOP - AspectJ - @After example
来源:互联网 发布:网易足球数据库 编辑:程序博客网 时间:2024/05/21 23:01
Last few pages talked about spring advices (before advice, after return advice, around advice and pointcut examples). In this page you will get an idea about how to integrate AspectJ annotations with Spring AOP framework. Using spring AOP and AspectJ, you can easily intercept methods.
AspectJ comes with below annotations:
1) @Before
2) @After
3) @AfterReturning
4) @AfterThrowing
5) @Around
In this page we will see an example for @After
annotation. @After
annotation intercepts method after calling it.
pom.xml
file gives all required dependencies: Add dependencies for spring-aop jar, aspectjrt jar and aspectjweaver jar file.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>SpringJavaBasedConfig</groupId> <artifactId>SpringJavaBasedConfig</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <spring.version>3.2.0.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.8.0</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.0</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>3.1</version> </dependency> </dependencies></project>
My business logic service class:
package com.java2novice.bean;public class MyBusinessService { public void runMyBusinessLogic(){ System.out.println("************************************"); System.out.println("Running business logic..."); System.out.println("************************************"); } public void testThrowException() { throw new NullPointerException(); }}
Here is the Aspect class example. The after()
method will be executed after calling the method.
package com.java2novice.aop;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.Aspect;@Aspectpublic class RunAfterExecution { @After("execution(* com.java2novice.bean.MyBusinessService.runMyBusinessLogic(..))") public void runAfter(JoinPoint joinPoint) throws Throwable { System.out.println("Inside RunAfterExecution.afterReturning() method..."); System.out.println("inserted after : " + joinPoint.getSignature().getName()); }}
Here is the xml based configuration file. By adding “<aop:aspectj-autoproxy />
” tag, you can enable AspectJ with in your application. Add bean definitions for your normal bean and your Aspect.
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <aop:aspectj-autoproxy /> <bean id="busService" class="com.java2novice.bean.MyBusinessService" /> <bean id="beforeAspectBean" class="com.java2novice.aop.RunAfterExecution" /> </beans>
Here is the final demo class:
package com.java2novice.test;import org.springframework.context.ConfigurableApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.java2novice.bean.MyBusinessService;public class SpringDemo { public static void main(String a[]){ String confFile = "applicationContext.xml"; ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(confFile); MyBusinessService busServ = (MyBusinessService) context.getBean("busService"); busServ.runMyBusinessLogic(); }}
Output:
************************************Running business logic...************************************Inside RunAfterExecution.afterReturning() method...inserted after : runMyBusinessLogic
- Spring AOP - AspectJ - @After example
- Spring AOP + AspectJ Annotation Example
- Spring AOP + AspectJ Annotation Example
- Spring AOP + AspectJ Annotation Example
- spring aop + aspectj annotation example
- Spring AOP + AspectJ annotation example
- Spring AOP + AspectJ annotation example
- Spring AOP + AspectJ annotation example
- Spring AOP - AspectJ - @Before example
- Spring AOP - AspectJ - @AfterReturning example
- Spring AOP - AspectJ - @AfterThrowing example
- Spring AOP + AspectJ in XML configuration example
- Spring AOP + AspectJ in XML configuration example
- Spring AOP Advices - After returning advice example - xml based configuration
- Spring AOP Advices - After throwing advice example - xml based configuration
- Spring AOP + AspectJ framework
- Spring AOP与Aspectj
- spring AspectJ aop学习
- 改变HyperlinkButton的下划线(Silverlight)
- GNU hello学习笔记(1)——autoconf和automake
- Android Annotations开源框架简介(一)
- Angular应用技巧
- Windows下搭建IOS开发环境(一)
- Spring AOP - AspectJ - @After example
- Altium Designer 加LOGO方法
- ios学习笔记——UITextField常用属性及设置
- 【设置】PotPlayer播放完毕后停止
- Leetcode61: Implement Queue using Stacks
- Windows下PyInstaller的使用教程
- Linux中的分页机制和虚拟机中的EPT机制
- 核心Javascript教程(二)
- JAVA 常用API String 对象 学习笔记