在Spring中使用@PropertyResource访问多个属性文件

时间:2013-01-24 15:37:49

标签: spring spring-mvc

在Spring 3.1中使用新的@PropertySource注释,如何使用Environment访问多个属性文件?

目前我有:

@Controller
@Configuration 
@PropertySource(
    name = "props",
    value = { "classpath:File1.properties", "classpath:File2.properties" })
public class TestDetailsController {


@Autowired
private Environment env;
/**
 * Simply selects the home view to render by returning its name.
 */
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {

    String file1Name = env.getProperty("file1.name","file1.name not found");
            String file2Name = env.getProperty("file2.name","file2.name not found");

            System.out.println("file 1: " + file1Name);
            System.out.println("file 2: " + file2Name);

    return "home";
}


结果是来自 File1.properties 的正确文件名,但未找到 file2.name 。如何访问 File2.properties

3 个答案:

答案 0 :(得分:46)

如果您可以迁移到 Spring 4.x ,则问题已通过新的@PropertySources注释解决:

@PropertySources({
        @PropertySource("/file1.properties"),
        @PropertySource("/file2.properties")
})

答案 1 :(得分:5)

有两种不同的方法: 第一个是在applicationContext.xml中使用PropertyPlaceHolder: beans-factory-placeholderconfigurer

<context:property-placeholder location="classpath*:META-INF/spring/properties/*.properties"/>

要添加的名称空间为xmlns:context="http://www.springframework.org/schema/context"

如果要直接访问控制器中String变量的键,请使用:

@Value("${some.key}")
private String valueOfThatKey;

第二种方法是在applicationContext.xml中使用util:properties

<util:properties id="fileA" location="classpath:META-INF/properties/a.properties"/>
<util:properties id="fileB" location="classpath:META-INF/properties/b.properties"/>

使用namesapce xmlns:util="http://www.springframework.org/schema/util" schemaLocations:http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd

然后在你的控制器中:

@Resource(name="fileA")
private Properties propertyA;

@Resource(name="fileB")
private Properties propertyB;

如果您想要文件中的值,请使用方法getProperty(String key)

答案 2 :(得分:4)

可以在Spring中访问多个Properties

  • @PropertySource({“name1”,“name2”})
  • @PropertySorces({@PropertySource(“name1”),@ PropertySource(“name2”)})

@PropertySource的示例,

@PropertySource({
        "classpath:hibernateCustom.properties",
        "classpath:hikari.properties"
})

@PropertySources的例子,

@PropertySources({
        @PropertySource("classpath:hibernateCustom.properties"),
        @PropertySource("classpath:hikari.properties")
})

指定properties路径后,您可以像往常一样通过Environment实例访问它们

注意:虽然

只是这些对我不起作用

我正在编译error,因为我使用属性值来配置我的应用上下文。我尝试了通过网络找到的所有内容,但它们对我不起作用!

在我配置Spring上下文之前,

applicationContext.setServletContext(servletContext);
applicationContext.refresh();

实施例

public class SpringWebAppInitializer implements WebApplicationInitializer{

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        // Create the 'root' Spring application context
        AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
        // register config class i.e. where i used @PropertySource
        applicationContext.register(AppContextConfig.class);
        // below 2 are added to make @PropertySources/ multi properties file to work
        applicationContext.setServletContext(servletContext);
        applicationContext.refresh();

        // other config
    }
}

有关您的信息,我使用的是 Spring 4.3

相关问题