springboot2

@ConfigurationProperties(prefix = "config2",locations="classpath:test.properties")

1. Configuration file parsing

 

Spring Boot uses the concept of "habit over configuration" (there are a lot of configurations in the project, and there is also a built-in habit configuration so that you don't need to configure it manually) to get your project up and running quickly. Therefore, if we want to play Spring Boot, we must know how to enable the default configuration of each functional module, which requires understanding of Spring Boot's configuration file application.properties.

 

Spring Boot uses a global configuration file application.properties, which is placed in the src/main/resources directory or /config in the classpath. The role of the global configuration file of Spring Boot is to modify the configuration values ​​of some default configurations.



 

 There is no creating one, application.properties provides support for custom properties, so we can configure some constants here:

 

test.baidu.url = http: //www.baidu.com
test.baidu.title=China is rich and powerful
test.baidu.s8how=nihao
test.url = $ {test.baidu.show}, $ {test.baidu.title}, Welcome!

#template encoding
spring.thymeleaf.encoding=UTF-8

 Then you can directly bind to the property you want by annotating @Value(value=”${config.name}”) where you want to use it

 

 

    @Value(value = "${test.url}")
    private String gourl;

    @Value(value = "${test.baidu.title}")
    private String gotitle;

 

 

Sometimes there are too many properties, and it is too tiring to bind one by one to the property fields. Officially, it is recommended to bind a bean of an object. Here we build a Config.java class, and the top needs to use the annotation @ConfigurationProperties(prefix = “test.baidu” ) to indicate which to use

 

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

/**
 * @author huxy
 * @Note configuration file mapping class
 * @version V1.0
 * @node
 */
@ConfigurationProperties(prefix="test.baidu")
public class Config {

    private String url;

    private String title;

    private String show;


    public String getUrl(){
        return url;

    }

    public void setUrl(String url){
        this.url=url;
    }


    public String getTitle(){
        return title;

    }

    public void setTitle(String title){
        this.title=title;
    }

    public String getShow(){
        return show;

    }

    public void setShow(String show){
        this.show=show;
    }
}

 After configuring here, you need to add @EnableConfigurationProperties to the spring Boot entry class and specify which bean to load. If you don't write Config.class, add it to the bean class

 

@SpringBootApplication
@EnableConfigurationProperties(Config.class)
public class DemoApplication {

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

 Finally, you can use Config in the Controller, as follows:

 

 

  @Autowired
    private Config config;

 Use: config.getShow()

 

It can also be used by direct reference between the various parameters in application.properties:

 

test.baidu.url = http: //www.baidu.com
test.baidu.title=China is rich and powerful
test.baidu.s8how=nihao
test.url = $ {test.baidu.show}, $ {test.baidu.title}, Welcome!

 Use a custom configuration file

 

Sometimes we don't want to put all the configuration in application.properties. At this time, we can define another one. Here I named it test.properties, and the path is also placed under src/main/resources.

 

@Configuration
@ConfigurationProperties(prefix = "com.md")
@PropertySource("classpath:test.properties")
public class ConfigTestBean {
    private String name;
    private String want;
    // omit getters and setters
}

 如果你使用的是1.5以前的版本,那么可以通过locations指定properties文件的位置,这样:

@ConfigurationProperties(prefix = "config2",locations="classpath:test.properties")

 但是1.5版本后就没有这个属性了,找了半天发现添加@Configuration和@PropertySource(“classpath:test.properties”)后才可以读取。

 

随机值配置

配置文件中${random} 可以用来生成各种不同类型的随机值,从而简化了代码生成的麻烦,例如 生成 int 值、long 值或者 string 字符串。

dudu.secret=${random.value}
dudu.number=${random.int}
dudu.bignumber=${random.long}
dudu.uuid=${random.uuid}
dudu.number.less.than.ten=${random.int(10)}
dudu.number.in.range=${random.int[1024,65536]}

 外部配置-命令行参数配置

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

 

java -jar xx.jar

可以以下命令修改tomcat端口号:

 

java -jar xx.jar --server.port=9090

可以看出,命令行中连续的两个减号--就是对application.properties中的属性值进行赋值的标识。

所以java -jar xx.jar --server.port=9090等价于在application.properties中添加属性server.port=9090。

如果你怕命令行有风险,可以使用SpringApplication.setAddCommandLineProperties(false)禁用它。

 

实际上,Spring Boot应用程序有多种设置途径,Spring Boot能从多重属性源获得属性,包括如下几种:

 

根目录下的开发工具全局设置属性(当开发工具激活时为~/.spring-boot-devtools.properties)。

测试中的@TestPropertySource注解。

测试中的@SpringBootTest#properties注解特性。

命令行参数

SPRING_APPLICATION_JSON中的属性(环境变量或系统属性中的内联JSON嵌入)。

ServletConfig初始化参数。

ServletContext初始化参数。

java:comp/env里的JNDI属性

JVM系统属性

操作系统环境变量

随机生成的带random.* 前缀的属性(在设置其他属性时,可以应用他们,比如${random.long})

应用程序以外的application.properties或者appliaction.yml文件

打包在应用程序内的application.properties或者appliaction.yml文件

通过@PropertySource标注的属性源

默认属性(通过SpringApplication.setDefaultProperties指定).

 

这里列表按组优先级排序,也就是说,任何在高优先级属性源里设置的属性都会覆盖低优先级的相同属性,列如我们上面提到的命令行属性就覆盖了application.properties的属性。

 

配置文件的优先级

application.properties和application.yml文件可以放在以下四个位置:

 

外置,在相对于应用程序运行目录的/congfig子目录里。

外置,在应用程序运行的目录里

内置,在config包内

内置,在Classpath根目录

同样,这个列表按照优先级排序,也就是说,src/main/resources/config下application.properties覆盖src/main/resources下application.properties中相同的属性

 

 

此外,如果你在相同优先级位置同时有application.properties和application.yml,那么application.properties里的属性里面的属性就会覆盖application.yml。

 

2.session 配置

 springboot session默认时间是 1800秒。

配置文件添加:

 

server.session.timeout = 3000

 工作原理:

http://blog.csdn.net/gaodebao1/article/details/51789188?readlog

http://blog.csdn.net/kite30/article/details/50681614

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326647526&siteId=291194637