您的位置:首页 > 编程语言 > Java开发

【springboot 入门篇】第2篇 springboot的各种配置

2017-09-21 00:46 791 查看

1. 配置文件简介

spring boot使用一个全局配置文件:application.properties或者application.yml,放置在src/main/resources目录下或者类路径的/config目录下。

application.properties是我们熟知的键值对配置文件:



application.yml是yaml语言的配置文件,yaml是一种以数据为中心的语言,在配置数据的时候具有面向对象的特征。我们以后的代码都会使用yml格式的配置文件。:



spring boot 的全局配置文件的作用是对一些默认配置的配置进行修改。后面我们会详细的讲解。

2. pom配置文件(starter)

pom,是maven的配置文件,gradel也差不多,因为我一直在用maven,所以就讲maven了。

为了解决大型项目以及一些常用组件jar包过多的问题,spring boot 为我们提供了许多starter pom,每一个pom都已经添加好了对应的依赖,以往我们需要写茫茫多的dependence,但是现在只需要一个starter就可以了,并且spring boot 还为它们提供了默认的配置和自动配置的bean。

2.1 官方starter

2.1.1 应用程序的starters

名称描述
spring-boot-starter核心Spring Boot starter,包括自动配置支持,日志和YAML
spring-boot-starter-actuator生产准备的特性,用于帮我们监控和管理应用
spring-boot-starter-amqp对”高级消息队列协议”的支持,通过spring-rabbit实现
spring-boot-starter-aop对面向切面编程的支持,包括spring-aop和AspectJ
spring-boot-starter-batch对Spring Batch的支持,包括HSQLDB数据库
spring-boot-starter-cloud-connectors对Spring Cloud Connectors的支持,简化在云平台下(例如,Cloud Foundry 和Heroku)服务的连接
spring-boot-starter-data-elasticsearch对Elasticsearch搜索和分析引擎的支持,包括spring-data-elasticsearch
spring-boot-starter-data-gemfire对GemFire分布式数据存储的支持,包括spring-data-gemfire
spring-boot-starter-data-jpa对”Java持久化API”的支持,包括spring-data-jpa,spring-orm和Hibernate
spring-boot-starter-data-mongodb对MongoDB NOSQL数据库的支持,包括spring-data-mongodb
spring-boot-starter-data-rest对通过REST暴露Spring Data仓库的支持,通过spring-data-rest-webmvc实现
spring-boot-starter-data-solr对Apache Solr搜索平台的支持,包括spring-data-solr
spring-boot-starter-freemarker对FreeMarker模板引擎的支持
spring-boot-starter-groovy-templates对Groovy模板引擎的支持
spring-boot-starter-hateoas对基于HATEOAS的RESTful服务的支持,通过spring-hateoas实现
spring-boot-starter-hornetq对”Java消息服务API”的支持,通过HornetQ实现
spring-boot-starter-integration对普通spring-integration模块的支持
spring-boot-starter-jdbc对JDBC数据库的支持
spring-boot-starter-jersey对Jersey RESTful Web服务框架的支持
spring-boot-starter-jta-atomikos对JTA分布式事务的支持,通过Atomikos实现
spring-boot-starter-jta-bitronix对JTA分布式事务的支持,通过Bitronix实现
spring-boot-starter-mail对javax.mail的支持
spring-boot-starter-mobile对spring-mobile的支持
spring-boot-starter-mustache对Mustache模板引擎的支持
spring-boot-starter-redis对REDIS键值数据存储的支持,包括spring-redis
spring-boot-starter-security对spring-security的支持
spring-boot-starter-social-facebook对spring-social-facebook的支持
spring-boot-starter-social-linkedin对spring-social-linkedin的支持
spring-boot-starter-social-twitter对spring-social-twitter的支持
spring-boot-starter-test对常用测试依赖的支持,包括JUnit, Hamcrest和Mockito,还有spring-test模块
spring-boot-starter-thymeleaf对Thymeleaf模板引擎的支持,包括和Spring的集成
spring-boot-starter-velocity对Velocity模板引擎的支持
spring-boot-starter-web对全栈web开发的支持, 包括Tomcat和spring-webmvc
spring-boot-starter-websocket对WebSocket开发的支持
spring-boot-starter-ws对Spring Web服务的支持
- 2.1.2 spring boot生产准备的starters:

名称描述
spring-boot-starter-actuator添加生产准备特性,比如指标和监控
spring-boot-starter-remote-shell添加远程ssh shell支持
- 2.1.3 排除或交换具体技术方面的starters

名称描述
spring-boot-starter-jetty导入Jetty HTTP引擎(作为Tomcat的替代)
spring-boot-starter-log4j对Log4J日志系统的支持
spring-boot-starter-logging导入Spring Boot的默认日志系统
spring-boot-starter-tomcat导入Spring Boot的默认HTTP引擎
spring-boot-starter-undertow导入Undertow HTTP引擎(作为Tomcat的替代)

2.2 非官方starter

也有一些第三方为spring boot 提供了starter:

Handlebars

Vaadin

Apache Camel

WRO4J

Spring Batch(高级用法)

HDIV

Jade Templates(Jade4J)

Actitivi

这些都可以在github上找到,感兴趣的可以去搜一下。

3. xml配置文件

spring boot 提倡零配置,也就是无xml配置,但是在实际项目中,可能有一些要求必须使用xml配置,这时我们可以使用spring提供的@ImportResource 来加载xml配置,比如:

4000

@ImportResource({"classpath:some-context.xml","classpath:other-context.xml"})
1


[/code]

4. 命令行参数配置

上一篇文章中也有提到,spring boot 项目是可以打成jar包的,然后通过
java -jar xxx.jar
来执行,我们都知道,main函数是可以接受参数的,同样,spring boot的main函数也可以。

我们可以通过下面的命令来修改Tomcat的端口号:

java -jar xxx.jar --server.port=9090
1


[/code]

5. 自定义属性配置

5.1 普通配置

在写spring项目的时候,我们可以在properties文件中定义一个变量,然后再代码中通过 @PropertySource 指明 properties 文件的位置,然后通过@Value注入相应的值。

在spring boot 中我们同样可以自定义一个变量,因为spring boot中的全局配置文件默认在一个目录下,所以我们可以直接用@Value注入值。

比如,在上一篇文章的demo项目中,我们可以在application.yml文件中这样定义:

key:
hello: hello world
1
2


[/code]

修改DemoApplication.java代码为:

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
@RestController
public class DemoApplication {

@Value("${key.hello}")
private String hello;

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

@RequestMapping("/hello")
public String hello(){
return hello;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16


[/code]

运行项目,在浏览器中访问http://localhost:8080/hello



值被正常的注入进去了。

5.2 类型安全的配置

上面的例子中,使用@Value注入每个配置在实际的应用中会显得格外的麻烦,因为配置一般会有很多,用上面的方式就要写好多@Value,并且如果在多个类文件中都使用这个值的话,工作量会更大。所以,需要一种更好的方式。并且,这种方式是有安全隐患的,比如我需要的是一个long类型,但是注入的却是String,这个时候就报错了:

Failed to convert value of type 'java.lang.String' to required type 'long';
1


[/code]

spring boot 提供了基于类型安全的配置方式,通过@ConfigurationProperties将properties属性和一个bean及其属性关联,从而实现类型安全的配置。

我们来看一下:

application.yml:

key:
name: cleverfan
sex: man
1
2
3


[/code]

在demo项目中创建一个class: Person.java

@Component
@ConfigurationProperties(prefix = "key")
public class Person {
private String name;
private String sex;

public void setName(String name) {
this.name = name;
}

public void setSex(String sex) {
this.sex = sex;
}

public String getName() {
return name;
}

public String getSex() {
return sex;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23


[/code]

修改DemoApplication.java :

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
@RestController
public class DemoApplication {

@Autowired
private Person person;

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

@RequestMapping("/hello")
public String hello(){
return person.getName() + ": " + person.getSex();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17


[/code]

启动项目,访问http://localhost:8080/hello



回到刚刚的问题,如果这个时候我需要的是long,但是给的是String怎么办呢?这里给出一个很简单的处理方式:

Person.java

@Component
@ConfigurationProperties(prefix = "key")
public class Person {
private String name;
private long sex;

public void setName(String name) {
this.name = name;
}

public void setSex(String sex) {
try{
this.sex = Long.valueOf(sex);
}catch (Exception e){
this.sex = 0L;
}

}

public String getName() {
return name;
}

public long getSex() {
return sex;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28


[/code]

yml配置文件不变,我们传递的是字符串,这次重启项目,访问http://localhost:8080/hello



这样就可以简单的保证数据的安全了。

6. 日志配置

spring boot 支持Java Util Loggin、Log4J、Log4J2和LogBack作为日志框架,无论使用哪种日志框架,spring boot已为当前使用日志框架的控制台输出及文件输出做好了配置。

默认情况下,spring boot 使用 Logback作为日志框架。可以在配置文件中(yml)修改默认配置文件的配置:

f6a0
配置日志级别:

logging:
level: debug
1
2


[/code]

配置日志文件

logging:
org:
springframework:
web: debug
1
2
3
4


[/code]

7. Profile配置

Profile 是 spring 用来针对不同的环境对不同的配置提供支持的,比如生成环境,测试环境和开发环境。全局 Profile 使用application-{profile}.yml(例如application-prod.yml)命名。

下面我们做一个演示,我们把demo项目分为生产(prod)和开发(dev)环境,生产环境下端口号为8000,开发环境下为8888。

我们在demo项目的application.yml文件的同级目录下创建两个文件:

application-prod.yml:

server:
port: 8000
1
2


[/code]

application-dev.yml:

server:
port: 8888
1
2


[/code]

目录结构是这样的:



application.yml

key:
name: cleverfan
sex: man

logging: level: debug

spring:
profiles:
active: dev
1
2
3
4
5
6
7
8
9
10
11


[/code]

启动项目:



端口号是8888

修改application.yml:

spring:
profiles:
active: prod
1
2
3


[/code]

重新启动项目:



端口号的8000

证明我们的配置生效了。

配置就先讲到这里,如果有疑问或者错误,请及时与我联系。如果有需要补充的地方,也可以私戳我~

文章首发自简书

(function () {('pre.prettyprint code').each(function () {
var lines = (this).text().split(′\n′).length;varnumbering = $('').addClass('pre-numbering').hide();
(this).addClass(′has−numbering′).parent().append(numbering);
for (i = 1; i
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring-boot