springBoot 获取配置的方式(5种)

一、利用@PropertySource获取resource下面的资源,Environment获取属性

 

@Configuration
@EnableTransactionManagement
@PropertySource(value = {"classpath:DBsource.properties"})
public class DBBeanConfig {
    @Autowired
    private Environment env;

    @Bean(destroyMethod = "close")
    public DataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(env.getProperty("source.driverClassName").trim());
        dataSource.setUrl(env.getProperty("source.url").trim());
        dataSource.setUsername(env.getProperty("source.username").trim());
        dataSource.setPassword(env.getProperty("source.password").trim());
        return dataSource;
    }
}

二、@PropertySource获取resource下面的资源,@ConfigurationProperties找到该资源的头部@Value注入属性.

 

 
 
@PropertySource({"classpath:redis-config.properties"})
@ConfigurationProperties(prefix="spring.redis")
public class RedisConfig {

    @Value("${host}")
    private  String  host;
    @Value("${port}")
    private  int  port;
}
 

三、@ConfigurationProperties找到该资源的头部,通过getter、setter方法注入及获取配置

 

@PropertySource({"classpath:redis-config.properties"})
@ConfigurationProperties(prefix="spring.redis")
public class RedisConfig2 {

    private  String  host;
    private  int  port;

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }
}

 

四、利用PropertiesLoaderUtil加载配置文件

 

public class RedisConfig3 {

    public static String  host;
    public static int  port;
    private static String property="redis-config.properties";

    private static RedisConfig3 mConfig;
    static {
        mConfig=loadConfig();
    }
    public  static  RedisConfig3 loadConfig(){
        if (mConfig==null){
            mConfig=new RedisConfig3();
            Properties properties = null;
            try {
                properties = PropertiesLoaderUtils.loadAllProperties(property);

                host=properties.getProperty("spring.redis.host");
                port=Integer.valueOf(properties.getProperty("spring.redis.port"));
                System.out.println(host+":"+port);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return mConfig;
    }

    public RedisConfig3 getInstance(){
        return mConfig;
    }
}
五、输入流读取文件中的数据,Properties 装载
/*
 * 读取属性配置文件返回的是Properties对象
 * 
 * @param propertiesFileName
 * @return
 */
public static Properties readPropertiesFile(String proFileName)
{
   Properties properties = new Properties();
   InputStream inCfg = null;

   try
   {
      inCfg = new BufferedInputStream(new FileInputStream(getConfigPath(proFileName)));
      properties.load(inCfg);
   }
   catch (Exception e)
   {
      LocalFilewrite.localFileWirte("读取属性配置文件失败!位置PropertiesManager类readPropertiesFile(String proFileName)方法."
            + e.toString());
   }

   return properties;

}
private static String getConfigPath(String fileName)
{
   String jarPath = PropertiesManager.class.getProtectionDomain().getCodeSource().getLocation().getFile();
   try
   {
      jarPath = java.net.URLDecoder.decode(jarPath, "UTF-8");
   }
   catch (UnsupportedEncodingException e)
   {
      LocalFilewrite.localFileWirte(e.toString());
   }
   String filePath = (new File(jarPath)).getParentFile().getParentFile().getAbsolutePath() + "/" + "config" + "/" + fileName;

   return filePath;
}

 

 

 

 

 

 

你可能感兴趣的:(springboot,配置)