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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14505078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:39:38  来源:igfitidea点击:

Accessing multiple property files with @PropertyResource in Spring

springspring-mvc

提问by E Paiz

Using the new @PropertySourceannotation in Spring 3.1, how can you access multiple property files with Environment?

使用@PropertySourceSpring 3.1 中的新注解,如何使用 Environment 访问多个属性文件?

Currently I have:

目前我有:

@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";
}


The result is the correct file name from File1.properties, but file2.namenot found. How can access File2.properties?


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

采纳答案by Rigg802

there are two different approaches: the first one is to use the PropertyPlaceHolder in your applicationContext.xml: beans-factory-placeholderconfigurer

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

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

the namespace to add is xmlns:context="http://www.springframework.org/schema/context"

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

If you want a direct access of a key to a String variable in your controller, use:

如果您想直接访问控制器中字符串变量的键,请使用:

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

The second approach is to use the util:propertiesin you applicationContext.xml:

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

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

using the 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

使用命名空间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

Then in your Controller:

然后在您的控制器中:

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

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

If you want a value from the files, just use the method getProperty(String key)

如果您想从文件中获取值,只需使用该方法 getProperty(String key)

回答by user2731143

If you can migrate to Spring 4.xthe problem has been solved with the new @PropertySourcesannotation:

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

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

回答by Arif Rahman

Multiple Propertiescan be accessed in Spring by either,

Properties可以通过以下任一方式在 Spring 中访问多个,

  • @PropertySource( {"name1", "name2"} )
  • @PropertySorces( { @PropertySource("name1"), @PropertySource("name2") } )
  • @PropertySource( {"name1", "name2"} )
  • @PropertySorces( { @PropertySource("name1"), @PropertySource("name2") })

Example of @PropertySource,

@PropertySource 示例,

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

Example of @PropertySources,

@PropertySources 示例,

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

After you specify propertiespath you can access them through Environmentinstance as you usually did

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

NOTE: Only these simply did not work for me though

注意:虽然只有这些对我不起作用

I was getting compilation erroras I was using property values to configure my app context. I tried all that I found through web but they did not work for me !

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

Until I configured Spring context as below,

直到我如下配置 Spring 上下文,

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

Example

例子

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
    }
}

For your information, I am using Spring 4.3

供您参考,我使用的是Spring 4.3