当前位置: 首页 > news >正文

从代理模式到注解开发

代理模式

package org.example.proxy;public class ProxyClient {public static void main(String[] args) {ProxyBuilder proxyBuilder = new ProxyBuilder();proxyBuilder.build();}
}interface BuildDream {void build();
}class CustomBuilder implements BuildDream {@Overridepublic void build() {System.out.println("自定义执行方法");}
}class ProxyBuilder implements BuildDream {private CustomBuilder customBuilder;@Overridepublic void build() {if (this.customBuilder == null) {this.customBuilder = new CustomBuilder();}System.out.println("代理执行前");customBuilder.build();System.out.println("代理执行后");}
}

执行结果:

代理执行前
自定义执行方法
代理执行后

动态代理

Java 动态代理是一种设计模式,允许在运行时创建代理对象,以拦截对目标对象的方法调用。动态代理通常用于横切关注点(如日志记录、事务管理、权限控制等)的实现。Java 提供了两种主要的动态代理机制:

  1. JDK 动态代理:基于接口的代理。
  2. CGLIB 动态代理:基于类的代理。

JDK 动态代理

JDK 动态代理使用 java.lang.reflect.Proxy 类和 java.lang.reflect.InvocationHandler 接口来实现。它只能代理实现了接口的类。

示例代码
package org.example.proxy;import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;/*** JDK动态代理实现*/
public class DynamicProxy {public static void main(String[] args) {MyService myService = (MyService) MyServiceProxy.newProxyInstance(new MyServiceImpl());myService.saveInfo();}
}// 定义接口
interface MyService {void saveInfo();
}// 实现接口的类
class MyServiceImpl implements MyService {@Overridepublic void saveInfo() {System.out.println("保存信息成功");}
}class MyServiceProxy implements InvocationHandler {private Object target;MyServiceProxy(Object target) {this.target = target;}@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {System.out.println("代理方法执行前");Object result = method.invoke(target, args);System.out.println("代理方法执行后");return result;}public static Object newProxyInstance(Object target) {return Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(),new MyServiceProxy(target));}
}

执行结果:

代理方法执行前
保存信息成功
代理方法执行后

CGLIB 动态代理

CGLIB 动态代理使用字节码生成库来生成代理类,它可以代理没有实现接口的类。

示例代码
package org.example.proxy;import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;import java.lang.reflect.Method;/*** CGLIB实现动态代理*/
public class CGLibProxy {public static void main(String[] args) {HerService herService = (HerService) ProxyServiceInterceptor.newProxyInstance(new HerService());herService.saveInfo();herService.sayHello();}
}class HerService {public void saveInfo() {System.out.println("保存信息成功");}public void sayHello() {System.out.println("Hi");}
}class ProxyServiceInterceptor implements MethodInterceptor {private Object target;ProxyServiceInterceptor(Object target) {this.target = target;}@Overridepublic Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {System.out.println("代理方法执行前");Object result = methodProxy.invokeSuper(o, objects);System.out.println("代理方法执行后");return result;}public static Object newProxyInstance(Object target) {Enhancer enhancer = new Enhancer();enhancer.setSuperclass(target.getClass());enhancer.setCallback(new ProxyServiceInterceptor(target));return enhancer.create();}
}

运行结果:

代理方法执行前
保存信息成功
代理方法执行后
代理方法执行前
Hi
代理方法执行后

总结

  • JDK 动态代理:适用于代理实现了接口的类。
  • CGLIB 动态代理:适用于代理没有实现接口的类。

这两种动态代理机制都可以用于实现 AOP(面向切面编程),以便在不修改目标对象代码的情况下添加额外的功能。

AOP

AOP(面向切面编程)是一种编程范式,它允许你在不修改业务逻辑代码的情况下,添加横切关注点(如日志记录、事务管理、权限控制等)。Spring Framework 提供了强大的 AOP 支持,主要通过以下几种方式实现:

  1. 基于代理的 AOP:使用 JDK 动态代理或 CGLIB 动态代理。
  2. 基于注解的 AOP:使用注解来定义切面和切点。
  3. 基于 XML 配置的 AOP:使用 XML 配置文件来定义切面和切点。

基于注解的 AOP 示例

下面是一个使用 Spring AOP 和注解的示例,展示了如何在 Spring 应用程序中使用 AOP。

1. 添加依赖

首先,确保你的项目中包含 Spring AOP 相关的依赖。如果你使用的是 Maven,可以在 pom.xml 中添加以下依赖:

<dependencies><!-- Spring AOP --><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>5.3.10</version></dependency><!-- AspectJ --><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>5.3.10</version></dependency>
</dependencies>
2. 定义业务逻辑类

定义一个简单的业务逻辑类:

package com.example.service;import org.springframework.stereotype.Service;@Service
public class HelloService {public void sayHello() {System.out.println("Hello, World!");}
}
3. 定义切面类

定义一个切面类,使用注解来指定切点和通知:

package com.example.aspect;import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;@Aspect
@Component
public class LoggingAspect {@Before("execution(* com.example.service.HelloService.sayHello(..))")public void logBefore() {System.out.println("Before method call");}@After("execution(* com.example.service.HelloService.sayHello(..))")public void logAfter() {System.out.println("After method call");}
}
4. 配置 Spring 应用程序

创建一个 Spring 配置类,启用 AOP 支持:

package com.example.config;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;@Configuration
@ComponentScan(basePackages = "com.example")
@EnableAspectJAutoProxy
public class AppConfig {
}
5. 测试代码

编写一个测试类来验证 AOP 的效果:

package com.example;import com.example.config.AppConfig;
import com.example.service.HelloService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Main {public static void main(String[] args) {ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);HelloService helloService = context.getBean(HelloService.class);helloService.sayHello();}
}

运行结果

当你运行测试代码时,输出将会是:

Before method call
Hello, World!
After method call

解释

  1. 业务逻辑类HelloService 是一个简单的业务逻辑类,包含一个 sayHello 方法。
  2. 切面类LoggingAspect 是一个切面类,包含两个通知方法 logBeforelogAfter,分别在 sayHello 方法调用之前和之后执行。
  3. Spring 配置类AppConfig 是一个 Spring 配置类,启用了 AOP 支持并扫描指定包中的组件。
  4. 测试代码:在测试代码中,通过 Spring 容器获取 HelloService 的代理对象,并调用 sayHello 方法。

总结

通过使用 Spring AOP 和注解,你可以在不修改业务逻辑代码的情况下,轻松地添加横切关注点。Spring AOP 提供了强大的功能和灵活性,使得代码更加模块化和可维护。

@Before中的参数

在 Spring AOP 中,@Before 注解用于定义一个前置通知(Advice),它会在目标方法执行之前执行。@Before 注解的参数是一个切点表达式,用于指定哪些方法应该被拦截。切点表达式可以使用多种方式来匹配目标方法,包括方法签名、注解、包名等。

常见的切点表达式

  1. 匹配方法签名

    • execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)
    • 例如:execution(* com.example.service.HelloService.sayHello(..))
  2. 匹配类上的注解

    • @within(annotationType)
    • 例如:@within(org.springframework.stereotype.Service)
  3. 匹配方法上的注解

    • @annotation(annotationType)
    • 例如:@annotation(org.springframework.transaction.annotation.Transactional)
  4. 匹配包名

    • within(package-name)
    • 例如:within(com.example.service..*)
  5. 匹配参数

    • args(argument-types)
    • 例如:args(String, ..)

示例代码

以下是一些常见的 @Before 注解的使用示例:

1. 匹配特定方法

匹配 com.example.service.HelloService 类中的 sayHello 方法:

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;@Aspect
@Component
public class LoggingAspect {@Before("execution(* com.example.service.HelloService.sayHello(..))")public void logBefore() {System.out.println("Before method call");}
}
2. 匹配特定包中的所有方法

匹配 com.example.service 包及其子包中的所有方法:

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;@Aspect
@Component
public class LoggingAspect {@Before("within(com.example.service..*)")public void logBefore() {System.out.println("Before method call");}
}
3. 匹配带有特定注解的方法

匹配带有 @Transactional 注解的方法:

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;@Aspect
@Component
public class LoggingAspect {@Before("@annotation(org.springframework.transaction.annotation.Transactional)")public void logBefore() {System.out.println("Before transactional method call");}
}
4. 匹配带有特定参数的方法

匹配第一个参数为 String 类型的方法:

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;@Aspect
@Component
public class LoggingAspect {@Before("args(java.lang.String, ..)")public void logBefore() {System.out.println("Before method call with String argument");}
}

组合切点表达式

你可以使用 &&||! 运算符来组合多个切点表达式。例如,匹配 com.example.service 包中的所有方法,并且这些方法带有 @Transactional 注解:

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;@Aspect
@Component
public class LoggingAspect {@Before("within(com.example.service..*) && @annotation(org.springframework.transaction.annotation.Transactional)")public void logBefore() {System.out.println("Before transactional method call in service package");}
}

总结

  • 匹配特定方法:使用 execution 表达式。
  • 匹配特定包中的所有方法:使用 within 表达式。
  • 匹配带有特定注解的方法:使用 @annotation 表达式。
  • 匹配带有特定参数的方法:使用 args 表达式。
  • 组合切点表达式:使用 &&||! 运算符。

通过合理使用这些切点表达式,你可以灵活地定义哪些方法应该被拦截,从而实现各种横切关注点的功能。

反射

package org.example.reflect;import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;public class ReflectTest {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {Class<?> aClass = Class.forName("org.example.reflect.Student");Class<Student> studentClass = Student.class;Constructor<Student> constructor = studentClass.getDeclaredConstructor(String.class, Integer.class);constructor.setAccessible(true);Student oh = constructor.newInstance("Oh", 66);//        Constructor<?>[] constructors = aClass.getDeclaredConstructors();
//        Iterator<Constructor<?>> constructorIterator = Arrays.stream(constructors).iterator();
//        while (constructorIterator.hasNext()) {
//            System.out.println(constructorIterator.next());
//        }Constructor<?> declaredConstructor = aClass.getDeclaredConstructor(String.class, Integer.class);// 为私有构造函数获取访问性declaredConstructor.setAccessible(true);Student instance = (Student) declaredConstructor.newInstance("Xin", 25);
//        instance.printInfo();Method declaredMethod = aClass.getDeclaredMethod("printInfo");declaredMethod.setAccessible(true);String result = (String) declaredMethod.invoke(instance);System.out.println(result);System.out.println(declaredMethod.invoke(oh).toString());}
}class Student {private String name;private Integer age;public Student() {System.out.println("无参构造函数");}private Student(String name, Integer age) {this.name = name;this.age = age;}private String printInfo() {System.out.println(this.name + " " + this.age);return "OK";}
}

Annotation

实现自定义注解

在Java中,自定义注解和使用AOP(面向切面编程)来扫描带有该注解的类和方法,可以通过以下步骤实现。我们将使用Spring AOP来实现这一功能。

1. 创建自定义注解

首先,我们需要创建一个自定义注解。例如,我们创建一个名为 @MyAnnotation 的注解:

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, ElementType.TYPE})
public @interface MyAnnotation {String value() default "";
}

2. 创建AOP切面

接下来,我们需要创建一个AOP切面类,用于拦截带有 @MyAnnotation 注解的方法或类。我们可以使用Spring AOP来实现这一点。

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;@Aspect
@Component
public class MyAnnotationAspect {@Before("@within(myAnnotation) || @annotation(myAnnotation)")public void beforeMethod(MyAnnotation myAnnotation) {// 在方法执行之前执行的逻辑System.out.println("Intercepted method with annotation: " + myAnnotation.value());}
}

3. 配置Spring AOP

为了使AOP切面生效,我们需要在Spring配置文件中启用AOP支持。可以在Spring Boot应用程序的主类中添加 @EnableAspectJAutoProxy 注解:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;@SpringBootApplication
@EnableAspectJAutoProxy
public class MyApplication {public static void main(String[] args) {SpringApplication.run(MyApplication.class, args);}
}

4. 使用自定义注解

现在,我们可以在类或方法上使用自定义注解 @MyAnnotation,并且AOP切面会拦截这些带有注解的方法或类。

import org.springframework.stereotype.Service;@Service
public class MyService {@MyAnnotation(value = "testMethod")public void testMethod() {System.out.println("Executing testMethod");}
}

5. 运行应用程序

启动Spring Boot应用程序,并调用带有 @MyAnnotation 注解的方法。你会看到AOP切面拦截了该方法,并执行了 beforeMethod 中的逻辑。

总结

通过上述步骤,我们实现了自定义注解,并使用Spring AOP扫描带有该注解的类和方法,从而实现类似于Spring的操作。关键步骤包括创建自定义注解、创建AOP切面类、配置Spring AOP以及在类或方法上使用自定义注解。

@Retention@Target 注解

@Retention@Target 是Java注解的元注解(meta-annotations),它们用于定义自定义注解的行为和适用范围。

@Retention

@Retention 注解指定了自定义注解的保留策略,即注解在什么阶段仍然存在。RetentionPolicy 枚举有以下几种值:

  • RetentionPolicy.SOURCE:注解只在源代码中存在,编译器在编译时会丢弃这种注解。
  • RetentionPolicy.CLASS:注解在编译时被保留在类文件中,但在运行时不会被JVM保留。这是默认的保留策略。
  • RetentionPolicy.RUNTIME:注解在运行时也会被JVM保留,因此可以通过反射机制读取这种注解。

在你的例子中,@Retention(RetentionPolicy.RUNTIME) 表示 @MyAnnotation 注解在运行时也会被保留,因此可以通过反射机制读取。

@Target

@Target 注解指定了自定义注解可以应用的Java元素类型。ElementType 枚举有以下几种值:

  • ElementType.TYPE:可以应用于类、接口(包括注解类型)或枚举声明。
  • ElementType.FIELD:可以应用于字段或属性。
  • ElementType.METHOD:可以应用于方法。
  • ElementType.PARAMETER:可以应用于方法参数。
  • ElementType.CONSTRUCTOR:可以应用于构造函数。
  • ElementType.LOCAL_VARIABLE:可以应用于局部变量。
  • ElementType.ANNOTATION_TYPE:可以应用于注解类型。
  • ElementType.PACKAGE:可以应用于包声明。
  • ElementType.TYPE_PARAMETER:可以应用于类型参数(Java 8及以上)。
  • ElementType.TYPE_USE:可以应用于任何使用类型的地方(Java 8及以上)。

在你的例子中,@Target({ElementType.METHOD, ElementType.TYPE}) 表示 @MyAnnotation 注解可以应用于方法和类(包括接口和枚举)。

总结

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface MyAnnotation {String value() default "";
}
  • @Retention(RetentionPolicy.RUNTIME):表示 @MyAnnotation 注解在运行时也会被保留,可以通过反射机制读取。
  • @Target({ElementType.METHOD, ElementType.TYPE}):表示 @MyAnnotation 注解可以应用于方法和类(包括接口和枚举)。

通过这些元注解的配置,你可以控制自定义注解的行为和适用范围。

重新理解@Transactional

@Transactional 是 Spring 提供的一个非常重要的注解,用于声明式事务管理。它可以应用于类或方法上,以便在方法执行时自动开启和管理事务。通过理解 @Transactional 的工作原理,我们可以更好地理解如何自定义注解并使用 AOP 来实现类似的功能。

@Transactional 注解的元注解

@Transactional 注解本身使用了一些元注解来定义其行为和适用范围:

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Transactional {// 其他属性和方法
}
  • @Target({ElementType.METHOD, ElementType.TYPE}):表示 @Transactional 注解可以应用于方法和类(包括接口和枚举)。
  • @Retention(RetentionPolicy.RUNTIME):表示 @Transactional 注解在运行时也会被保留,可以通过反射机制读取。
  • @Documented:表示使用 @Transactional 注解的元素应当被 javadoc 或类似工具文档化。

@Transactional 的属性

@Transactional 注解有许多属性,用于配置事务的行为,例如:

  • propagation:事务的传播行为(例如,REQUIRED、REQUIRES_NEW 等)。
  • isolation:事务的隔离级别(例如,READ_COMMITTED、SERIALIZABLE 等)。
  • timeout:事务的超时时间。
  • readOnly:是否为只读事务。
  • rollbackFor:指定哪些异常会触发事务回滚。
  • noRollbackFor:指定哪些异常不会触发事务回滚。

@Transactional 的工作原理

@Transactional 注解的工作原理主要依赖于 Spring AOP。Spring AOP 会拦截带有 @Transactional 注解的方法或类,并在方法执行前后管理事务的开启、提交和回滚。

自定义注解和 AOP 实现类似 @Transactional 的功能

我们可以通过自定义注解和 AOP 来实现类似 @Transactional 的功能。以下是一个示例,展示了如何创建一个自定义注解 @MyTransactional,并使用 AOP 来管理事务。

1. 创建自定义注解
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, ElementType.TYPE})
public @interface MyTransactional {// 可以添加其他属性,例如事务传播行为、隔离级别等
}

@Transactional 注解是 Spring 框架中用于声明式事务管理的核心注解。它可以应用于类或方法上,以声明该类或方法需要事务支持。通过 @Transactional 注解,Spring 可以自动管理事务的开始、提交和回滚。

@Transactional 注解的定义

@Transactional 注解的定义如下:

package org.springframework.transaction.annotation;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.interceptor.TransactionAttribute;@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Transactional {@AliasFor("transactionManager")String value() default "";@AliasFor("value")String transactionManager() default "";Propagation propagation() default Propagation.REQUIRED;Isolation isolation() default Isolation.DEFAULT;int timeout() default TransactionDefinition.TIMEOUT_DEFAULT;boolean readOnly() default false;Class<? extends Throwable>[] rollbackFor() default {};String[] rollbackForClassName() default {};Class<? extends Throwable>[] noRollbackFor() default {};String[] noRollbackForClassName() default {};
}

元注解的解释

@Retention
@Retention(RetentionPolicy.RUNTIME)
  • RetentionPolicy.RUNTIME:表示 @Transactional 注解在运行时也会被保留,可以通过反射机制读取。这对于事务管理是必要的,因为 Spring 需要在运行时动态地管理事务。
@Target
@Target({ElementType.METHOD, ElementType.TYPE})
  • ElementType.METHOD:表示 @Transactional 注解可以应用于方法。
  • ElementType.TYPE:表示 @Transactional 注解可以应用于类(包括接口和枚举)。当应用于类时,该类的所有方法都将具有事务支持,除非方法上另有声明。

@Transactional 注解的属性

  • valuetransactionManager:指定要使用的事务管理器的名称。默认情况下,使用默认的事务管理器。
  • propagation:指定事务的传播行为。默认值为 Propagation.REQUIRED,表示当前方法必须在事务中运行。如果当前没有事务,则会启动一个新的事务。
  • isolation:指定事务的隔离级别。默认值为 Isolation.DEFAULT,表示使用底层数据库的默认隔离级别。
  • timeout:指定事务的超时时间(以秒为单位)。默认值为 TransactionDefinition.TIMEOUT_DEFAULT,表示使用底层事务管理器的默认超时时间。
  • readOnly:指定事务是否为只读。默认值为 false。只读事务可以优化性能,因为数据库可以跳过某些锁定操作。
  • rollbackForrollbackForClassName:指定哪些异常会触发事务回滚。默认情况下,只有未捕获的运行时异常会触发回滚。
  • noRollbackFornoRollbackForClassName:指定哪些异常不会触发事务回滚。

使用示例

在类上使用 @Transactional
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;@Service
@Transactional
public class MyService {public void performOperation() {// 该方法将在事务中运行}
}
在方法上使用 @Transactional
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;@Service
public class MyService {@Transactionalpublic void performOperation() {// 该方法将在事务中运行}
}

自定义注解与 @Transactional 的对比

通过前面的自定义注解示例,我们可以看到 @Transactional 注解的实现方式与自定义注解类似。它们都使用了 @Retention@Target 元注解来定义注解的保留策略和适用范围。不同的是,@Transactional 注解还包含了多个属性,用于配置事务的行为。

总结

  • @Retention(RetentionPolicy.RUNTIME):表示 @Transactional 注解在运行时也会被保留,可以通过反射机制读取。
  • @Target({ElementType.METHOD, ElementType.TYPE}):表示 @Transactional 注解可以应用于方法和类(包括接口和枚举)。
  • 属性@Transactional 注解包含多个属性,用于配置事务的传播行为、隔离级别、超时时间、只读属性以及回滚规则。

通过理解 @Transactional 注解的定义和使用,我们可以更好地理解如何创建和使用自定义注解,并通过AOP实现类似的功能。

使用反射机制实现自定义注解

在Java中,自定义注解是一种强大的工具,用于提供额外的元数据信息。注解以@符号开头,后面紧跟注解名。下面是如何创建和使用自定义注解的步骤:

1. 创建自定义注解

创建注解非常简单,只需要定义一个Java接口,并使用@interface关键字。注解接口通常用于定义注解的元数据,如参数、默认值等。

// 创建一个自定义注解
@interface MyAnnotation {String value() default "";
}

在这个例子中,我们创建了一个名为MyAnnotation的注解,它有一个名为value的参数,参数默认值为""

2. 使用自定义注解

在类、方法、字段或参数上使用自定义注解,只需要在相应的位置使用@MyAnnotation

示例:使用自定义注解在方法上
public class ExampleClass {@MyAnnotation("example")public void myMethod() {// 方法体}
}
示例:使用自定义注解在字段上
public class ExampleClass {@MyAnnotation("example")private String myField;
}
示例:使用自定义注解在参数上
public class ExampleClass {public void myMethod(@MyAnnotation("example") String myParam) {// 方法体}
}
3. 反射操作

使用Java的反射API,可以读取类上的注解信息。

import java.lang.reflect.Method;public class AnnotationReader {public static void main(String[] args) throws Exception {ExampleClass exampleClass = new ExampleClass();Method[] methods = exampleClass.getClass().getMethods();for (Method method : methods) {MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class);if (myAnnotation != null) {System.out.println("Method: " + method.getName() + ", Value: " + myAnnotation.value());}}}
}
总结

自定义注解在Java中非常有用,可以帮助开发者添加额外的元数据信息,提高代码的可读性和可维护性。通过结合注解处理器(Annotation Processing Tool,APT)和反射API,可以实现更强大的代码生成和动态行为。

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • css实现每个小盒子占32%,超出就换行
  • 【Linux】将IDEA项目部署到云服务器上,让其成为后台进程(保姆级教学,满满的干货~~)
  • 关系型数据库和非关系型数据库
  • 1千多看图猜成语游戏ACCESS\EXCEL数据库
  • 使用Scikit-Learn决策树:分类问题解决方案指南
  • 【Js】导出 HTML 为 Word 文档
  • 【EXCELL技巧篇】使用Excel公式,获取当前 Excel的Sheet页的名字
  • FPGA笔试
  • 数据湖表格式 Hudi/Iceberg/DeltaLake/Paimon TPCDS 性能对比(Spark 引擎)
  • Ubuntu linux安装新版本go
  • 采用反相正基准电压电路的同相运算放大器(运放)
  • easyswoole/Hyperf开发的php系统 cpu超负荷定位排查
  • debian 实现离线批量安装软件包
  • Netty HTTP
  • kibana连接elasticsearch(版本8.11.3)
  • [ 一起学React系列 -- 8 ] React中的文件上传
  • 《Javascript高级程序设计 (第三版)》第五章 引用类型
  • 《微软的软件测试之道》成书始末、出版宣告、补充致谢名单及相关信息
  • 【跃迁之路】【733天】程序员高效学习方法论探索系列(实验阶段490-2019.2.23)...
  • 08.Android之View事件问题
  • Angular 响应式表单之下拉框
  • Bytom交易说明(账户管理模式)
  • Codepen 每日精选(2018-3-25)
  • Effective Java 笔记(一)
  • Eureka 2.0 开源流产,真的对你影响很大吗?
  • input实现文字超出省略号功能
  • JavaWeb(学习笔记二)
  • MySQL Access denied for user 'root'@'localhost' 解决方法
  • Python学习之路16-使用API
  • Spring框架之我见(三)——IOC、AOP
  • vue-cli3搭建项目
  • 程序员最讨厌的9句话,你可有补充?
  • 电商搜索引擎的架构设计和性能优化
  • 利用阿里云 OSS 搭建私有 Docker 仓库
  • 设计模式走一遍---观察者模式
  • 消息队列系列二(IOT中消息队列的应用)
  • 异步
  • 再次简单明了总结flex布局,一看就懂...
  • 中国人寿如何基于容器搭建金融PaaS云平台
  • 你对linux中grep命令知道多少?
  • “十年磨一剑”--有赞的HBase平台实践和应用之路 ...
  • ​2021半年盘点,不想你错过的重磅新书
  • ​io --- 处理流的核心工具​
  • ​如何使用ArcGIS Pro制作渐变河流效果
  • # 利刃出鞘_Tomcat 核心原理解析(二)
  • # 利刃出鞘_Tomcat 核心原理解析(七)
  • (10)Linux冯诺依曼结构操作系统的再次理解
  • (windows2012共享文件夹和防火墙设置
  • (一)spring cloud微服务分布式云架构 - Spring Cloud简介
  • (原创)攻击方式学习之(4) - 拒绝服务(DOS/DDOS/DRDOS)
  • (转载)跟我一起学习VIM - The Life Changing Editor
  • *setTimeout实现text输入在用户停顿时才调用事件!*
  • .[backups@airmail.cc].faust勒索病毒的最新威胁:如何恢复您的数据?
  • .[hudsonL@cock.li].mkp勒索加密数据库完美恢复---惜分飞
  • .NET Core 2.1路线图