Spring Boot(三) 配置文件

3.1、说明

Spring Boot为了更好的快速的启动和运行,遵行优先于配置的原则,其中Spring Boot默认加载src/main/Java/resources目录下application.properties或者application.yml,二者等同。

3.2、@Value读取配置文件

在application.properties新增 :

#运用名称
spring.application.name=spring-boot-demo
#端口号
server.port=9090
#可以修改项目路径
#server.context-path=/dy_bom
#@Value测试
value-word=Greetings from Spring Boot!

新建ConfigConctroller.java:

@RestController
public class ConfigController {
    //获取配置文件的value-word值,没有则默认test
    @Value("${value-word:test}")
    String words;

    @GetMapping("")
    public Object index() {
        return words;
    }
}

浏览器访问:http://localhost:9090
这里写图片描述

3.3、@ConfigurationProperties读取多前缀值

修改配置文件application.properties:

#运用名称
spring.application.name=spring-boot-demo
#服务器端口
server.port=9090
#@Value测试
value-word=Greetings from Spring Boot!
#ConfigurationProperties读取多前缀值
user.name=dy_bom
user.age=${random.int(30)}
user.uuid=${random.uuid}
hello.word=Hi,${user.name}!

新建ConfigController.java

@RestController
public class ConfigController {
    @Autowired
    UserBean userBean;

    @Value("${value-word:test}")
    String words;

    @Value("${hello.word:test}")
    String hello;

    @GetMapping("")
    public Object index() {
        return words;
    }

    @GetMapping("user_info")
    public Object getUserInfo(){
            return hello+"your message:"+userBean.toString();
    }
}

新建userBean:

@ConfigurationProperties(prefix = "user")
@Component
@Data
public class UserBean {
    private String uuid;
    private String name ;
    private int age;

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

浏览器访问:http://localhost:9090/user_info
这里写图片描述

3.4、读取其他配置文件,如自定义等

我们在resources目录下新建目录config,然后新增配置文件user-test.properties,结构如图:
这里写图片描述
user-test.properties:

request.url=http://www.baidu.com

控制器代码如下:

@RestController
@PropertySource(value = "classpath:config/user-test.properties")
@ConfigurationProperties(prefix = "request")
@Data
public class ConfigController {

    private String url;

    @GetMapping("req_url")
    public Object getUrl(){
        return url;
    }
   }

浏览器访问:http://localhost:9090/req_url
这里写图片描述

3.5、利用application.properties多环境自由切换

我们知道,springBoot默认加载配置文件,application.properties,我们可以根据我们的环境构建其他配置文件applilcation.-.properties如:application-dev.proerties等.
application-dev.properties我们把端口修改一下:

server.port=9091

spring boot设置多配置文件很简单,可以在启动的时候加上:

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
            SpringApplication app = new SpringApplication(Application.class);
           app.setAdditionalProfiles("dev");
        app.run(args);
    }
}

浏览器访问:http://localhost:9091/
这里写图片描述
可见,环境切换成功,
你也可以在通过命令行执行的时候指定配置文件,其他不变,命令如下:

java -jar XXX.jar --spring.profiles.active=dev

3.6、说明:关闭特定的自动配置使用注解@SpringBootApplication的exclude参数

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})

3.7、常见的starter pom

名称描述
spring-boot-starterSpringBoot核心starter,包含自动配置、日志、yaml配置文件支持
spring-boot-starter-actuator准生产特性,用来监控和管理应用
spring-boot-starter-remote-shell提供基于ssh协议的监控和管理
spring-boot-starter-amqp使用spring-rabbit来支持AMQP
spring-boot-starter-aop使用spring-aop和AspectJ支持切面编程
spring-boot-starter-batch对Spring Batch支持
spring-boot-starter-cache对Spring Cache支持
spring-boot-starter-cloud-connectors对云平台支持
spring-boot-starter-data-elasticsearch对ElasticSearch支持
spring-boot-starter-data-gemfire对分布式存储GemFire支持
spring-boot-starter-data-jpa对JPA,包括了spring-data-jpa、spring-orm和Hibernate支持
spring-boot-starter-data-mongodb对MongonDB支持
spring-boot-starter-data-rest将Spring Data repository暴露为REST形式服务
spring-boot-starter-data-solr对Apache Solr数据检索平台支持
spring-boot-starter-freemarker对FreeMarker模版引擎支持
spring-boot-starter-groovy-templates对Groovy模版引擎支持
spring-boot-starter-jdbc对JDBC数据库支持
spring-boot-starter-eamil对Javax.mail支持
spring-boot-starter-security对spring-security支持
spring-boot-starter-redis对Redis支持
spring-boot-starter-web对WEB项目开发支持,包括Tomcat、spring-webmvc
spring-boot-starter-Jetty使用Jetty容器替换Tomcat

。。。。。。

3.8、使用xml配置

Spring Boot提倡零配置,即无xml配置,但是在实际开发中,可能要求xml配置。我们可以通过Spring提供的@ImportResource来加载xml配置。

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

上一篇:Spring Boot(二) 项目构建
下一篇:Spring Boot(四) 自定义Banner和日志

  • 7
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿呆编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值