Springboot 热部署
Springboot为开发者提供了一个名叫 spring-boot-devtools来使Springboot应用支持热部署,提供开发者的开发效率,无需手动重启Spring Boot应用

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>

  

导入配置文件处理器,以后会出现提示

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-configuration-processor -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>

  

注意: Properties配置文件在idea中默认utf-8可能会乱码

配置文件中,我们可以自己制定配置数据源
通过type来选择使用哪种数据库

Springboot自动配置原理
1. Springboot启动的时候加载主配置类,开启了自动配置功能@EnableAutoConfiguration
2. @EnableAutoConfiguration的作用
3.

@Value获取值和@ConfigurationProperties获值比较

配置文件是yml还是properties 他们都能够获取到值
如果说,我们只是在某个业务逻辑领域,需要获取一下配置文件中的某项值,使用@Value
如果说,我们专门编写了一个javaBean来和配置文件进行映射,我们就直接使用@ConfigurationProperties

@ConfigurationProperties
该注解可以将配置文件中配置的每一个属性的值,通过set方法映射到被注释的组件中(因此不可以缺少setter方法)

/**
* 批量注入、松散绑定、数据校验、复杂类型封装
*/
@Component
@ConfigurationProperties(prefix = "person1") //批量注入
@Validated //数据校验
public class Person {
@Email
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;//复杂类型封装

public void setLastName(String lastName) {
this.lastName = lastName;
}
......
}
public class Dog {
private String name;
private Integer age;
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
}

applicaton.yml

person1:
lastName: 888888@qq.com
#以下二种写法与上述等同(松散绑定):
#last-name: 88888@qq.com
#last_name: 88888@qq.com
age: 18
boss: false
birth: 2018/04/04
maps: {k1: v1,k2: v2}
lists:
- lisi
- zhaoliu
dog:
name: 小狗
age: 12

@Value
@Component
@Validated
public class Person {
@Email//数据校验无效
@Value("${person1.lastName}")//从环境变量、配置文件中获取值
private String lastName;
@Value("#{11*2}")//使用SpEL表达式
private Integer age;
@Value("true")//直接赋值
private Boolean boss;
private Date birth;
@Value("${person1.maps}")//不支持复杂类型,报错
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
......
}

@PropertySource、@ImportResource、@Bean
//指明当前类为配置类,替代之前的Spring配置文件
@Configuration
//加载指定的配置文件
@PropertySource(value = {"classpath:person.properties"})
//SpringBoot项目没有Spring的配置文件,若要想自己编写Spring配置文件加载并生效,需要使用@ImportResoure注解标注在一个配置类上
@ImportResource(locations = {"classpath:application.xml"})
public class MyAppConfig {

@Bean //注册bean,默认是方法名作为id
public Person person() {
return new Person();
}
}

SpringBoot给容器中添加组件的方式(推荐使用全注解的方式):

配置类@Configuration标注@ImportResoure加载Spring配置文件
使用@Bean给容器中添加组件
3.3 配置文件属性
3.3.1 随机数
RandomValuePropertySource:配置文件中可以使用随机数

${random.value}:随机数字与字母组合的字符串
${random.uuid}:随机uuid
${random.long}:随机long值
${random.int}:随机int值
${random.int(value)}:0~value之间的随机数
${random.int(value,max)}:value~max之间的随机数
这些随机数可以使用在配置文件或@Value注解中

person1:
lastName: ${random.value}
age: ${random.int(0,100)}
#如果birthday不存在使用默认值2014/1/1
birth: ${birthday:2014/1/1}

3.3.2 profile
Profile是Spring对不同环境提供不同配置功能的支持,可以通过激活、指定参数等方式快速切换环境,多环境有以下二种方式:

多个profile文件:格式是application-{profile}.properties/yml,例如application-dev.properties,SpringBoot默认加载application.propeties/yml配置文件,可以在该文件中激活不同的profile:spring.profiles.active=dev
多个profile文档块模式application.yml:

#激活指定环境

spring:
profiles:
active: dev

---

#开发环境

spring:
profiles: dev
server:
port: 8090

---

#生产环境

spring:
profiles: product
server:
port: 9090

---

#默认环境

spring:
profiles: default
server:
port: 8080

配置环境也可以在外部文件、命令行或jvm参数中指定,如使用命令行(- -spring.profiles.active=dev)或jvm参数(-Dspring.profiles.active=dev)来激活指定的profile。

3.3.3 配置文件加载顺序
springboot 启动会扫描以下位置的application.properties或者application.yml文件作为Spring boot的默认配置文件

file:./config/
file:./
classpath:/config/
classpath:/
上述优先级由高到低,高优先级的配置会覆盖低优先级的配置,同时配置互补。项目打包好以后,我们可以使用命令行参数的形式,启动项目的时候来可以使用spring.config.location指定外部的配置文件位置:

java -jar spring-boot-0.0.1-SNAPSHOT.jar --spring.config.location=D:/application.properties
1
springboot的所有配置都可以在命令行上指定,多个配置使用空格分开,spring配置的优先级由高到低:

命令行参数
来自java:comp/env的JNDI属性
Java系统属性(System.getProperties())
操作系统环境变量
RandomValuePropertySource配置的random.*属性值
jar包外部的application-{profile}.properties或application.yml(带spring.profile)配置文件
jar包内部的application-{profile}.properties或application.yml(带spring.profile)配置文件
jar包外部的application.properties或application.yml(不带spring.profile)配置文件
jar包内部的application.properties或application.yml(不带spring.profile)配置文件
@Configuration注解类上的@PropertySource
通过SpringApplication.setDefaultProperties指定的默认属性
4. 自动配置原理
SpringBoot启动的时候加载主配置类(@SpringBootApplication),开启了自动配置功能@EnableAutoConfiguration
@EnableAutoConfiguration的组合注解@AutoConfigurationPackage将该配置类包及子包路径下的所有组件扫描进Spring容器中
@EnableAutoConfiguration的组合注解 @Import(AutoConfigurationImportSelector.class)注解导入AutoConfigurationImportSelector类,AutoConfigurationImportSelector类实现了DeferredImportSelector接口重写了selectImports方法,SpringFactoriesLoader.loadFactoryNames()扫描所有jar包类路径下 META-INF/spring.factories把扫描到的这些文件的内容包装成List对象,selectImports将这些自动配置类注册到容器中
4.1 自动配置类案例分析
以HttpEncodingAutoConfiguration 这个自动配置类为例:

@Configuration //标注该类为配置类
@EnableConfigurationProperties(HttpEncodingProperties.class) //启动指定类的ConfigurationProperties功能,将配置文件中对应的值和HttpEncodingProperties绑定起来,并把HttpEncodingProperties加入到ioc容器中
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) //只有基于Servlet的web环境配置类才会生效
@ConditionalOnClass(CharacterEncodingFilter.class) //判断当前项目有没有CharacterEncodingFilter这个类,CharacterEncodingFilter是SpringMVC中进行乱码解决的过滤器
@ConditionalOnProperty(prefix = "spring.http.encoding", value = "enabled", matchIfMissing = true)//判断配置文件中是否存在spring.http.encoding.enabled这个配置,如果缺失则默认为true
public class HttpEncodingAutoConfiguration {

private final HttpEncodingProperties properties;
//将与SpringBoot配置文件映射过的HttpEncodingProperties注入
public HttpEncodingAutoConfiguration(HttpEncodingProperties properties) {
this.properties = properties;
}
//若容器中没有CharacterEncodingFilter这个组件就注入该bean
@Bean
@ConditionalOnMissingBean(CharacterEncodingFilter.class)
public CharacterEncodingFilter characterEncodingFilter() {
CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
filter.setEncoding(this.properties.getCharset().name());
filter.setForceRequestEncoding(this.properties.shouldForce(HttpEncodingProperties.Type.REQUEST));
filter.setForceResponseEncoding(this.properties.shouldForce(HttpEncodingProperties.Type.RESPONSE));
return filter;
}

@Bean
public LocaleCharsetMappingsCustomizer localeCharsetMappingsCustomizer() {
return new LocaleCharsetMappingsCustomizer(this.properties);
}

private static class LocaleCharsetMappingsCustomizer implements
WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>, Ordered {

private final HttpEncodingProperties properties;

LocaleCharsetMappingsCustomizer(HttpEncodingProperties properties) {
this.properties = properties;
}

@Override
public void customize(ConfigurableServletWebServerFactory factory) {
if (this.properties.getMapping() != null) {
factory.setLocaleCharsetMappings(this.properties.getMapping());
}
}

@Override
public int getOrder() {
return 0;
}
}
}

HttpEncodingProperties类映射配置文件中以spring.http.encoding为前缀的配置

@ConfigurationProperties(
prefix = "spring.http.encoding"
)
public class HttpEncodingProperties {
public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;

/**
* Charset of HTTP requests and responses. Added to the "Content-Type" header if not
* set explicitly.
*/
private Charset charset = DEFAULT_CHARSET;

/**
* Whether to force the encoding to the configured charset on HTTP requests and
* responses.
*/
private Boolean force;

/**
* Whether to force the encoding to the configured charset on HTTP requests. Defaults
* to true when "force" has not been specified.
*/
private Boolean forceRequest;

/**
* Whether to force the encoding to the configured charset on HTTP responses.
*/
private Boolean forceResponse;

/**
* Locale in which to encode mapping.
*/
private Map<Locale, Charset> mapping;
.......
}

application.yml

#yml中可配置的属性也都是根据这些类中的属性来指定
spring:
http:
encoding:
enabled: true
charset: utf-8
force: true
force-request: true
force-response: true
mapping: UTF-8

Springboot常用的模板引擎
JSP、Velecity、Freemaker、Thymeleaf

jdbcTemplate

Spring boot Value注入 未整理 待完善的更多相关文章

  1. spring boot 配置注入

    spring boot配置注入有变量方式和类方式(参见:<spring boot 自定义配置属性的各种方式>),变量中又要注意静态变量的注入(参见:spring boot 给静态变量注入值 ...

  2. Spring Boot动态注入删除bean

    Spring Boot动态注入删除bean 概述 因为如果采用配置文件或者注解,我们要加入对象的话,还要重启服务,如果我们想要避免这一情况就得采用动态处理bean,包括:动态注入,动态删除. 动态注入 ...

  3. spring boot servlet 注入

    spring boot 注入servlet的方法是借助ServletRegistrationBean这个类 例子如下: 先建一个servlet import java.io.IOException; ...

  4. spring boot thymeleaf 标签未关闭报错

    每天学习一点点 编程PDF电子书免费下载: http://www.shitanlife.com/code spring boot,input标签未关闭报bug,代码稍有不慎就出小问题,后来百度,goo ...

  5. 关于spring boot自动注入出现Consider defining a bean of type 'xxx' in your configuration问题解决方案

    搭建完spring boot的demo后自然要实现自动注入来体现spring ioc的便利了,但是我在实施过程中出现了这么一个问题,见下面,这里找到解决办法记录下来,供遇到同样的问题的同僚参考 Des ...

  6. spring boot 1.x配置,不断完善中

    spring boot是典型的约定大于配置,那么很有必要对在开发过程中这些配置做统一的添加记录,以免用到的时候到处搜索,网上的东西又良莠不齐. server.port=8880 微服务注册中心,yml ...

  7. Spring Boot 1.5.* 升级 2.1 - 完善中

    Spring Boot 原版本 1.5.12.RELEASE 新版本 2.1.0.RELEASE Spring Cloud 原版本 Edgware.SR3 新版本 Finchley.SR2 一.Act ...

  8. Spring Boot中注入配置文件application.properties中的list 对象参数

    例如要注入下列参数: dyn.spring.datasources[0].name=branchtadyn.spring.datasources[0].driverClassName=oracle.j ...

  9. Spring Boot - 依赖注入

    @Autowired 查找被注解的变量类型,找到所有此类型的构建或此类型子类的构建 如果一个也没有找到,看required参数,false则用null,true则失败(默认,即spring会启动失败) ...

随机推荐

  1. 通过junit/TestNG+java 实现自动化测试

    第一步 安装JDK JDk1.7. 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-188026 ...

  2. 一起学习在 Ubuntu 上授予和移除 sudo 权限

    如你所知,用户可以在 Ubuntu 系统上使用 sudo 权限执行任何管理任务.在 Linux 机器上创建新用户时,他们无法执行任何管理任务,直到你将其加入 sudo 组的成员.在这个简短的教程中,我 ...

  3. 关于Eclipse新建Dynamic Web Projecj默认未创建web.xml的问题

    当使用eclipse新建Dynamic Web Projecj时,由于J2EE技术规范的更新,当使用Dynamic web module version默认版本为3.0时,将默认不会创建web.xml ...

  4. JavaWeb中的资源映射

    一./与/* <url-pattern>/</url-pattern>  会匹配到/login这样的路径型url,不会匹配到模式为*.jsp这样的后缀型url< url- ...

  5. android 颜色值参考,(有颜色图

    ) 2011-10-13 19:55:30| 分类: android | 标签:android颜色值|字号大中小 订阅 Android 常用RGB值以及中英文名称   颜 色 RGB值 英文名 中文名 ...

  6. 写给大忙人的nginx核心配置详解

    由于当前很多应该都是前后端分离了,同时大量的基于http的分布式和微服务架构,使得很多时候应用和不同项目组之间的系统相互来回调用,关系复杂.如果使用传统的做法,都在应用中进行各种处理和判断,不仅维护复 ...

  7. Java线程池详解,看这篇就够了!

    构造一个线程池为什么需要几个参数?如果避免线程池出现OOM?Runnable和Callable的区别是什么?本文将对这些问题一一解答,同时还将给出使用线程池的常见场景和代码片段. 基础知识 Execu ...

  8. python简说(二)list

    一.list # 1.list 列表 数组a = ['A', 'B', 'C', 'D']# 0 1 2# 2.空list# a = []# a = list()# 3.下标 角标 索引# print ...

  9. openvas漏洞扫描

    openvas配置步骤 1.-因为老师给的kali中自带的openvas,所以我们可以直接执行命令:openvas-check-setup来查看下他的安装状态: 如下图所示:在步骤7中出现错误,其中图 ...

  10. OO课程第四次总结

    终于来到了最后一次的OO作业,以博客作业的形式来终结也是极好的,回顾一下过去十六周自己的经历,感慨颇深. 测试和正确性论证 简单来说,测试的目的是将程序的代码做到全覆盖,从而确保每个分支都运行一遍,进 ...