资源文件中的属性配置与映射到实体类
demo/pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
添加资源文件
src/main/resources/resource.properties
com.oracle.opensource.name=oracle
com.oracle.opensource.website=www.oracle.com
com.oracle.opensource.language=java
com.oracle.pojo/Resource.java
package com.oracle.pojo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@ConfigurationProperties(prefix="com.oracle.opensource")
@PropertySource(value="classpath:resource.properties")
public class Resource {
private String name;
private String website;
private String language;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
this.website = website;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
}
com.oracle.controller/HelloContoller.java
package com.oracle.controller;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.oracle.pojo.IMoocJSONResult;
import com.oracle.pojo.Resource;
@RestController
public class HelloContoller {
@Autowired
private Resource resource;
@RequestMapping("/getResource")
public IMoocJSONResult getResource() {
Resource bean = new Resource();
BeanUtils.copyProperties(resource, bean);
return IMoocJSONResult.ok(bean);
}
}
测试
一、 SpringBoot资源文件配置server以及tomcat
src/main/resources/application.properties.java
############################################################
#
# Server 服务端配置
#
############################################################
#配置端口号
#server.port=8080
#配置context-path,这个配置在正式发布时不需要配置。
#server.context-path=/IMooc
#错误页,指定发生错误时,跳转的URL
#server.error.path=/error
#session最大超时时间,默认为30分钟
server.session-timeout=30
#该服务绑定的IP地址
#server.address=192.168.1.2
############################################################
# Server - tomcat 相关配置
############################################################
#tomcat最大线程数,默认为200
#server.tomcat.max-threads=250
#tomcat的URI编码
server.tomcat.uri-encoding=UTF-8
#存放tomcat的日志,dump文件的临时文件夹,默认为系统的tmp文件夹。
#如: C:\Users\Shanhy\AppData\Local\Temp
#server.tomcat.basedir=H:/springboot-tomcat-tmp
#打开tomcat的Access日志,并可以设置日志格式的方法。
#server.tomcat.access-log-enabled=true
#server.tomcat.access-log-pattern=
# accesslog目录,默认在basedir/logs
#server.tomcat.accesslog.directory=
#日志文件目录
#logging.path=H:/springboot-tomcat-tmp
#日志文件名,默认为spring.log
#logging.file=myapp.log