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

E P*_*aiz 15 spring spring-mvc

@PropertySource在Spring 3.1中使用新的注释,如何使用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";
}
Run Code Online (Sandbox Code Playgroud)


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

小智 50

如果你可以迁移到Spring 4.x,那么问题已经通过新的@PropertySources注释解决了:

@PropertySources({
        @PropertySource("/file1.properties"),
        @PropertySource("/file2.properties")
})
Run Code Online (Sandbox Code Playgroud)


Ari*_*man 7

Properties可以在Spring中访问多个,

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

@PropertySource的示例,

@PropertySource({
        "classpath:hibernateCustom.properties",
        "classpath:hikari.properties"
})
Run Code Online (Sandbox Code Playgroud)

@PropertySources的示例,

@PropertySources({
        @PropertySource("classpath:hibernateCustom.properties"),
        @PropertySource("classpath:hikari.properties")
})
Run Code Online (Sandbox Code Playgroud)

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

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

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

在按如下方式配置Spring上下文之前,

applicationContext.setServletContext(servletContext);
applicationContext.refresh();
Run Code Online (Sandbox Code Playgroud)

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
    }
}
Run Code Online (Sandbox Code Playgroud)

供您参考,我正在使用Spring 4.3

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

Rig*_*802 5

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

<context:property-placeholder location="classpath*:META-INF/spring/properties/*.properties"/>
Run Code Online (Sandbox Code Playgroud)

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

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

@Value("${some.key}")
private String valueOfThatKey;
Run Code Online (Sandbox Code Playgroud)

第二种方法是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"/>
Run Code Online (Sandbox Code Playgroud)

使用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;
Run Code Online (Sandbox Code Playgroud)

如果您想要文件中的值,只需使用该方法即可 getProperty(String key)