spring学习之使用AspectJ实现AOP
2017-04-14 00:21
531 查看
Hello.java
package com.aspect.service;
public interface Hello {
public void foo();
public int addUser(String name,String pass);
}
HelloImpl.java
/**
*
*/
package com.aspect.service.impl;
import org.springframework.stereotype.Component;
import com.aspect.service.Hello;
import com.sun.xml.internal.txw2.IllegalAnnotationException;
@Component("hello")
public class HelloImpl implements Hello {
@Override
public void foo() {
System.out.println("exect hello.foo()....");
}
@Override
public int addUser(String name, String pass) {
System.out.println("hello.addUser() begin, the param: " + name + " ," + pass);
if (name.length() < 3 && name.length() >= 10){
throw new IllegalAnnotationException("name参数的长度必须大于3,小于10");
}
return 20;
}
}
World.java
package com.aspect.service;
public interface World {
public void bar();
}
WorldImpl.java
package com.aspect.service.impl;
import org.springframework.stereotype.Component;
import com.aspect.service.World;
@Component("world")
public class WorldImpl implements World {
@Override
public void bar() {
System.out.println("exect bar()...");
}
}
AroundAspect,java
package com.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class AroundAspect {
@Around("execution (* com.aspect.service.impl.*.*(..))")
public Object processAround(ProceedingJoinPoint jp) throws Throwable {
System.out.println("执行目标方法前,模拟开始事务..");
Object[] args = jp.getArgs();
if (args != null && args.length > 1) {
args[0] = "【增加的前缀】" + args[0];
}
Object rvt = jp.proceed(args); // 执行目标方法
System.out.println("执行目标方法后,模拟结束事务.." + "\n\n");
if (rvt != null && rvt instanceof Integer) {
rvt = (Integer) rvt * (Integer) rvt;
}
return rvt;
}
}
AuthAspect,java
package com.aspect;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class AuthAspect {
@Before("execution(* com.aspect.service.impl.*.*(..))")
public void authority(){
System.out.println("模拟执行权限检查方法...");
}
}
DestoryAspect.java
package com.aspect;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class DestoryAspect {
@After("execution(* com.aspect.service.impl.*.*(..))")
public void logAfter() {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(date) + "模拟释放资源方法执行完毕 " );
}
}
LogAspect.java
package com.aspect;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class LogAspect {
@AfterReturning(returning = "rvt", pointcut = "SystemArchitecture.myPointCut()")
public void log(Object rvt) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(date) + "模拟日志调用方法,返回的参数----->>>" + rvt);
}
}
RepairAspect.java
package com.aspect;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class RepairAspect {
@AfterThrowing(throwing = "ex", pointcut = "execution(* com.aspect.service.impl.*.*(..))")
public void doRecoveryAction(Throwable ex) {
System.out.println("目标方法抛出的异常为: " + ex);
System.out.println("模拟Advice修复异常...");
}
}
SystemArchitecture.java
package com.aspect;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class SystemArchitecture {
@Pointcut("execution (* com.aspect.service.impl.*.*(..))")
public void myPointCut(){
System.out.println("SystemArchitecture...");
}
}
RunMain.java
package com.aspect.main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.aspect.service.Hello;
import com.aspect.service.World;
public class RunMain {
public static void main(String[] args) {
@SuppressWarnings("resource")
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"aspect.xml");
Hello hello = ctx.getBean("hello", Hello.class);
hello.foo();
hello.addUser("Lucy", "1122");
World world = ctx.getBean("world", World.class);
world.bar();
// hello.addUser("Li","2233");
}
}
aspect.xml
<?xml version="1.0" encoding="GBK"?>
<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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <!-- 指定自动搜索Bean组件、自动搜索切面类 -->
<context:component-scan base-package="ocom.aspect.service
,com.aspect">
<context:include-filter type="annotation"
expression="org.aspectj.lang.annotation.Aspect" />
</context:component-scan>
<!-- 启动@AspectJ支持 -->
<aop:aspectj-autoproxy />
</beans>
结果:
2017-4-14 0:20:04 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@133f1d7: startup date [Fri Apr 14 00:20:04 CST 2017]; root of context hierarchy
2017-4-14 0:20:04 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [aspect.xml]
执行目标方法前,模拟开始事务..
模拟执行权限检查方法...
exect hello.foo()....
2017-04-14 00:20:04模拟日志调用方法,返回的参数----->>>null
2017-04-14 00:20:04模拟释放资源方法执行完毕
执行目标方法后,模拟结束事务..
执行目标方法前,模拟开始事务..
模拟执行权限检查方法...
hello.addUser() begin, the param: 【增加的前缀】Lucy ,1122
2017-04-14 00:20:04模拟日志调用方法,返回的参数----->>>20
2017-04-14 00:20:04模拟释放资源方法执行完毕
执行目标方法后,模拟结束事务..
执行目标方法前,模拟开始事务..
模拟执行权限检查方法...
exect bar()...
2017-04-14 00:20:04模拟日志调用方法,返回的参数----->>>null
2017-04-14 00:20:04模拟释放资源方法执行完毕
执行目标方法后,模拟结束事务..
package com.aspect.service;
public interface Hello {
public void foo();
public int addUser(String name,String pass);
}
HelloImpl.java
/**
*
*/
package com.aspect.service.impl;
import org.springframework.stereotype.Component;
import com.aspect.service.Hello;
import com.sun.xml.internal.txw2.IllegalAnnotationException;
@Component("hello")
public class HelloImpl implements Hello {
@Override
public void foo() {
System.out.println("exect hello.foo()....");
}
@Override
public int addUser(String name, String pass) {
System.out.println("hello.addUser() begin, the param: " + name + " ," + pass);
if (name.length() < 3 && name.length() >= 10){
throw new IllegalAnnotationException("name参数的长度必须大于3,小于10");
}
return 20;
}
}
World.java
package com.aspect.service;
public interface World {
public void bar();
}
WorldImpl.java
package com.aspect.service.impl;
import org.springframework.stereotype.Component;
import com.aspect.service.World;
@Component("world")
public class WorldImpl implements World {
@Override
public void bar() {
System.out.println("exect bar()...");
}
}
AroundAspect,java
package com.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class AroundAspect {
@Around("execution (* com.aspect.service.impl.*.*(..))")
public Object processAround(ProceedingJoinPoint jp) throws Throwable {
System.out.println("执行目标方法前,模拟开始事务..");
Object[] args = jp.getArgs();
if (args != null && args.length > 1) {
args[0] = "【增加的前缀】" + args[0];
}
Object rvt = jp.proceed(args); // 执行目标方法
System.out.println("执行目标方法后,模拟结束事务.." + "\n\n");
if (rvt != null && rvt instanceof Integer) {
rvt = (Integer) rvt * (Integer) rvt;
}
return rvt;
}
}
AuthAspect,java
package com.aspect;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class AuthAspect {
@Before("execution(* com.aspect.service.impl.*.*(..))")
public void authority(){
System.out.println("模拟执行权限检查方法...");
}
}
DestoryAspect.java
package com.aspect;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class DestoryAspect {
@After("execution(* com.aspect.service.impl.*.*(..))")
public void logAfter() {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(date) + "模拟释放资源方法执行完毕 " );
}
}
LogAspect.java
package com.aspect;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class LogAspect {
@AfterReturning(returning = "rvt", pointcut = "SystemArchitecture.myPointCut()")
public void log(Object rvt) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(date) + "模拟日志调用方法,返回的参数----->>>" + rvt);
}
}
RepairAspect.java
package com.aspect;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class RepairAspect {
@AfterThrowing(throwing = "ex", pointcut = "execution(* com.aspect.service.impl.*.*(..))")
public void doRecoveryAction(Throwable ex) {
System.out.println("目标方法抛出的异常为: " + ex);
System.out.println("模拟Advice修复异常...");
}
}
SystemArchitecture.java
package com.aspect;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class SystemArchitecture {
@Pointcut("execution (* com.aspect.service.impl.*.*(..))")
public void myPointCut(){
System.out.println("SystemArchitecture...");
}
}
RunMain.java
package com.aspect.main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.aspect.service.Hello;
import com.aspect.service.World;
public class RunMain {
public static void main(String[] args) {
@SuppressWarnings("resource")
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"aspect.xml");
Hello hello = ctx.getBean("hello", Hello.class);
hello.foo();
hello.addUser("Lucy", "1122");
World world = ctx.getBean("world", World.class);
world.bar();
// hello.addUser("Li","2233");
}
}
aspect.xml
<?xml version="1.0" encoding="GBK"?>
<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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <!-- 指定自动搜索Bean组件、自动搜索切面类 -->
<context:component-scan base-package="ocom.aspect.service
,com.aspect">
<context:include-filter type="annotation"
expression="org.aspectj.lang.annotation.Aspect" />
</context:component-scan>
<!-- 启动@AspectJ支持 -->
<aop:aspectj-autoproxy />
</beans>
结果:
2017-4-14 0:20:04 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@133f1d7: startup date [Fri Apr 14 00:20:04 CST 2017]; root of context hierarchy
2017-4-14 0:20:04 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [aspect.xml]
执行目标方法前,模拟开始事务..
模拟执行权限检查方法...
exect hello.foo()....
2017-04-14 00:20:04模拟日志调用方法,返回的参数----->>>null
2017-04-14 00:20:04模拟释放资源方法执行完毕
执行目标方法后,模拟结束事务..
执行目标方法前,模拟开始事务..
模拟执行权限检查方法...
hello.addUser() begin, the param: 【增加的前缀】Lucy ,1122
2017-04-14 00:20:04模拟日志调用方法,返回的参数----->>>20
2017-04-14 00:20:04模拟释放资源方法执行完毕
执行目标方法后,模拟结束事务..
执行目标方法前,模拟开始事务..
模拟执行权限检查方法...
exect bar()...
2017-04-14 00:20:04模拟日志调用方法,返回的参数----->>>null
2017-04-14 00:20:04模拟释放资源方法执行完毕
执行目标方法后,模拟结束事务..
相关文章推荐
- ITCAST视频-Spring学习笔记(使用Spring的注解方式实现AOP的细节)
- [原创]java WEB学习笔记105:Spring学习---AOP介绍,相关概念,使用AOP,利用 方法签名 编写 AspectJ 切入点表达式
- Spring学习笔记(14)----使用Spring的注解方式实现AOP
- 详解在Spring中如何使用AspectJ来实现AOP
- Spring中使用AspectJ实现AOP
- Spring学习--用 ASpectJ 注解实现 AOP
- spring 4.0 AOP (使用AspectJ的注解方式 的aop实现)简单实例
- Spring系列之 (九):AOP实现方式(一):使用AspectJ的xml方式
- spring 4.0 AOP (使用AspectJ的注解方式 的aop实现)简单实例
- ITCAST视频-Spring学习笔记(使用CGLIB实现AOP功能与AOP概念解释)
- spring学习笔记7--使用spring进行面向切面的(AOP)编程(1)注解方式实现
- Spring学习笔记 —— AOP(面向切面编程) 之使用ProxyFactoryBean实现AOP
- Spring基础学习(九)——基于AspectJ的两种AOP实现方式
- Spring3.1 使用AspectJ实现AOP
- ITCAST视频-Spring学习笔记(使用Spring的注解方式实现AOP入门)
- Spring使用AspectJ注解和XML配置实现AOP
- spring AOP (使用AspectJ的xml方式 的aop实现) (7)
- spring框架的学习(五)——Spring使用AspectJ进行AOP的开发:注解方式
- 学习笔记之springAOP的aspectJ实现注意点总结