java注解

96 阅读3分钟

用来记录做项目中碰见到的一些注解使用会慢慢补充

@PreDestroy

import org.springframework.context.annotation.Configuration;

import javax.annotation.PreDestroy;
@Configuration
public class MyBean {  
    private SomeResource resource;  
  
    public MyBean() {  
        resource = new SomeResource();  
    }  
    @PreDestroy  
    public void cleanup() {  
        System.out.println("对象即将被销毁,正在执行清理工作...");  
        resource.close();  
    }  
    // 其他方法...  
}

在这个示例中,MyBean类是一个由容器管理的bean(在此示例中,它是一个托管bean,由Jakarta EE容器管理)。当这个bean的实例即将被销毁时cleanup方法会被自动调用,从而允许我们执行必要的清理工作。 需要注意的是,@PreDestroy注解的方法应该没有任何参数,并且返回void。此外,该方法不能抛出已检查的异常。`

@ConfigurationProperties

用于将外部配置(通常是application.propertiesapplication.yml文件)映射到Java对象。这使得从配置文件中读取属性变得更加简单和直观。 你可以创建一个Java类来映射这些属性:

@Component  
@ConfigurationProperties(prefix = "wx.mp")  
public class WxMpProperties {  

    private String appId;  

    private String appSecret;  
    // getters and setters  

}
wx.mp.appId=yourAppId 
wx.mp.appSecret=yourAppSecret

在上面的代码中,WxMpProperties类的appId属性会映射到wx.mp.appId,而appSecret属性会映射到wx.mp.appSecret。 当Spring Boot应用程序启动时,它会自动将这些配置值注入到WxMpProperties类的相应属性中。这样,你就可以在应用程序的其他部分使用这个类来访问这些配置值,而无需直接读取配置文件。

为了使@ConfigurationProperties注解工作,你可能还需要在启动类或配置类上添加@EnableConfigurationProperties注解,或者在@ConfigurationProperties标注的类上使用@Component注解,以便Spring能够扫描并注册这个类。

更新

可以映射数组

@Data
@ConfigurationProperties(prefix = "wx.mp")
public class WxMpProperties {

    private String names;

    private List<MpConfig> configs;
    @Data
    public static class MpConfig {

        private String appId;

        private String secret;

        private String token;

        private String aesKey;
    }
}
wx:
  mp:
    names: 123
    configs:
      - appId: ${appId}
        secret: ${secret}
        token: ${token}
        aesKey: ${aesKey}
        # 第二组
       - appId: ${appId}
        secret: ${secret}
        token: ${token}
        aesKey: ${aesKey}

@ComponentScan

@ComponentScan 是 Spring 框架中用于配置组件扫描的注解。其主要作用是在指定的包(或包路径)中自动发现并注册由 @Component@Service@Repository@Controller@Configuration 等注解标记的类为 Spring 应用上下文中的 bean。

@Configuration
@ComponentScan(basePackages = "com.annotation.service"
        ,excludeFilters =
@ComponentScan.Filter(
        //        默认过滤条件为 注解,指示出过滤注解的名称.
        type = FilterType.ANNOTATION,value = Service.class
))
public class JavaConfig {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(JavaConfig.class);
        for (String definitionName : context.getBeanDefinitionNames()) {
            System.out.println(definitionName);
        }
    }
}
  • basePackages指定扫描包的路径
  • excludeFilters指定按照某种规则排除一些指定的类型
  • includeFilters指定要包含指定的类型
  • FilterType.ANNOTATION指定过滤的类型为注解类型

上述的意思就是:从com.annotation.service包下扫描所有的bean然后根据注解类型来过滤,排除掉打了@Service的注解,不注入到容器中去.

@Configuration
@ComponentScan(basePackages = "com.annotation.service"
        ,useDefaultFilters = false  // 因为他有默认的过滤方式,所以要排除默认的过滤方式,哪怕StudentService类没有被一些注解标记,也是会被注入的,只要是一个类
        ,includeFilters =
@ComponentScan.Filter(
        //       使用类型来排除,包含某一个类
        type = FilterType.ASSIGNABLE_TYPE,value = StudentService.class
))
public class JavaConfig2 {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(JavaConfig2.class);
        for (String definitionName : context.getBeanDefinitionNames()) {
            System.out.println(definitionName);
        }
    }
}
  • FilterType.ASSIGNABLE_TYPE指定过滤的类型为类
  • useDefaultFilters是否使用默认的过滤规则,默认的过滤规则为:注解规则既FilterType.ANNOTATION

上述意思是: 扫描com.annotation.service包下的bean,不使用默认的过滤规则来过滤(指示是否应启用对用 @Component @Repository、@Service 或 @Controller 批注的类的自动检测),说人话就是不会把这些注解注释的对象加载到容器中.注意,StudentService类本身不需要被任何Spring特定的注解(如@Service)标记,因为是通过includeFilters来明确指定它的。但是,如果StudentService类上确实有@Service或其他Spring组件注解,那么它仍然会被注册,因为includeFilters的优先级高于默认的过滤器.