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

SpringBoot(二)配置详解

2018-12-24 17:06 85 查看
版权声明:本博客文章均来源于风尘博客,敬请关注https://www.dustyblog.cn https://blog.csdn.net/weixin_42036952/article/details/85236104

SpringBoot是为了简化Spring应用的创建、运行、调试、部署等一系列问题而诞生的产物,自动装配的特性让我们可以更好的关注业务本身而不是外部的XML配置,我们只需遵循规范,引入相关的依赖就可以轻易的搭建出一个 WEB 工程。

一、配置依赖

  1. 首先,跟上文一样建立一个SpringBoot项目,导入:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
  1. 然后倒入配置所需的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

该依赖只会在编译时调用,可以不添加,但是在 IDEA 和 STS 中不会有属性提示,所以为了便捷,一般加入该依赖。

二、自定义属性配置

2.1在application.properties写入如下配置内容:

my1.age=24
my1.name=Van

2.2其次定义
MyProperties1.java
文件,用来映射我们在
application.properties
中的内容,这样一来我们就可以通过操作对象的方式来获得配置文件的内容了.

2.3定义我们的PropertiesController用来注入MyProperties1测试我们编写的代码.

package com.van.dusty.controller;

import com.van.dusty.properties.MyProperties1;
import com.van.dusty.properties.MyProperties2;
import org.springframework.beans.factory.annotation.Autowired;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/properties")
@RestController
public class PropertiesController {

private static final Logger logger = LoggerFactory.getLogger(PropertiesController.class);

private final MyProperties1 myProperties1;

@Autowired
public PropertiesController(MyProperties1 myProperties1) {
this.myProperties1 = myProperties1;
}

@GetMapping("/test1")
public MyProperties1 getMyProperties1() {
final String method = "myProperties1";
logger.info("method = {},start",method);
logger.info(myProperties1.toString());
logger.info("method = {},end",method);
return myProperties1;
}
}

三、自定义文件配置

3.1定义一个名为
my2.properties
的资源文件,自定义配置文件的命名不强制application开头

my2.age=24
my2.name=Van
my2.email=1251787298@qq.com

3.2其次定义
MyProperties2.java
文件,用来映射我们在
my2.properties
中的内容。

package com.van.dusty.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@PropertySource("classpath:my2.properties")
@ConfigurationProperties(prefix = "my2")
public class MyProperties2 {

private int age;
private String name;
private String email;
// 省略 get set

@Override
public String toString() {
return "MyProperties2{" +
"age=" + age +
", name='" + name + '\'' +
", email='" + email + '\'' +
'}';
}
}

3.3接下来在
PropertiesController
用来注入
MyProperties2
测试我们编写的代码

private final MyProperties1 myProperties1;

private final MyProperties2 myProperties2;

@Autowired
public PropertiesController(MyProperties1 myProperties1, MyProperties2 myProperties2) {
this.myProperties1 = myProperties1;
this.myProperties2 = myProperties2;
}

@GetMapping("/test2")
public MyProperties1 getMyProperties2() {
final String method = "myProperties2";
logger.info("method = {},start",method);
logger.info(myProperties2.toString());
logger.info("method = {},end",method);
return myProperties1;
}

四、多环境化配置

真实的应用中,常常会有多个环境(如:开发,测试,正式),不同的环境数据库连接都不一样,这个时候就需要用到spring.profile.active的强大功能了,它的格式为application-{profile}.properties,这里的application为前缀不能改,{profile}是我们自己定义的。

  • application-dev.properties
server.servlet.context-path=/dev
  • application-test.properties
server.servlet.context-path=/test
  • application-prod.properties
server.servlet.context-path=/master

application.properties
配置文件中写入

spring.profiles.active=dev

这个时候我们在次访问http://localhost:8080/properties/test1就没用处了,因为我们设置了它的

context-path=/dev
,所以新的路径就是http://localhost:8080/dev/properties/test1,可以通过此方式切换环境。

五、项目源码

本项目源码:https://github.com/vanDusty/SpringBoot-Home/tree/master/SpringBoot-demo
个人博客:https://www.dustyblog.cn

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: