springboot的默认配置文件是application.properties或者application.yml。读取配置文件有以下两种方式
1、注入Environment对象,用Environment对象获取
2、使用Value注解
获取的值默认都是字符串类型,使用Value注解时,自动转化类型,使用Environment对象时,指定类,如下:
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.edu.spring</groupId> <artifactId>springboot</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>springboot</name> <!-- FIXME change it to the project‘s website --> <url>http://www.example.com</url> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.1.6.RELEASE</version> <scope>import</scope> <type>pom</type> </dependency> </dependencies> </dependencyManagement> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies> </project>
UserConfig.java
package com.edu.spring.springboot; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; @Component public class UserConfig { @Autowired private Environment environment; @Value("${local.port}") private String port; @Value("${local.port}") private Integer port2; public void show(){ System.out.println("local.ip= "+environment.getProperty("local.ip")); System.out.println("local.port= "+environment.getProperty("local.port",Integer.class)); System.out.println("local.port= "+port); System.out.println("local.port= "+port2); } }
App.java
package com.edu.spring.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication public class App { public static void main( String[] args ) { ConfigurableApplicationContext context=SpringApplication.run(App.class, args); System.out.println(context.getEnvironment().getProperty("local.ip")); context.getBean(UserConfig.class).show(); context.close(); } }
运行结果如下:
假如在配置文件中没对应的属性,取值的时候可以指定默认值,即配置文件中存在属性,取配置文件中的,否则取指定的。如下:
UserConfig.java
package com.edu.spring.springboot; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; @Component public class UserConfig { @Autowired private Environment environment; @Value("${name:你好}") private String name; public void show(){ System.out.println("name= "+environment.getProperty("name","你好")); System.out.println("name= "+name); } }
App.java
package com.edu.spring.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication public class App { public static void main( String[] args ) { ConfigurableApplicationContext context=SpringApplication.run(App.class, args); context.getBean(UserConfig.class).show(); context.close(); } }
运行结果如下:
1
原文地址:https://www.cnblogs.com/javasl/p/11827832.html
时间: 2024-10-07 11:47:33