【问题标题】:Accessing multiple property files with @PropertyResource in Spring在 Spring 中使用 @PropertyResource 访问多个属性文件
【发布时间】:2013-01-24 15:37:49
【问题描述】:

使用 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

【问题讨论】:

  • 在制作控制器 @Configuration 类之前我会三思而后行 - @Configuration 类的处理方式与普通 spring bean 不同,很难预测它如何影响 bean 生命周期和行为。

标签: spring spring-mvc


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    多个Properties可以在Spring中被任意一个访问,

    • @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

    【讨论】:

    • @PropertySource( {"name1", "name2"} ) - 为我工作。我正在加载两个配置属性,一个在类路径中,另一个在外部位置。我给了这样的 - @PropertySource({"classpath:/app.properties", "file:/C:/my_dir/config.properties"})
    【解决方案3】:

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

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

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

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

    @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"/>
    

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

    【讨论】:

    • 第一种方法如何解决这个问题?添加通配符并不是真正指向多个特定的属性文件。您能否解释一下如何指定多个位置?
    • *.properties,它将扫描类路径中的文件夹以匹配您的所有属性。那是春天 3 岁,我相信现在有更好的方法。但无论如何,这应该仍然有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-31
    • 2011-08-02
    • 2021-11-27
    • 2021-03-15
    • 2011-12-24
    • 2010-12-18
    • 1970-01-01
    相关资源
    最近更新 更多