基于自定义注解annotation 实例

本文介绍了如何创建并使用自定义注解@NeedJSONNullReplace在AOP(面向切面编程)中实现特定功能。注解的@Target指定可以应用于方法,@Retention设置为RUNTIME以在运行时保留。只需在需要过滤的方法上添加该注解,即可生效。
摘要由CSDN通过智能技术生成

1,自定义注解格式:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface NeedJSONNullReplace {
	
}
2,注解实现类

public class JSONNullReplaceAdvice {
	/**
	 * 用于在返回的时候,将jsonObject,jsonArray,ResultInfoVO中object中的jsonnull替换为“”;
	 * @param jp
	 * @param returnValue
	 * @throws Throwable
	 */
	public void afterReturning(JoinPoint jp, Object returnValue) throws Throwable {
	    if(returnValue instanceof JSONObject){
	  	    JSONObject obj = (JSONObject)returnValue;
		    returnValue =  JSONUtil.JSONNullFilter(obj);
	    }else if(returnValue instanceof JSONArray){
		    JSONArray arr = (JSONArray)returnValue;
		    returnValue = JSONUtil.JSONNullFilter(arr);
	    }else if(returnValue instanceof ResultInfoVO){
		    Object obj = ((ResultInfoVO)returnValue).getObject();
		    if(obj instanceof JSONObject){
			    JSONObject jsonObj = (JSONObject)obj;
		  	    jsonObj = JSONUtil.JSONNullFilter(jsonObj);
		    }else if(obj instanceof JSONArray){
			    JSONArray jsonArr = (JSONArray)obj;
			    jsonArr = JSONUtil.JSONNullFilter(jsonArr);
		    }
		   
	    }
	}
}
3,面向切面配置

<bean id="j
自定义注解是一种在Java中实现AOP(面向切面编程)的方式。通过定义注解并在代码中使用该注解,可以实现对特定方法或类的增强操作。下面是一个详细的自定义注解实现AOP的示例: 首先,定义一个自定义注解,例如@LogExecution: ```java 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 LogExecution { String value() default ""; } ``` 然后,在需要增强的方法上使用该注解: ```java public class MyClass { @LogExecution("执行MyMethod方法") public void myMethod() { // 方法逻辑 } } ``` 接下来,创建一个切面类,用于在方法执行前后进行增强操作: ```java import java.lang.reflect.Method; public class LogExecutionAspect { public void logExecution(JoinPoint joinPoint) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); LogExecution annotation = method.getAnnotation(LogExecution.class); String value = annotation.value(); System.out.println("开始执行方法:" + method.getName()); System.out.println("注解值:" + value); // 执行原始方法 joinPoint.proceed(); System.out.println("方法执行结束:" + method.getName()); } } ``` 最后,在应用程序中配置切面和目标对象的关系,并启动应用程序: ```java import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Application { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(LogExecutionAspect.class); context.register(MyClass.class); context.refresh(); MyClass myClass = context.getBean(MyClass.class); myClass.myMethod(); context.close(); } } ``` 以上示例中,自定义注解@LogExecution用于标记需要增强的方法,切面类LogExecutionAspect中的logExecution方法会在方法执行前后进行增强操作。在应用程序中,通过配置切面和目标对象的关系,实现了自定义注解AOP功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值