java spring mvc 中的属性文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16890733/
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-11-01 00:17:37  来源:igfitidea点击:

Property file in spring mvc

javaspringspring-mvc

提问by Srikrishnan

I have a property file with key value pairs:

我有一个带有键值对的属性文件:

key1=value1
key2=value2

Now, in my controller, I want to directly print the value of a property file (of course after loading the property file using web.xml / app-servlet.xml), like:

现在,在我的控制器中,我想直接打印属性文件的值(当然在使用 web.xml / app-servlet.xml 加载属性文件之后),例如:

System.out.printl(${key1});

Is it possible to do that?

有可能这样做吗?

If not, I want to create an interface with all constant variable to read values from property file. How do I do it??

如果没有,我想创建一个包含所有常量变量的接口,以从属性文件中读取值。我该怎么做??

public interface MyConstants
{
      @Value("${key1}")
      public static final KEY_1="";
}

But as expected only empty string is assigned.

但正如预期的那样,只分配了空字符串。

How do I solve this issue? Or, what is the best way to using property files to retrieve values? Thanks in advance...

我该如何解决这个问题?或者,使用属性文件检索值的最佳方法是什么?提前致谢...

回答by Arun Manivannan

There are two reasons why having an interface for 'MyConstants' instead of a class is incorrect :

使用“MyConstants”而不是类的接口不正确的原因有两个:

1) Spring cannot inject values to an interface which has no implementation. Simply because you wont be able instantiate the interface. Remember, Spring is just a factory and it can play only with 'things' which can be instantiated.

1) Spring 不能向没有实现的接口注入值。仅仅是因为您将无法实例化接口。请记住,Spring 只是一个工厂,它只能处理可以实例化的“事物”。

2) Another reason is that having an interface for storing your constants is an anti-pattern in itself. That is not what interfaces are designed for. You might want to refer to the Constant interface anti-pattern.

2) 另一个原因是,拥有一个用于存储常量的接口本身就是一种反模式。这不是接口的设计目的。您可能需要参考 Constant 接口反模式。

http://en.wikipedia.org/wiki/Constant_interface

http://en.wikipedia.org/wiki/Constant_interface

回答by Tuan

It's possible! You need to use the utilnamespace in your app-servlet.xmlas below:

这是可能的!您需要使用如下util命名空间app-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

    <util:properties id="props" location="classpath:yourfile.properties" />

    <!-- other -->
</beans>

And your controlleris something like

controller是这样的

@org.springframework.beans.factory.annotation.Value("#{props.key1}")
public void setFoo(String foo) {
    System.out.println("props.key1: " + foo);
}

updatefor another way:

以另一种方式更新

You also can use namespace context

您也可以使用命名空间 context

<context:property-placeholder location="classpath:yourfile.properties" />

In controller, declare a property as below

在控制器中,声明一个属性如下

@Value("${pros.key1}")
private String foo;

回答by Akshay

Creating a ''Constants'' class / interface is a widely used approach, but I think its a flawed approach. It creates a weird coupling where classes from different layers in your system suddenly start depending on one Constants class. It also becomes difficult to understand by looking at the constants class, as to which constant is being used by who? Not to mention the fact that it completely mocks abstraction. You suddenly have a constants class which contains information about the error message to show on the jsp, username and password of a third party api, thread pool size etc.. all in one "I know everything" class

创建“常量”类/接口是一种广泛使用的方法,但我认为这是一种有缺陷的方法。它创建了一种奇怪的耦合,其中来自系统中不同层的类突然开始依赖于一个 Constants 类。通过查看常量类也变得难以理解,谁正在使用哪个常量?更不用说它完全嘲笑抽象的事实。您突然有了一个常量类,其中包含有关在 jsp、第三方 API 的用户名和密码、线程池大小等上显示的错误消息的信息。所有这些都在一个“我知道一切”类中

So avoid a constant class / interface as far as possible. Look at your controllers / services, if a particular service class needs a particular configuration value that you want exposed in a property file, inject it into the class and store it as an instance level constant. This design is much cleaner from an abstraction point of view, it also helps to unit test this class easily.

所以尽量避免使用常量类/接口。查看您的控制器/服务,如果特定服务类需要您希望在属性文件中公开的特定配置值,请将其注入该类并将其存储为实例级常量。从抽象的角度来看,这种设计更加清晰,它还有助于轻松地对此类进行单元测试。

In Spring, you can create a handle to a property file as follows:

在 Spring 中,您可以创建属性文件的句柄,如下所示:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations" value="classpath:my-application.properties" />       
</bean>

As the code suggests, you can mention multiple property files here. After you do this, you can reference a key from the mentioned property file, elsewhere in the context like so:

如代码所示,您可以在此处提及多个属性文件。执行此操作后,您可以从提到的属性文件中引用一个键,在上下文中的其他位置,如下所示:

<bean id="xx" class="com.xx.SomeClass" p:imageUrl="${categories.images}"/>

The SomeClassinstance here has a property called imageUrlwhich is now injected with the value mentioned against the categories.imageskey from the property file called my-application.properties

SomeClass这里的实例有一个名为的属性imageUrl,它现在注入了针对categories.images名为的属性文件中的键提到的值my-application.properties

Hope this helps.

希望这可以帮助。