SpringBoot(二)配置详解
2018-12-24 17:06
85 查看
版权声明:本博客文章均来源于风尘博客,敬请关注https://www.dustyblog.cn https://blog.csdn.net/weixin_42036952/article/details/85236104
2.2其次定义
3.1定义一个名为
3.2其次定义
3.3接下来在
SpringBoot是为了简化Spring应用的创建、运行、调试、部署等一系列问题而诞生的产物,自动装配的特性让我们可以更好的关注业务本身而不是外部的XML配置,我们只需遵循规范,引入相关的依赖就可以轻易的搭建出一个 WEB 工程。
一、配置依赖
- 首先,跟上文一样建立一个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>
- 然后倒入配置所需的依赖:
<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
相关文章推荐
- spring boot application properties配置详解
- spring-boot-starter-logging logback配置之<configuration><logger>标签详解
- 详解SpringBoot配置连接池
- 详解Spring Boot配置文件之多环境配置
- (转)Spring boot——logback.xml 配置详解(三)<appender>
- Spring Boot日志配置详解
- Spring boot外部配置(配置中心化)详解
- 详解Spring Boot Profiles 配置和使用
- Spring boot(2)-配置详解
- SpringBoot2.x 整合Druid数据源(详解及配置配置)
- SpringBoot非官方教程 | 第二篇:Spring Boot配置文件详解
- Spring Boot基础3-配置文件详解:Properties和YAML
- SpringBoot中使用UEditor基本配置(图文详解)
- 详解Spring Boot 配置加载顺序及属性加载顺序
- Spring Boot中使用MyBatis注解配置详解
- Spring Boot属性配置文件详解
- (转)Spring boot——logback.xml 配置详解(二)
- 详解SpringBoot初始教程之Tomcat、Https配置以及Jetty优化
- 详解用Spring Boot零配置快速创建web项目
- Spring Cloud Spring Boot mybatis分布式微服务云架构(四)属性配置文件详解(2)