spring-boot读取外部的配置文件(properties)

比如说,我们现在要加载jdbc.properties这个配置文件,以便拿到里面的一些配置信息,我们可以这样做。

我们可以自己定义一个配置类,用来加载这个配置文件。

package com.zsf.springboot;

import com.jolbox.bonecp.BoneCPDataSource;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.PropertySource;

@Configuration()//通过该注解来表明该类是一个Spring的配置,相当于一个xml文件

@ComponentScan(basePackages ="com.zsf.springboot")

@PropertySource(value = {"classpath:jdbc.properties" }, ignoreResourceNotFound =true)

public class SpringConfig {

@Bean

    // 通过该注解来表明是一个Bean对象,相当于xml中的

    public UserDAO getUserDAO() {

return new UserDAO();// 直接new对象做演示

    }

@Value("${jdbc.url}")

private StringjdbcUrl;

@Value("${jdbc.driverClassName}")

private StringjdbcDriverClassName;

@Value("${jdbc.username}")

private StringjdbcUsername;

@Value("${jdbc.password}")

private StringjdbcPassword;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。