spring学习之使用AspectJ实现AOP

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模拟释放资源方法执行完毕  
执行目标方法后,模拟结束事务..







































你可能感兴趣的:(spring,aspectj)