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-starter | SpringBoot核心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"})