Spring boot reading configuration files (multiple yml s)

When learning spring boot (in this case, when integrating mybatis+druid), because there are too many configurations written in a single yml configuration file, it is obviously redundant, so it is configured separately.

However, when loading the druid database configuration information, it is found that the relevant parameters cannot be read (it is confirmed here that they can be read before separation), and then the location may be the reading mechanism, the default is the application.yml file. In fact, this problem has been encountered before, but I didn't think of it for a while. Here are several ways to read multi file data.

1. Select the additional configuration file to read (not required for application.yml) @ PropertySource and @Value As follows

@Configuration
    @PropertySource("classpath:application-datasource.yml")
    class IDataSourceProperties {
	@Value("${url}")
        private String url;
        @Value("${username}")
        private String username;
		********The following ellipsis*********
	}

2. Obtained through Environment

Load all required configurations at startup

@SpringBootApplication
@PropertySource("classpath:application.yml")
@PropertySource("classpath:application-datasource.yml")
@PropertySource("classpath:application-druid.yml")
public class IndexApplication {
    private  static Logger logger = Logger.getLogger(IndexApplication.class);

    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }
    public static void main(String[] args) throws Exception{
        SpringApplication.run(IndexApplication.class, args);
        logger.info("======spring boot start success ===========");
    }

    @Bean
    public PropertySourcesPlaceholderConfigurer getSources() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    /*@Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource druidDataSource() {
        return new DruidDataSource();
    }*/

}

Configuration when using

@Configuration
@Slf4j
public class DruidConfig {
    @Autowired
    private Environment env;

    /**
     * Registering a StatViewServlet is equivalent to declaring a servlet in web.xml
     */
    @Bean
    public ServletRegistrationBean<StatViewServlet> druidServlet()
    {
        ServletRegistrationBean<StatViewServlet> reg = new ServletRegistrationBean<StatViewServlet>();
        reg.setServlet(new StatViewServlet());
        reg.addUrlMappings("/monitor/druid/*");
        /** White list */
        reg.addInitParameter("allow", env.getProperty("spring.druid.web-stat-filter.allow"));
        /** IP Blacklist (deny takes precedence over allow when co existing) */
        reg.addInitParameter("deny", env.getProperty("spring.druid.web-stat-filter.deny"));
        /** Can you reset the data to disable the "Reset All" function on the HTML page */
        reg.addInitParameter("resetEnable", env.getProperty("spring.druid.web-stat-filter.resetEnable"));
        reg.addInitParameter("loginUsername", env.getProperty("spring.druid.web-stat-filter.loginUsername"));
        reg.addInitParameter("loginPassword", env.getProperty("spring.druid.web-stat-filter.loginPassword"));
        return reg;
    }
	*****The following sections are omitted****

Keywords: Druid Spring Mybatis Database

Added by windjohn on Sun, 15 Dec 2019 23:10:18 +0200