【SpringBoot】SpringBoot核心-外部配置

简介: 转载请注明出处:http://blog.csdn.net/qq_26525215本文源自【大学之旅_谙忆的博客】Spring Boot允许使用properties文件、yaml文件或者命令行参数作为外部配置。

转载请注明出处:http://blog.csdn.net/qq_26525215

本文源自大学之旅_谙忆的博客

Spring Boot允许使用properties文件、yaml文件或者命令行参数作为外部配置。

命令行参数配置

Spring Boot可以是基于jar包运行的,打成jar包的程序可以直接通过下面命令运行:

java -jar *.jar
AI 代码解读

如果你的Tomcat的端口和你其他的端口起冲突了,
还可以通过以下命令修改Tomcat端口号:

java -jar *.jar --server.port=10090
AI 代码解读

*为你的jar包名。

常规属性配置

在常规Spring环境下,注入properties文件里面的值的方式可以通过@PropertySource指明properties文件的位置,然后通过@Value注入值。
详情见此篇博客:

【Spring】Spring常用配置-Spring EL和资源调用

在Spring Boot中,我们只需要在application.properties定义属性,直接使用@Value注入即可。

实战

利用IDEA–>Spring-Initializr
创建好SpringBoot骨架!
步骤如下(以后的博客中可能就不再累赘写创建SpringBoot骨架啦):

创建好骨架后,进行如下的修改。

1、application.properties增加属性

springBoot2_2.author=chenhaoxiang
springBoot2_2.name=spring Boot

#修改Tomcat启动端口
server.port=10090
#修改访问路径-也就是把默认的"/"修改为了"/helloboot"
server.context-path=/helloboot
AI 代码解读

2、修改入口类

package cn.hncu;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class SpringBoot22Application {
    @Value("${springBoot2_2.author}")
    private String author;
    @Value("${springBoot2_2.name}")
    private String name;

    @RequestMapping("/")
    String index(){
        return "name:"+name+" and author:"+author;
    }

    public static void main(String[] args) {
        SpringApplication.run(SpringBoot22Application.class, args);
    }
}
AI 代码解读

3、运行程序,访问http://localhost:10090/helloboot/

效果如下:

类型安全的配置

上面我们使用@Value注入每个配置,但是在实际项目中会显得格外麻烦,因为我们的配置通常会是许多个,若使用上例的方式则要使用@Value注入很多次。

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

实战

我们在上面的例子上修改。

1、添加配置,即在application.properties上添加:

author.name=chx
author.age=20
AI 代码解读

当然,如果你不想在这个properties中配置属性,也可以自己新建一个properties文件,这就需要我们在@ConfigurationProperties的属性locations里指定properties的位置,且需要在入口类上配置。

2、类型安全的Bean

package cn.hncu.model;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 * User: 陈浩翔.
 * Date: 2017/2/18.
 * Time: 下午 4:52.
 * Explain:类型安全的Bean
 */
@Component
@ConfigurationProperties(prefix = "author")
//通过locations指定properties文件的位置,这里是在application.properties文件中,可以不指定
public class AuthorSettings {
    private String name;
    private Long age;

    public String getName() {
        return name;
    }

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

    public Long getAge() {
        return age;
    }

    public void setAge(Long age) {
        this.age = age;
    }
}
AI 代码解读

通过@ConfigurationProperties加载properties文件内的配置,通过prefix属性指定properties的配置的前缀,通过locations指定properties文件的位置。

如果不是在application.properties文件中,则需要配置locations。例如:

@ConfigurationProperties(prefix  = "author",locations = {"classpath:author.properties"})
AI 代码解读

本例不需要配置locations。

3、检验代码

package cn.hncu;

import cn.hncu.model.AuthorSettings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * User: 陈浩翔.
 * Date: 2017/2/18.
 * Time: 下午 4:59.
 * Explain:检验代码类 - 类型安全的配置
 */
@RestController
public class CheckoutAuthor {
    @Autowired //直接注入该配置
    private AuthorSettings authorSettings;

    @RequestMapping("/author")
    public String index2(){
        return "author name is "+ authorSettings.getName() +" and author age is "+authorSettings.getAge();
    }

}
AI 代码解读

运行。

访问:http://localhost:10090/helloboot/author

效果如下:

本篇博客涉及到的源码链接:

->点击访问源码-©CHX

本文章由[谙忆]编写, 所有权利保留。
欢迎转载,分享是进步的源泉。

转载请注明出处:http://blog.csdn.net/qq_26525215

本文源自大学之旅_谙忆的博客

目录
打赏
0
0
0
0
988
分享
相关文章
Java--Redis-08-redis在SpringBoot中配置
在Spring Boot中,我们可以通过配置来自定义一个属于我们自己的RedisTemplate。
141 0
Java--Redis-08-redis在SpringBoot中配置
《SpringBoot系列三》:自定义配置时@Value和@ConfigurationProperties孰优孰劣?
《SpringBoot系列三》:自定义配置时@Value和@ConfigurationProperties孰优孰劣?
761 1
《SpringBoot系列三》:自定义配置时@Value和@ConfigurationProperties孰优孰劣?
SpringBoot2.x基础篇:谈谈SpringBoot内提供的这几种配置绑定
`SpringBoot`在不断地版本迭代中陆续提供了不同的配置参数绑定的方式,我们可以单独获取`一个配置参数`也可以将`一系列的配置`映射绑定到`JavaBean`的属性字段,下面我们来看看这几种方式的配置绑定哪一种是你最常用到的。
SpringBoot2.x基础篇:谈谈SpringBoot内提供的这几种配置绑定
《SpringBoot系列十五》源码+案例分析条件装配时多个Condition执行的顺序是什么样的?可以配置优先级吗?
《SpringBoot系列十五》源码+案例分析条件装配时多个Condition执行的顺序是什么样的?可以配置优先级吗?
260 0
《SpringBoot系列十五》源码+案例分析条件装配时多个Condition执行的顺序是什么样的?可以配置优先级吗?
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等