SpringBoot配置项目属性
[size=medium][b]1. 项目内置属性[/b][/size] [code="说明"] 配置文件application.properties可以自定义 之前的版本使用server.context-path 后面的版本使用server.servlet.context-path [/code] [code="java"] src/main/resource/com/andrew/application.properties server.port=8000 server.servlet.context-path=/HelloWorld helloWorld=spring boot \u4F60\u975E\u5E38\u597D\uFF01 src/main/java/com/andrew/controller/HelloWorldController.java package com.andrew.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloWorldController { @Value("${helloWorld}") private String helloWorld; @RequestMapping("/hello") public String say(){ return helloWorld; } } HelloWorldApplication.java右键Run As选择Spring Boot App http://localhost:8000/HelloWorld/hello 运行结果: spring boot 你非常好! [/code] [size=medium][b]2. 自定义属性[/b][/size] [code="java"] src/main/resource/com/andrew/application.properties server.port=8000 server.servlet.context-path=/HelloWorld helloWorld=spring boot \u4F60\u975E\u5E38\u597D\uFF01 mysql.jdbcName=com.mysql.jdbc.Driver mysql.dbUrl=jdbc:mysql://localhost:3306/springboot mysql.userName=root mysql.password=root src/main/java/com/andrew/controller/HelloWorldController.java package com.andrew.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloWorldController { @Value("${helloWorld}") private String helloWorld; @Value("${mysql.jdbcName}") private String jdbcName; @Value("${mysql.dbUrl}") private String dbUrl; @Value("${mysql.userName}") private String userName; @Value("${mysql.password}") private String password; @RequestMapping("/hello") public String say() { return helloWorld; } @RequestMapping("/showJdbc") public String showJdbc() { return "mysql.jdbcName:" + jdbcName + "
" + "mysql.dbUrl:" + dbUrl + "
" + "mysql.userName:" + userName + "
" + "mysql.password:" + password; } } HelloWorldApplication.java右键Run As选择Spring Boot App http://localhost:8000/HelloWorld/showJdbc 运行结果: mysql.jdbcName:com.mysql.jdbc.Driver mysql.dbUrl:jdbc:mysql://localhost:3306/springboot mysql.userName:root mysql.password:root [/code] [size=medium][b]3. Configuration Properties配置[/b][/size] [code="java"] src/main/resource/com/andrew/application.properties server.port=8000 server.servlet.context-path=/HelloWorld helloWorld=spring boot \u4F60\u975E\u5E38\u597D\uFF01 mysql.jdbcName=com.mysql.jdbc.Driver mysql.dbUrl=jdbc:mysql://localhost:3306/springboot mysql.userName=root mysql.password=root src/main/java/com/andrew/properties/MysqlProperties.java package com.andrew.properties; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix="mysql") public class MysqlProperties { private String jdbcName; private String dbUrl; private String userName; private String password; // getter and setter } 根据@ConfigurationProperties(prefix="mysql")的提示 在pom.xml中Add spring-boot-configuration-processor to pom.xml 也可以自己查询手动添加 pom.xml org.springframework.boot spring-boot-configuration-processor true src/main/java/com/andrew/controller/MysqlController.java package com.andrew.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.andrew.properties.MysqlProperties; @RestController public class MysqlController { @Autowired private MysqlProperties mysqlProperties; @RequestMapping("/showMysqlJdbc") public String showMysqlJdbc() { return "mysql.jdbcName:" + mysqlProperties.getJdbcName() + "
" + "mysql.dbUrl:" + mysqlProperties.getDbUrl() + "
" + "mysql.userName:" + mysqlProperties.getUserName() + "
" + "mysql.password:" + mysqlProperties.getPassword(); } } http://localhost:8000/HelloWorld/showMysqlJdbc 运行结果: mysql.jdbcName:com.mysql.jdbc.Driver mysql.dbUrl:jdbc:mysql://localhost:3306/springboot mysql.userName:root mysql.password:root [/code]