Annotation

@NonNull

  • https://www.cnblogs.com/aaacarrot/p/17025725.html
  • import org.springframework.lang.NonNull;
  • 编译时不检查,在程序运行时被赋值null,提前报错,而不是执行无用代码,到用到的时候再报错。

@SneakyThrows

  • https://www.cnblogs.com/acmaner/p/13967688.html
  • 把本该在编译器显示申明的exception 包装成 Runtime Exception,化繁琐为简单。

@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)

  • https://www.cnblogs.com/hyry/p/11987067.html
  • 作用域范围

    • singleton单例范围,这个是比较常见的,Spring中bean的实例默认都是单例的,单例的bean在Spring容器初始化时就被直接创建,不需要通过proxyMode指定作用域代理类型。
    • prototype原型范围,这个使用较少,这种作用域的bean,每次注入调用,Spring都会创建返回不同的实例,但是,需要注意的是,如果未指明代理类型,即不使用代理的情况下,将会在容器启动时创建bean,那么每次并不会返回不同的实例,只有在指明作用域代理类型例如TARGET_CLASS后,才会在注入调用每次创建不同的实例。
    • requestweb请求范围,(最近遇到的问题就是和request作用域的bean有关,才发现之前的理解有偏差),当使用该作用域范围时(包括下面的session作用域),必须指定proxyMode作用域代理类型,否则将会报错,对于request作用域的bean,(之前一直理解的是每次有http请求时都会创建),但实际上并不是这样,而是Spring容器将会创建一个代理用作依赖注入,只有在请求时并且请求的处理中需要调用到它,才会实例化该目标bean。
    • sessionweb会话范围,这个和request类似,同样必须指定proxyMode,而且也是Spring容器创建一个代理用作依赖注入,当有会话创建时,并且在会话中请求的处理中需要调用它,才会实例话该目标bean,由于是会话范围,生命依赖于session。

@EnableTransactionManagement 

  • https://www.cnblogs.com/zhuyeshen/p/10907632.html
  • Spring Boot 使用事务非常简单,首先使用注解 @EnableTransactionManagement 开启事务支持后,然后在访问数据库的Service方法上添加注解 @Transactional 便可。

@Bean,@Component,@Service,@Repository 和 @Controller注解的区别

  • https://www.cnblogs.com/lovemelucky/p/15323898.html
  • @Bean:表示一个方法实例化、配置或者初始化一个Spring IoC容器管理的新对象。
  • @Component: 自动被comonent扫描。 表示被注解的类会自动被component扫描
  • @Repository: 用于持久层,主要是数据库存储库。
  • @Service: 表示被注解的类是位于业务层的业务component。
  • @Controller:表明被注解的类是控制component,主要用于展现层 。

@NewSpace(name = "CallExternalSystem")

自定义

复制代码
package com.sam.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, ElementType.PARAMETER})
public @interface NewSpan {
    String name() default "";
}
复制代码

 

自定义注解

  • https://www.cnblogs.com/acm-bingzi/p/javaAnnotation.html
  • @Retention – 定义该注解的生命周期
      ●   RetentionPolicy.SOURCE : 在编译阶段丢弃。这些注解在编译结束之后就不再有任何意义,所以它们不会写入字节码。@Override, @SuppressWarnings都属于这类注解。
      ●   RetentionPolicy.CLASS : 在类加载的时候丢弃。在字节码文件的处理中有用。注解默认使用这种方式
      ●   RetentionPolicy.RUNTIME : 始终不会丢弃,运行期也保留该注解,因此可以使用反射机制读取该注解的信息。我们自定义的注解通常使用这种方式。

  • Target – 表示该注解用于什么地方。默认值为任何元素,表示该注解用于什么地方。可用的ElementType 参数包括

      ● ElementType.CONSTRUCTOR: 用于描述构造器
      ● ElementType.FIELD: 成员变量、对象、属性(包括enum实例)
      ● ElementType.LOCAL_VARIABLE: 用于描述局部变量
      ● ElementType.METHOD: 用于描述方法
      ● ElementType.PACKAGE: 用于描述包
      ● ElementType.PARAMETER: 用于描述参数
      ● ElementType.TYPE: 用于描述类、接口(包括注解类型) 或enum声明

  • 复制代码

    import java.lang.annotation.Documented;
    import java.lang.annotation.Retention;
    import java.lang.annotation.Target;
    import static java.lang.annotation.ElementType.FIELD;
    import static java.lang.annotation.RetentionPolicy.RUNTIME;

    /**
    * 水果名称注解
    */
    @Target(FIELD)
    @Retention(RUNTIME)
    @Documented
    public @interface FruitName {
    String value() default "";
    }

    
    

    import java.lang.reflect.Field;

    /**
    * 注解处理器
    */
    public class FruitInfoUtil {
    public static void getFruitInfo(Class<?> clazz){

    Field[] fields = clazz.getDeclaredFields();

    for(Field field :fields){
    if(field.isAnnotationPresent(FruitName.class)){
    FruitName fruitName = (FruitName) field.getAnnotation(FruitName.class);
    strFruitName=strFruitName+fruitName.value();
    System.out.println(strFruitName);
    }

    复制代码

     

 @Around

  • Learn from official wesite: https://docs.spring.io/spring-framework/reference/core/aop/ataspectj/advice.html#aop-ataspectj-around-advice
  • https://www.cnblogs.com/JourneyOfFlower/p/17648156.html
  • 当被@Around注解修饰的方法被调用时,它会在目标方法执行前和执行后拦截处理。常用于加日志。
  • 复制代码
    @Around注解
    在Java中,@Around注解是AspectJ框架中的一个切面注解,用于定义环绕通知(around advice)。
    
    具体来说,@Around注解可以应用在切面类的方法上,用于包围目标方法的执行。当被@Around注解修饰的方法被调用时,它会在目标方法执行前和执行后拦截处理。
    
    @Around注解的主要作用是在目标方法的执行前后进行一些额外的操作,例如日志记录、性能监控、多数据源动态切换、事务管理等。
    
    下面是一个使用@Around注解的示例:
    
    @Aspect
    @Component
    public class LoggingAspect {
    
        @Around("execution(* com.example.MyService.*(..))")
        public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
            // 在目标方法执行前的操作
            System.out.println("Before method execution");
    
            // 执行目标方法
            Object result = joinPoint.proceed();
    
            // 在目标方法执行后的操作
            System.out.println("After method execution");
    
            return result;
        }
    }
    javaCopy Code@Aspect
    @Component
    public class LoggingAspect {
    
        @Around("@annotation(logPrint)")
        public Object around(ProceedingJoinPoint proceedingJoinPoint, LogPrint logPrint) throws Throwable {
            // 在目标方法执行前的操作
            System.out.println("Before method execution");
    
            // 执行目标方法
            Object result = joinPoint.proceed();
    
            // 在目标方法执行后的操作
            System.out.println("After method execution");
    
            return result;
        }
    }
    复制代码

     

    复制代码
    The following examples show some common pointcut expressions:
    
    The execution of any public method:
    
    execution(public * *(..))
    The execution of any method with a name that begins with set:
    
    execution(* set*(..))
    The execution of any method defined by the AccountService interface:
    
    execution(* com.xyz.service.AccountService.*(..))
    The execution of any method defined in the service package:
    
    execution(* com.xyz.service.*.*(..))
    The execution of any method defined in the service package or one of its sub-packages:
    
    execution(* com.xyz.service..*.*(..))
    Any join point (method execution only in Spring AOP) within the service package:
    
    within(com.xyz.service.*)
    Any join point (method execution only in Spring AOP) within the service package or one of its sub-packages:
    
    within(com.xyz.service..*)
    Any join point (method execution only in Spring AOP) where the proxy implements the AccountService interface:
    
    this(com.xyz.service.AccountService)
    复制代码

     

@Aspect

  • @Aspect注解表示该类是一个切面类

@ConfigurationProperties(prefix = "mail")

复制代码
@ConfigurationProperties(
    prefix = "mail"
)
@Configuration
public class MailConfig {
  private String address; application.yml mail: address: ABC.com
复制代码

@PropertySources

 多个配置源,后者覆盖前者。

复制代码
@ConfigurationProperties(prefix = "mailconfig")
@PropertySources(
        @PropertySource(
                value = "classpath:mailconfig.yml",
                ignoreResourceNotFound = true,
                factory = YMLPropertySourceFactory.class)
)
@Component
public class MailProperties {
复制代码

 

@PostConstruct

  • https://www.jb51.net/program/293449iig.htm
  • 在bean的依赖注入完成之后被调用

 

posted @   A-P-I  阅读(17)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示