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”)),也可以增加前缀注解@ConfigurationProperties(prefix = "person")

方式三:通过@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;
    }
}
  • @Configuration:声明我们JdbcConfig是一个配置类

  • @PropertySource:指定属性文件的路径是:classpath:jdbc.properties

  • 通过@Value为属性注入值

  • 通过@Bean将 dataSource()方法声明为一个注册Bean的方法,Spring会自动调用该方法,将方法的返回值加入Spring容器中。

方式四:通过属性类获取

  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;

}

 其它:配置文件可以使用集合。

posted @ 2020-05-03 18:22  Water540  阅读(228)  评论(0编辑  收藏  举报