SpringBoot——属性注入

SpringBoot——启动与自动配置类查找中通过@EnableAutoConfiguration找到了自动配置类,然后通过自动配置类中的@Bean等注解,创建一个一个的Bean实例到IOC容器中,

但还有一个问题没有弄清楚:属性注入,这些Bean的默认属性是在什么时候放入进去的。写了一堆东西然后发现

错误的认识了spring-configuration-metadata.json以及additional-spring-configuration-metadata.json文件的作用。这两个文件是application配置的提示文件。

并不参与Bean的默认属性注入。

Bean的默认属性是在Java文件中或者注解中默认。例如:

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ Servlet.class, StandardServletMultipartResolver.class, MultipartConfigElement.class })
//如果spring.servlet.multipart.enabled未设置,默认为true ,matchIfMissing = true
@ConditionalOnProperty(prefix = "spring.servlet.multipart", name = "enabled", matchIfMissing = true)
@ConditionalOnWebApplication(type = Type.SERVLET)
@EnableConfigurationProperties(MultipartProperties.class)
public class MultipartAutoConfiguration {

    //MultipartProperties的默认属性都在MultipartProperties.java中
    private final MultipartProperties multipartProperties;

    public MultipartAutoConfiguration(MultipartProperties multipartProperties) {
        this.multipartProperties = multipartProperties;
    }

    @Bean
    @ConditionalOnMissingBean({ MultipartConfigElement.class, CommonsMultipartResolver.class })
    public MultipartConfigElement multipartConfigElement() {
        return this.multipartProperties.createMultipartConfig();
    }

    @Bean(name = DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME)
    @ConditionalOnMissingBean(MultipartResolver.class)
    public StandardServletMultipartResolver multipartResolver() {
        StandardServletMultipartResolver multipartResolver = new StandardServletMultipartResolver();
        multipartResolver.setResolveLazily(this.multipartProperties.isResolveLazily());
        return multipartResolver;
    }

}

MultipartProperties.java

//这个注解作用是跟application.properties中的spring.servlet.mutipart绑定,设置了就会覆盖默认值
@ConfigurationProperties(prefix = "spring.servlet.multipart", ignoreUnknownFields = false)
public class MultipartProperties {
    //Java类中属性都是用默认值的
    private boolean enabled = true;
    private String location;
    private DataSize maxFileSize = DataSize.ofMegabytes(1);//默认1MB
    private DataSize maxRequestSize = DataSize.ofMegabytes(10);//默认10MB
    private DataSize fileSizeThreshold = DataSize.ofBytes(0);//默认0B
    private boolean resolveLazily = false;

}

    public static DataSize ofMegabytes(long megabytes) {
        //new DataSize(megabytes * 1024 * 1024 B)
        return new DataSize(Math.multiplyExact(megabytes, BYTES_PER_MB));
    }

    public static DataSize ofBytes(long bytes) {
        //new DataSize(bytes  B)
        return new DataSize(bytes);
    }

至于spring-configuration-metadata.json以及additional-spring-configuration-metadata.json文件:仅仅只是配置application.properties文件时的提示文件

   //。。。 跟默认配置相同,但仅仅是提示作用,不参与Bean初始化,下面是截取的multipart的默认提示
   {
      "name": "spring.servlet.multipart.enabled",
      "type": "java.lang.Boolean",
      "description": "Whether to enable support of multipart uploads.",
      "sourceType": "org.springframework.boot.autoconfigure.web.servlet.MultipartProperties",
      "defaultValue": true
    },
    {
      "name": "spring.servlet.multipart.file-size-threshold",
      "type": "org.springframework.util.unit.DataSize",
      "description": "Threshold after which files are written to disk.",
      "sourceType": "org.springframework.boot.autoconfigure.web.servlet.MultipartProperties",
      "defaultValue": "0B"
    },
    {
      "name": "spring.servlet.multipart.location",
      "type": "java.lang.String",
      "description": "Intermediate location of uploaded files.",
      "sourceType": "org.springframework.boot.autoconfigure.web.servlet.MultipartProperties"
    },
    {
      "name": "spring.servlet.multipart.max-file-size",
      "type": "org.springframework.util.unit.DataSize",
      "description": "Max file size.",
      "sourceType": "org.springframework.boot.autoconfigure.web.servlet.MultipartProperties",
      "defaultValue": "1MB"
    },
    {
      "name": "spring.servlet.multipart.max-request-size",
      "type": "org.springframework.util.unit.DataSize",
      "description": "Max request size.",
      "sourceType": "org.springframework.boot.autoconfigure.web.servlet.MultipartProperties",
      "defaultValue": "10MB"
    },
    {
      "name": "spring.servlet.multipart.resolve-lazily",
      "type": "java.lang.Boolean",
      "description": "Whether to resolve the multipart request lazily at the time of file or parameter access.",
      "sourceType": "org.springframework.boot.autoconfigure.web.servlet.MultipartProperties",
      "defaultValue": false
    },
     //。。。

...

 

posted on 2020-03-25 13:49  FFStayF  阅读(414)  评论(0编辑  收藏  举报