04.14 三、Spring Boot基本配置解读

1.1、说明

Spring Boot为了更好的快速的启动和运行,遵行优先于配置的原则,其中Spring Boot默认加载src/main/Java/resources目录下application.properties或者application.yml,二者等同。

1.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

三、Spring Boot基本配置解读

1.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

三、Spring Boot基本配置解读

1.4、读取其他配置文件,如自定义等

我们在resources目录下新建目录config,然后新增配置文件user-test.properties,结构如图:

三、Spring Boot基本配置解读

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

三、Spring Boot基本配置解读

1.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/

三、Spring Boot基本配置解读

可见,环境切换成功,

你也可以在通过命令行执行的时候指定配置文件,其他不变,命令如下:

java -jar XXX.jar --spring.profiles.active=dev

1.6、说明:

关闭特定的自动配置使用注解@SpringBootApplication的exclude参数

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})

1.7、常见的starter pom

<table><thead>名称描述/<thead><tbody>spring-boot-starterSpringBoot核心starter,包含自动配置、日志、yaml配置文件支持spring-boot-starter-actuator准生产特性,用来监控和管理应用spring-boot-starter-remote-shell提供基于ssh协议的监控和管理
spring-boot-starter-amqp使用spring-rabbit来支持AMQPspring-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-webmvcspring-boot-starter-Jetty使用Jetty容器替换Tomcat/<tbody>/<table>

。。。。。。

1.8、使用xml配置

Spring Boot提倡零配置,即无xml配置,但是在实际开发中,可能要求xml配置。我们可以通过Spring提供的@ImportResource来加载xml配置。

@ImportResource({"classpath:some-context.xml","classpath:another-context.xml"})


分享到:


相關文章: