The role of some annotations in Java projects

1. @PostConstruct

@PostConstruct is a Java annotation that indicates that a method should be executed immediately after the class in which it is located is initialized. It is usually used to perform some initialization work or setup after dependency injection is completed. When the container creates a bean annotated with @PostConstruct, the annotated method will be called immediately after dependency injection is completed.

For example, in an application using the Spring framework, you can use the @PostConstruct annotation to mark a method as an initialization method to perform some setup operations after dependency injection is completed, such as initializing certain properties or establishing a database connection.

Here is an example using the @PostConstruct annotation:

@Component
public class MyBean {

    private String message;

    @PostConstruct
    public void init() {
        message = "Hello, World!";
        // 执行其他初始化操作
    }

    // 其他方法和属性
}

In the above example, when the container creates the MyBean instance and completes dependency injection, the init() method with the @PostConstruct annotation will be called to set the message attribute and perform other initialization operations.

Reference: Detailed explanation of the use of SpringBoot @PostConstruct and @PreDestroy

2. @Pointcut

@PointcutIs an annotation used in Spring AOP (Aspect Oriented Programming). It is used to define pointcuts, which are the locations of methods or code fragments that need to be crosscutted during program execution. Pointcuts declare which methods can be captured and processed by the AOP proxy.

Use @Pointcutannotations to bind pointcut expressions to pointcut names. A pointcut expression is a rule that matches a specific method in a program. By specifying pointcut expressions, developers can define methods that need to be intercepted, enhanced, or modified.

Example:

@Pointcut("execution(* com.example.service.*.*(..))")
public void myPointcut() {
    
    }

The above example will myPointcut()be defined as a pointcut that matches all methods in the com.example.service package.

By using the @Pointcut annotation, developers can more easily manage pointcuts and reference them where needed to apply crosscutting logic to specific locations in the program.

2.1 @Pointcut usage examples

When using Spring AOP, we can use the @Pointcut annotation to define a pointcut expression, and then reference the pointcut in the advice (Advice). Here's an example:

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class LoggingAspect {
    
    
    
    @Pointcut("execution(* com.example.service.*.*(..))")
    public void myPointcut() {
    
    }
    
    @Before("myPointcut()")
    public void beforeMethod() {
    
    
        System.out.println("前置通知:在方法执行之前执行");
    }
    
    // 其他通知方法...
}

In the above example, we defined a myPointcut()pointcut named which matches com.example.serviceall methods in the package. We can then beforeMethod()use pointcut expressions on advice methods (e.g. ) to specify which methods this advice applies to.

In the above example, beforeMethod()the method uses @Beforean annotation, which indicates that the method is a pre-advice and is executed before the method matching the pointcut is executed. @Before("myPointcut()")in myPointcut()references the entry point we defined earlier.

In this way, we can apply notifications to specific methods and implement logic that cuts across concerns. In this example, we print a message as a pre-notification before each matching method is executed.

3. The role of @annotation annotation

Annotation concept and function

4.@Aspect annotation

@Aspect annotation

5. @Around

In Java, @Aroundannotations are used to perform additional logic before and after method execution. It is an aspect annotation in the Spring framework, used to declare that a method or class is an aspect.

@AroundMethods annotated with will be run before and after the target method is executed. This method can have a ProceedingJoinPointparameter that proceed()actually executes the target method by calling the method. You can @Aroundwrite additional code in annotated methods, such as logging, handling exceptions, tracking method execution time, etc.

Here is a sample code that demonstrates using @Aroundannotations:

@Aspect
@Component
public class LoggingAspect {
    
    
  
    @Around("execution(* com.example.myapp.service.*.*(..))")
    public Object logMethodExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
    
    
        long startTime = System.currentTimeMillis();
        Object result = joinPoint.proceed();
        long endTime = System.currentTimeMillis();
        long executionTime = endTime - startTime;
        
        System.out.println(joinPoint.getSignature() + " executed in " + executionTime + "ms");
        
        return result;
    }
}

In the above example, logMethodExecutionTimethe method uses @Aroundthe annotation, which com.example.myapp.servicerecords the execution time of the method before and after all methods under the package are executed.

In general, @Aroundannotations can be used to execute some common logic before and after method execution, thereby achieving some cross-cutting functions. In the Spring framework, aspects are usually used to handle logging, transaction management, security checks, etc.

// ALL

Guess you like

Origin blog.csdn.net/Blue_Pepsi_Cola/article/details/131764683