Spring Boot 入门 (二)属性配置

Spring Boot 入门 (二)属性配置




SpringBoot的配置都写在application.properties配置文件中

application.properties:添加端口配置

#server port
server.port=8081
#add project context path
server.context-path=/demo

再次启动


 启动完成,可以看见端口已经切换为8081了

 访问:





注意:
1.由于配置了server.context-path属性,所以访问的时候前面加上
2.端口必须修改为8081才能正常访问

切换属性文件为yml文件

  1.备份属性文件:application.properties.bk

  2.创建application.yml配置文件

server:
  port: 8082
  context-path: /demo

注意:冒号后面必须有个空格,这是语法


 重启应用:




注意:

重启后端口已经按照yml格式配置文件修改为8082端口

访问:




访问成功

推荐:推荐大家使用yml格式配置文件

 

获取配置文件中的参数值

1.配置文件添加

server:
  port: 8080

cupSize: B
age: 18
content: "cpuSize:${cupSize},age:${age}"

2.java代码

package com.example;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
 * create note234.com
 */
@RestController
public class HelloController {

	@Value("${cupSize}")//注意写法,获取配置文件中的cupSize
	private String cpuSize;
	
	@Value("${age}")
	private int age;
	
	@Value("${content}")
	private String content;
	
	@RequestMapping(value="/hello",method=RequestMethod.GET)//写法与springMVC有点相似
	public String say(){
		return "Hello Spring Boot!"+cpuSize+" "+age+" "+content;
	}
}

3.访问结果


属性对象方式取值
1.属性文件
 

server:
  port: 8080

cupSize: B
age: 18
content: "cpuSize:${cupSize},age:${age}"

persion: 
    name: xqlee
    age: 18

2.属性文件里面对象对应的bean

package com.example;

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

@Component
@ConfigurationProperties(prefix = "persion") // 这里的person与配置文件一致
public class PersionProperties {

	private String name;
	private int age;

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the age
	 */
	public int getAge() {
		return age;
	}

	/**
	 * @param age
	 *            the age to set
	 */
	public void setAge(int age) {
		this.age = age;
	}

}

3.controller

package com.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
 * create note234.com
 */
@RestController
public class HelloController {

	@Value("${cupSize}")//注意写法,获取配置文件中的cupSize
	private String cpuSize;
	
	@Value("${age}")
	private int age;
	
	@Value("${content}")
	private String content;
	
	@Autowired
	PersionProperties persionProperties;
	
	@RequestMapping(value="/hello",method=RequestMethod.GET)//写法与springMVC有点相似
	public String say(){
		System.out.println("name:"+persionProperties.getName()+"  age:"+persionProperties.getAge());
		return "Hello Spring Boot!"+cpuSize+" "+age+" "+content;
	}
}

4.执行结果

 多配置文件方式
1.复制两份application.yml,分别命名为application-dev.yml application-prod.yml
2.修改prod配置内容
 

server:
  port: 8081

cupSize: B
age: 18
content: "cpuSize:${cupSize},age:${age}"

persion: 
    name: xqlee2
    age: 182

3配置原本的application.yml
 

spring:
  profiles:
    active: prod

启动:


由此得出:
已经启用配置文件application-prod.xml
同理,修改application.yml中的active: dev则启动application-dev.yml

spring boot 入门(一)hello word
spring boot入门(二)属性配置
spring boot入门(三)controller的使用
spring boot入门(四)数据库操作

Spring Boot 入门 (二)属性配置

相关文章:

你感兴趣的文章:

标签云: