SpringBoot读取配置文件
方式一:通过环境变量类Environment
或者通过@Autowired装载
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication public class SpringbootPropertiesApplication { public static void main(String[] args) { // 获取 Spring Boot 上下文 ConfigurableApplicationContext ctx = SpringApplication.run(SpringbootPropertiesApplication.class, args); // ctx.getEnvironment(); // 获取 边境变量 System.out.println("==========================================="); //获取字符串 System.out.println("String: " + (ctx.getEnvironment().getProperty("string.port") + 1111) ); //获取整数 System.out.println("Interger: " + (ctx.getEnvironment().getProperty("integer.port",Integer.class) + 1111 )); System.out.println(ctx.getEnvironment().getProperty("db.link.url")); System.out.println(ctx.getEnvironment().getProperty("db.link.driver")); System.out.println(ctx.getEnvironment().getProperty("db.link.username")); System.out.println(ctx.getEnvironment().getProperty("db.link.password")); System.out.println("==========================================="); } }
方式二:通过@Component与@Value获取
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class MyConf { @Value("${string.port}") private int intPort; @Value("${string.port}") private String stringPort; @Value("${db.link.url}") private String dbUrl; @Value("${db.link.driver}") private String dbDriver; @Value("${db.link.username}")private String dbUsername; @Value("${db.link.password}")private String dbPassword; public void show(){ System.out.println("==========================================="); System.out.println("intPort : " + (intPort + 1111)); System.out.println("stringPort : " + (stringPort + 1111)); System.out.println("string : " + dbUrl); System.out.println("string : " + dbDriver); System.out.println("string : " + dbUsername); System.out.println("string : " + dbPassword); System.out.println("==========================================="); } }
- 可以增加注解,指定属性文件的名称与位置:@PropertySources(@PropertySource(“xxx”),@PropertySource(“xxx”)),也可以增加前缀注解 。
方式三:通过@Configuration和@Value获取
@Configuration @PropertySource("classpath:jdbc.properties") public class JdbcConfig { @Value("${jdbc.url}") String url; @Value("${jdbc.driverClassName}") String driverClassName; @Value("${jdbc.username}") String username; @Value("${jdbc.password}") String password; @Bean public DataSource dataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(url); dataSource.setDriverClassName(driverClassName); dataSource.setUsername(username); dataSource.setPassword(password); return dataSource; } }
-
-
@PropertySource
:指定属性文件的路径是:classpath:jdbc.properties
-
通过
@Value
为属性注入值 -
通过@Bean将
dataSource()
方式四:通过属性类获取
1、新建属性类
@ConfigurationProperties(prefix = "jdbc") public class JdbcProperties { private String url; private String driverClassName; private String username; private String password; // ... 略 // getters 和 setters }
2、配置类
@Configuration @EnableConfigurationProperties(JdbcProperties.class) //可以配置多个,以逗号分隔 public class JdbcConfig { @Bean public DataSource dataSource(JdbcProperties jdbc) { DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(jdbc.getUrl()); dataSource.setDriverClassName(jdbc.getDriverClassName()); dataSource.setUsername(jdbc.getUsername()); dataSource.setPassword(jdbc.getPassword()); return dataSource; } }
3、更优雅的注入
@Configuration public class JdbcConfig { @Bean // 声明要注入的属性前缀,SpringBoot会自动把相关属性通过set方法注入到DataSource中 @ConfigurationProperties(prefix = "jdbc") public DataSource dataSource() { DruidDataSource dataSource = new DruidDataSource(); return dataSource; } }
// 直接注入
@EnableConfigurationProperties(SmsProperties.class)
public class SmsUtils {
@Autowired
private SmsProperties prop;
}
其它:配置文件可以使用集合。