spring4.0 @PropertySource读取配置文件

@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();
    }
	
	@Bean
	public 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方式
	@Autowired
	private Environment env;
	@Bean
	public 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=yes
borderColor=105,179,90
fontColor=blue
imgWidth=125
imgHeight=45
fontSize=45
sessionKey=code
codeLength=4
fontName=\u5B8B\u4F53,\u6977\u4F53,\u5FAE\u8F6F\u96C5\u9ED1



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值