Spring Boot 注解

341 阅读2分钟

实体类映射配置文件 application.properties

把配置文件的信息,读取并自动封装成实体类

配置文件示例

wechat.mp.appId= 11
wechat.mp.secret= 22
wechat.mp.token= 33
wechat.mp.aesKey= 44

方式1、实体类加注解 @ConfigurationProperties和@Component,使用时直接通过@Auotwired注入

实体类

将配置文件中的属性值映射到类属性上

@Component
@ConfigurationProperties(prefix = "wechat.mp") //prefix 设置前缀
public class WechatMpProperties {
  /**
   * 设置微信公众号的appid
   */
  private String appId;
  /**
   * 设置微信公众号的app secret
   */
  private String secret;
  /**
   * 设置微信公众号的token
   */
  private String token;
  /**
   * 设置微信公众号的EncodingAESKey
   */
  private String aesKey;

}

测试

@Log4j
@Controller
@RequestMapping("/wechat")
public class Test {

    @Autowired
    private WechatMpProperties wechatMpProperties;

    @RequestMapping("/testConfig")
    @ResponseBody
    public String testConfig(){
        log.info("wechatMpProperties: "+wechatMpProperties.getAppId());
        return "ceshi";
    }
}

结果:日志中打印出wechatMpProperties:11

方式2、实体类加注解 @ConfigurationProperties, 使用类加注解 @EnableConfigurationProperties 然后通过引用类变量来使用

实体类

将配置文件中的属性值映射到类属性上

@ConfigurationProperties(prefix = "wechat.mp") //prefix 设置前缀
public class WechatMpProperties {
  /**
   * 设置微信公众号的appid
   */
  private String appId;
  /**
   * 设置微信公众号的app secret
   */
  private String secret;
  /**
   * 设置微信公众号的token
   */
  private String token;
  /**
   * 设置微信公众号的EncodingAESKey
   */
  private String aesKey;

}

测试

@Log4j
@Controller
@EnableConfigurationProperties(WechatMpProperties.class)
@RequestMapping("/wechat")
public class Test {

    private WechatMpProperties wechatMpProperties;

    @RequestMapping("/testConfig")
    @ResponseBody
    public String testConfig(){
        log.info("wechatMpProperties: "+wechatMpProperties.getAppId());
        return "ceshi";
    }
}

结果:日志中打印出wechatMpProperties:11

方式3、 配置类中方法中使用@Bean 和 @ConfigurationProperties 注解

实体类

将配置文件中的属性值映射到类属性上

public class WechatMpProperties {
  /**
   * 设置微信公众号的appid
   */
  private String appId;
  /**
   * 设置微信公众号的app secret
   */
  private String secret;
  /**
   * 设置微信公众号的token
   */
  private String token;
  /**
   * 设置微信公众号的EncodingAESKey
   */
  private String aesKey;

}

测试

@Log4j
@Controller
@RequestMapping("/wechat")
public class Test {

    @Autowired
    private WechatMpProperties WechatMpProperties;

    @Bean
    @ConfigurationProperties(prefix = "wechat.mp")
    public WechatMpProperties wechatMpProperties(){
        return new WechatMpProperties();
    }
    
    @RequestMapping("/testConfig")
    @ResponseBody
    public String testConfig(){
        log.info("wechatMpProperties: "+wechatMpProperties.getAppId());
        return "ceshi";
    }
}

结果:日志中打印出wechatMpProperties:11

@ConfigurationProperties()属性

  • prefix 和 value 都是属性前缀
  • ignoreInvalidFields 是否忽略不合法的属性,如类型不匹配
  • ignoreUnknownFields 是否忽略不匹配的属性

声明配置类

@Configuration 声明当前类为配置类,相当于xml配置文件

@Bean 注解在方法上,声明当前方法返回值为一个Bean,名称为方法名,可通过调用方法来注入Bean,无需@Autowired注解或者作为参数来注入。

@Mapper 和 @MapperScan

@Mapper 用在Mapper接口类上
@MapperScan 用在启动类上,默认值是 basePackages

例:
@SpringBootApplication
@MapperScan("com.homework.blog")//扫描此包下的所有Mapper接口类
public class HomeworkApplication {

    public static void main(String[] args) {
        SpringApplication.run(HomeworkApplication.class, args);
    }
}