比如说,我们现在要加载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;
}