spring4.0 @PropertySource读取配置文件

来源:互联网 发布:网络大电影方案 编辑:程序博客网 时间:2024/05/18 05:38

@Configurable

@ComponentScan(basePackages = "com.9leg.java.spring")

@PropertySource(value = "classpath:spring/config.properties")

public class AppConfigTest {

    @Bean

    public PropertySourcesPlaceholderConfigurer propertyConfigInDev() {

        return new PropertySourcesPlaceholderConfigurer();

    }

    

}

通过@PropertySource注解将properties配置文件中的值存储到Spring的 Environment中,Environment接口提供方法去读取配置文件中的值,参数是properties文件中定义的key值。上面是读取一个配置文件,如果你想要读取多个配置文件,请看下面代码片段:


@PropertySource(value = {"classpath:spring/config.properties","classpath:spring/news.properties"})


在Spring 4版本中,Spring提供了一个新的注解——@PropertySources,从名字就可以猜测到它是为多配置文件而准备的。

@PropertySources({

@PropertySource("classpath:config.properties"),

@PropertySource("classpath:db.properties")

})

public class AppConfig {

    //something

}


        另外在Spring 4版本中,@PropertySource允许忽略不存在的配置文件。先看下面的代码片段:

@Configuration

@PropertySource("classpath:missing.properties")

public class AppConfig {

    //something

}


如果missing.properties不存在或找不到,系统则会抛出异常FileNotFoundException。

Caused by: java.io.FileNotFoundException: 

        class path resource [missiong.properties] cannot be opened because it does not exist


幸好Spring 4为我们提供了ignoreResourceNotFound属性来忽略找不到的文件

@Configuration

    @PropertySource(value="classpath:missing.properties", ignoreResourceNotFound=true)

    public class AppConfig {

    }

  @PropertySources({

        @PropertySource(value = "classpath:missing.properties", ignoreResourceNotFound=true),

        @PropertySource("classpath:config.properties")

        })


以下是测试例子 读取kapacha 属性:

package com.trunko.oa.config;import java.util.Properties;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.PropertySource;import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;import org.springframework.core.env.Environment;import com.google.code.kaptcha.impl.DefaultKaptcha;import com.google.code.kaptcha.util.Config;@Configuration@PropertySource("classpath:kapatcha.properties")public class SpringCofig {/**方式一:@Value("${isBorder}")private String isBorder;@Value("${borderColor}")private String borderColor;@Value("${fontColor}")private String fontColor;@Value("${imgWidth}")private String imgWidth;@Value("${imgHeight}")private String imgHeight;@Value("${fontSize}")private String fontSize;@Value("${sessionKey}")private String sessionKey;@Value("${codeLength}")private String codeLength;@Value("${fontName}") private String fontName;   @Bean //要想使用@Value 用${}占位符注入属性,这个bean是必须的,这个就是占位bean,另一种方式是不用value直接用Envirment变量直接getProperty('key')    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {        return new PropertySourcesPlaceholderConfigurer();    }@Beanpublic DefaultKaptcha captchaProducer(){System.out.println(isBorder);Properties ps= new Properties();ps.setProperty("kaptcha.border", isBorder);ps.setProperty("kaptcha.border.color", borderColor);ps.setProperty("kaptcha.textproducer.font.color", fontColor);ps.setProperty("kaptcha.image.width", imgWidth);ps.setProperty("kaptcha.image.height", imgHeight);ps.setProperty("kaptcha.textproducer.font.size", fontSize);ps.setProperty("kaptcha.session.key", sessionKey);ps.setProperty("kaptcha.textproducer.char.length", codeLength);ps.setProperty("kaptcha.textproducer.font.names", fontName);Config config = new Config(ps);DefaultKaptcha df= new DefaultKaptcha();df.setConfig(config);return df;}方式一结束**///方式二:用Envirment方式@Autowiredprivate Environment env;@Beanpublic DefaultKaptcha captchaProducer(){Properties ps= new Properties();ps.setProperty("kaptcha.border", env.getProperty("isBorder"));ps.setProperty("kaptcha.border.color",env.getProperty("borderColor"));ps.setProperty("kaptcha.textproducer.font.color", env.getProperty("fontColor"));ps.setProperty("kaptcha.image.width", env.getProperty("imgWidth"));ps.setProperty("kaptcha.image.height",env.getProperty("imgHeight"));ps.setProperty("kaptcha.textproducer.font.size", env.getProperty("fontSize"));ps.setProperty("kaptcha.session.key", env.getProperty("sessionKey"));ps.setProperty("kaptcha.textproducer.char.length", env.getProperty("codeLength"));ps.setProperty("kaptcha.textproducer.font.names",env.getProperty("fontName"));Config config = new Config(ps);DefaultKaptcha df= new DefaultKaptcha();df.setConfig(config);return df;}//@Bean//public DefaultKaptcha captchaProducer(){//Properties ps= new Properties();//ps.setProperty("kaptcha.border", "yes");//ps.setProperty("kaptcha.border.color", "105,179,90");//ps.setProperty("kaptcha.textproducer.font.color", "blue");//ps.setProperty("kaptcha.image.width", "125");//ps.setProperty("kaptcha.image.height", "45");//ps.setProperty("kaptcha.textproducer.font.size", "45");//ps.setProperty("kaptcha.session.key", "code");//ps.setProperty("kaptcha.textproducer.char.length", "4");//ps.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");//Config config = new Config(ps);//DefaultKaptcha df= new DefaultKaptcha();//df.setConfig(config);//return df;////}}
配置文件:

isBorder=yesborderColor=105,179,90fontColor=blueimgWidth=125imgHeight=45fontSize=45sessionKey=codecodeLength=4fontName=\u5B8B\u4F53,\u6977\u4F53,\u5FAE\u8F6F\u96C5\u9ED1



0 0
原创粉丝点击