Java springmvc中的属性文件

Java springmvc中的属性文件,java,spring,spring-mvc,Java,Spring,Spring Mvc,我有一个包含键值对的属性文件: key1=value1 key2=value2 现在,在我的控制器中,我想直接打印属性文件的值(当然是在使用web.xml/app servlet.xml加载属性文件之后),如: 有可能吗 若并没有,我想创建一个包含所有常量变量的接口,以从属性文件中读取值。我该怎么做 public interface MyConstants { @Value("${key1}") public static final KEY_1=""; } 但正如预

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

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

有可能吗

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

public interface MyConstants
{
      @Value("${key1}")
      public static final KEY_1="";
}
但正如预期的那样,只分配了空字符串


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

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

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

2) 另一个原因是,拥有一个存储常量的接口本身就是一种反模式。这不是设计接口的目的。您可能希望引用常量接口反模式


有两个原因说明为什么用“MyConstants”接口代替类是不正确的:

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

2) 另一个原因是,拥有一个存储常量的接口本身就是一种反模式。这不是设计接口的目的。您可能希望引用常量接口反模式

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

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

在Spring中,可以按如下方式创建属性文件的句柄:

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

正如代码所示,您可以在这里提到多个属性文件。执行此操作后,您可以在上下文中的其他位置引用上述属性文件中的密钥,如下所示:

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

这里的
SomeClass
实例有一个名为
imageUrl
的属性,该属性现在被注入了
类别中提到的值。images
键来自名为
my application.properties的属性文件

希望这能有所帮助。

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

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

在Spring中,可以按如下方式创建属性文件的句柄:

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

正如代码所示,您可以在这里提到多个属性文件。执行此操作后,您可以在上下文中的其他位置引用上述属性文件中的密钥,如下所示:

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

这里的
SomeClass
实例有一个名为
imageUrl
的属性,该属性现在被注入了
类别中提到的值。images
键来自名为
my application.properties的属性文件


希望这有帮助。

这是可能的!您需要在
app servlet.xml
中使用
util
命名空间,如下所示:

<?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>
更新另一种方式:

您还可以使用名称空间
上下文

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

这是可能的!您需要在
app servlet.xml
中使用
util
命名空间,如下所示:

<?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>
更新另一种方式:

您还可以使用名称空间
上下文

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

谢谢你的答复。。那么,在我的不同服务类中使用这些属性值的最佳方法是什么呢。。我不想创建接口,我不想在每个类中存储变量。。解决方案是什么???因为您的常量(在您的示例中看到它们是字符串)只是对池中实际字符串对象的引用,我个人认为在您各自的类中注入必要的常量没有错。但是,您可以随时替换现有的MyConstants并将其声明为类而不是接口。感谢您的回复。。那么,使用这些属性值的最佳方式是什么