Spring AOP 实现业务日志记录 (注解)
2014-11-13 14:05
501 查看
用户管理业务逻辑接口(UserManagerApplogic.java )
Java代码
2. 用户管理业务逻辑实现类(UserManagerApplogicImpl.java)
Java代码
3.业务注释类(BusAnnotation.java)
Java代码
(1)RetentionPolicy(保留策略)是一个enum类型,共有三个值,分别是SOURCE,CLASS 和 RUNTIME。
SOURCE 代表的是这个Annotation类型的信息只会保留在程序源码里,源码如果经过了编译之后,Annotation的数据就会消失,并不会保留在编译好的.class文件里面。
ClASS的 代 表的是这个Annotation类型的信息保留在程序源码里,同时也会保留在编译好的.class文件里面,在执行的时候,并不会把这一些信息加载到虚拟 机(JVM)中去.注意一下,当你没有设定一个Annotation类型的Retention值时,系统默认值是CLASS。
RUNTIME代表的是表示在源码、编译好的.class文件中保留信息,在执行的时候会把这一些信息加载到JVM中去的。
(2)ElementType
@Target里面的ElementType是用来指定Annotation类型可以用在哪一些元素上的.
TYPE(类型)是指可以用在Class,Interface,Enum和Annotation类型上.
FIELD(属性)
METHOD(方法)
PARAMETER(参数)
CONSTRUCTOR(构造函数)
LOCAL_VARIABLE(局部变量)
ANNOTATION_TYPE
PACKAGE(包)
(3)@Documented
@Documented的目的就是让这一个Annotation类型的信息能够显示在javaAPI说明文档上;没有添加的话,使用javadoc生成API文档的时候就会找不到这一个类型生成的信息。
(4)@Inherited
如果需要把Annotation的数据继承给子类,那么就会用到@Inherited这一个Annotation类型。
4.切面类(LogInterceptor.java)
Java代码
5.配置文件(applicationContext-aop.xml)
Xml代码
6.测试类( test.java (Junit) )
Java代码
Java代码
package com.iteye.applogic; public interface UserManagerApplogic { public void addUser(String name); }
2. 用户管理业务逻辑实现类(UserManagerApplogicImpl.java)
Java代码
package com.iteye.applogic.impl; import org.springframework.stereotype.Component; import com.iteye.applogic.UserManagerApplogic; import com.iteye.annotation.BussAnnotation; @Component("userManager") public class UserManagerApplogicImpl implements UserManagerApplogic { @BussAnnotation(moduleName="人员管理",option="添加用户") public void addUser(String name) { System.out.println("add a User!Name is "+name); } }
3.业务注释类(BusAnnotation.java)
Java代码
package com.iteye.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target ({ElementType.METHOD}) public @interface BussAnnotation { //模块名 String moduleName(); //操作内容 String option(); }
(1)RetentionPolicy(保留策略)是一个enum类型,共有三个值,分别是SOURCE,CLASS 和 RUNTIME。
SOURCE 代表的是这个Annotation类型的信息只会保留在程序源码里,源码如果经过了编译之后,Annotation的数据就会消失,并不会保留在编译好的.class文件里面。
ClASS的 代 表的是这个Annotation类型的信息保留在程序源码里,同时也会保留在编译好的.class文件里面,在执行的时候,并不会把这一些信息加载到虚拟 机(JVM)中去.注意一下,当你没有设定一个Annotation类型的Retention值时,系统默认值是CLASS。
RUNTIME代表的是表示在源码、编译好的.class文件中保留信息,在执行的时候会把这一些信息加载到JVM中去的。
(2)ElementType
@Target里面的ElementType是用来指定Annotation类型可以用在哪一些元素上的.
TYPE(类型)是指可以用在Class,Interface,Enum和Annotation类型上.
FIELD(属性)
METHOD(方法)
PARAMETER(参数)
CONSTRUCTOR(构造函数)
LOCAL_VARIABLE(局部变量)
ANNOTATION_TYPE
PACKAGE(包)
(3)@Documented
@Documented的目的就是让这一个Annotation类型的信息能够显示在javaAPI说明文档上;没有添加的话,使用javadoc生成API文档的时候就会找不到这一个类型生成的信息。
(4)@Inherited
如果需要把Annotation的数据继承给子类,那么就会用到@Inherited这一个Annotation类型。
4.切面类(LogInterceptor.java)
Java代码
package com.iteye.aop; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; import com.iteye.annotation.BussAnnotation; @Aspect @Component public class LogInterceptor { @Pointcut("execution(public * com.iteye..*.addUser(..))") public void aApplogic() {} @Around(value = "aApplogic() && @annotation(annotation) &&args(object,..) ", argNames = "annotation,object") public Object interceptorApplogic(ProceedingJoinPoint pj, BussAnnotation annotation, Object object) throws Throwable { System.out.println("moduleName:"+annotation.moduleName()); System.out.println("option:"+annotation.option()); pj.proceed(); return object; } }
5.配置文件(applicationContext-aop.xml)
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: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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/sprin 7fe9 g-aop-2.5.xsd"> <context:annotation-config /> <context:component-scan base-package="com.iteye"/> <aop:aspectj-autoproxy /> </beans>
6.测试类( test.java (Junit) )
Java代码
import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.iteye.applogic.UserManagerApplogic; public class test { @Test public void test1() { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-aop.xml"); UserManagerApplogic userManager = (UserManagerApplogic) ctx.getBean("userManager"); userManager.addUser("-li.bb-"); ctx.destroy(); } }
相关文章推荐
- Spring AOP 实现业务日志记录
- Spring AOP 基于注解的AOP 实现 记录日志,异常捕获
- 从头认识Spring-3.6 简单的AOP日志实现(注解版)-需要记录方法的运行时间
- 使用spring aop + 注解完成对业务操作的日志记录
- Spring+SpringMVC+Mybatis 利用AOP自定义注解实现可配置日志快照记录
- Spring AOP实现复杂的日志记录(自定义注解)
- Spring AOP自定义注解实现系统日志记录管理
- 使用自定义注解+Spring AOP 实现日志记录
- springboot 自定义注解+AOP 实现日志记录
- 从头认识Spring-3.8 简单的AOP日志实现(注解版)-扩展增加检查订单功能,以便记录并检测输入的参数
- Spring+SpringMVC+Mybatis 利用AOP自定义注解实现可配置日志快照记录
- 从头认识Spring-3.5 简单的AOP日志实现(注解版)-某方法之前的前后记录日志
- springboot aop 自定义注解方式实现一套完善的日志记录(完整源码)
- Spring+SpringMVC+Mybatis 利用AOP自定义注解实现可配置日志快照记录
- 利用Spring AOP实现业务和异常日志记录
- spring aop自定义注解实现日志记录
- 从头认识Spring-3.8 简单的AOP日志实现(注解版)-扩展添加检查订单功能,以便记录并检測输入的參数
- spring AOP自定义注解方式实现日志管理
- SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)