美文网首页
springboot

springboot

作者: 涂森魄hl | 来源:发表于2018-12-17 16:20 被阅读0次

@Component

@ConfigurationProperties(prefix="person")

publicclassPerson{

}

需要添加依赖才有提示

dependencies{

   optional "org.springframework.boot:spring-boot-configuration-processor"

}

properties乱码问题

@Value和@ConfigurationProperties取值的不同

@ConfigurationProperties@Value

功能批量注入配置的属性值一个一个指定

松散语法(下划线转和字母大写变量名可识别)支持不支持

SpEL不支持支持

JSR303数据校验支持不支持

@PropertySource和@ImportSource

@PropertySource(value={"classpath:xxx.properties"}),加载另外的配置文件

@ImportResource(locations={"classpath:xxx.xml"}):导入Spring的配置文件,用在配置类上

xml文件替换成配置类

@Configuration

publicclassMyAppConfig{

//<bean></bean>将方法的返回值添加到容器中,容器中该组件的默认id就是方法名

@Bean

publicHelloServicehelloService(){

returnnewHelloService();

   }

}

配置文件占位符

${random.uuid}

${user.name:默认值}

profiles标识

不同环境可以使用不同的配置文件

application.properties

spring.profiles.active=dev  #激活dev环境的配置

application-dev.properties

springboot配置文件加载顺序

file:./config/

file:./

classpath:./config/

classpath:/

优先级由高到低,高的会覆盖低的

自动配置原理

1)、Springboot启动时加载主配置类,@SpringBootApplication中启用了自动配置@EnableAutoConfiguration

2)、在@EnableAutoConfiguration中用@Import(AutoConfigurationImportSelector.class)导入组件

3)、在AutoConfigurationImportSelector类中有一个selectImports方法

4)、在getAutoConfigurationEntry中调用getCandidateConfigurations从springfactory加载工厂

5)、loadFactoryNames中返回loadFactoryNames从FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";读取EnableAutoConfiguration.class的内容包装成properties对象

根据不同的条件判断决定该配置类是否生效

@Configuration//这是一个配置类

@EnableConfigurationProperties(HttpProperties.class)//启用制定类的ConfigurationProperties功能

@ConditionalOnWebApplication(type=ConditionalOnWebApplication.Type.SERVLET)//spring底层@Conditional注解,根据不同的条件,如果满足条件该配置类生效

@ConditionalOnClass(CharacterEncodingFilter.class)//判断当前项目有没有这个类

@ConditionalOnProperty(prefix="spring.http.encoding",value="enabled",matchIfMissing=true)//判断配置文件是否存在这些配置,matchIfMissing如果不存在还是返回true

publicclassHttpEncodingAutoConfiguration{

    privatefinalHttpProperties.Encodingproperties;

    publicHttpEncodingAutoConfiguration(HttpPropertiesproperties) {

        this.properties=properties.getEncoding();

    }

    @Bean//给容器中添加这个组件

    @ConditionalOnMissingBean//判断容器中没有这个组件时

    publicCharacterEncodingFiltercharacterEncodingFilter() {

        CharacterEncodingFilterfilter=newOrderedCharacterEncodingFilter();

        filter.setEncoding(this.properties.getCharset().name());

        filter.setForceRequestEncoding(this.properties.shouldForce(Type.REQUEST));

        filter.setForceResponseEncoding(this.properties.shouldForce(Type.RESPONSE));

        returnfilter;

    }

    @Bean

    publicLocaleCharsetMappingsCustomizerlocaleCharsetMappingsCustomizer() {

        returnnewLocaleCharsetMappingsCustomizer(this.properties);

    }

    privatestaticclassLocaleCharsetMappingsCustomizerimplements

            WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>,Ordered{

        privatefinalHttpProperties.Encodingproperties;

        LocaleCharsetMappingsCustomizer(HttpProperties.Encodingproperties) {

            this.properties=properties;

        }

        @Override

        publicvoidcustomize(ConfigurableServletWebServerFactoryfactory) {

            if(this.properties.getMapping()!=null) {

                factory.setLocaleCharsetMappings(this.properties.getMapping());

            }

        }

        @Override

        publicintgetOrder() {

            return0;

        }

    }

}

自定义错误信息继承DefaultErrorAttributes重写getErrorAttributes方法调用super.getErrorAttributes将自己的错误信息写入map返回将map放入request域取出异常处理器中的信息(Map<String,Object>)requestAttributes.getAttribute("ext",0);

嵌入式servlet

修改tomcat配置

注册servlet(localhost:8080/myServlet)

@Bean

publicServletRegistrationBeanmyServlet(){

ServletRegistrationBeanservletbean=newServletRegistrationBean(newMyServlet(),"/myServlet");

returnservletbean;

}

注册filter

@Bean

publicFilterRegistrationBeanmyFilter(){

    FilterRegistrationBeanfilterbean=newFilterRegistrationBean();

    filterbean.setFilter(newMyFilter());//注册过滤器

    filterbean.setUrlPatterns(Array.asList("/hello","/myServlet"));//过滤url

    returnfilterbean;

}

相关文章

网友评论

      本文标题:springboot

      本文链接:https://www.haomeiwen.com/subject/mpdckqtx.html