注解切面使用

来源:互联网 发布:儿童绘画软件下载 编辑:程序博客网 时间:2024/05/23 21:43

通过自定义spring注解方式,给指定的service业务类添加注解,用于日志记录等操作。


/**  

 *自定义注解 拦截Controller  
 */    
    
@Target({ElementType.PARAMETER, ElementType.METHOD})    
@Retention(RetentionPolicy.RUNTIME)    
@Documented    
public  @interface SystemControllerLog {        
    String LogAction()  default "";    
    String LogContent()  default "";
    int ModuleID()  default 0;


2、注解切面类

import java.lang.reflect.Method;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;


@Component
@Aspect
public class SystemLogServiceHelper {


@Pointcut("@annotation(com.netbox.test.SystemControllerLog)")
public void SystemLogHelper() {
System.out.println("SystemLogServiceHelper()............");
}


@Before("SystemLogHelper()")
public void before(JoinPoint point) throws ClassNotFoundException {
// 获取注解 数据内容
getControllerMethodDescription(point);
}


private void getControllerMethodDescription(JoinPoint point) throws ClassNotFoundException {


// 获取目标实现类全名
String targetName = point.getTarget().getClass().getName();
// 获取目标代理类方法名
String methodName = point.getSignature().getName();
Class declaringType = point.getSignature().getDeclaringType();
System.out.println("targetName: " + targetName);
System.out.println("methodName: " + methodName);
System.out.println("args: " + point.getArgs().length);
System.out.println("declaringType: " + declaringType);


// 获取参数
Object[] args = point.getArgs();
// 创建实例
Class<?> clazz = Class.forName(targetName);
Method[] methods = clazz.getMethods();
SystemControllerLog log;
for (Method method : methods) {
if (method.getName().equals(methodName)) {
Class<?>[] parameters = method.getParameterTypes();
if (parameters.length == args.length) {
// 获取注解数据内容
log = method.getAnnotation(SystemControllerLog.class);
System.out.println("logAction: " + log.LogAction());
System.out.println("logAction: " + log.LogContent());
System.out.println("logAction: " + log.ModuleID());
break;
}
}
}
}
}


3、测试service类

import org.springframework.stereotype.Component;
@Component
public class AnnotationService {


@SystemControllerLog(LogAction = "TestDemo.class", LogContent = "测试注解切面", ModuleID = 159)
public void testMethod() {
System.out.println("testMethod()..........");
}


@SystemControllerLog(LogAction = "TestDemo.class", LogContent = "测试注解切面", ModuleID = 159)
public void test2(String name,String password) {
System.out.println("testMethod()..........name:"+name+ " ,password:"+password);
}
}


4、测试类

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@RunWith(value = SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring.xml" })
public class SystemLogServiceHelperTest {


@Autowired
private AnnotationService annotationService;


@Test
public void test() throws Exception {
//getRegion
// annotationService.testMethod();

annotationService.test2("tom","1232");
}
}


5、xml配置文件

<?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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
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/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">
<!-- 开启cglib代理 -->
<aop:aspectj-autoproxy proxy-target-class="true" />
<!-- 开启spring自动扫描包 -->
<context:component-scan base-package="com.aop.test" />

</beans>