Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring MVC公共应用程序配置文件作为静态类_Java_Spring Mvc_Configuration - Fatal编程技术网

Java Spring MVC公共应用程序配置文件作为静态类

Java Spring MVC公共应用程序配置文件作为静态类,java,spring-mvc,configuration,Java,Spring Mvc,Configuration,我想实现一些全局配置静态类,它将包含应用程序的所有配置。此外,我想从xml配置文件中插入这些参数 第一种方法是创建配置类并将其注入到我需要的每个bean/类中。但我不这么做,因为我的配置类包含所有应用程序的属性,并且在任何地方都注入它太。。。我不知道是什么) 第二种方法是尝试将xml配置值注入静态类,但它更像是一种变通方法 哪种方法更好,为什么更好?我看到这种情况最常见的处理方式是将配置放置到属性文件中,并通过PropertyPlaceHolderConfigure引用属性值 例如,假设我具有以

我想实现一些全局配置静态类,它将包含应用程序的所有配置。此外,我想从xml配置文件中插入这些参数

第一种方法是创建配置类并将其注入到我需要的每个bean/类中。但我不这么做,因为我的配置类包含所有应用程序的属性,并且在任何地方都注入它太。。。我不知道是什么)

第二种方法是尝试将xml配置值注入静态类,但它更像是一种变通方法


哪种方法更好,为什么更好?

我看到这种情况最常见的处理方式是将配置放置到属性文件中,并通过
PropertyPlaceHolderConfigure
引用属性值

例如,假设我具有以下属性:

SO.properties

app.name=StackOverflow
app.mode=debug
在我的Spring配置文件中,我将包含
上下文
名称空间,并通过
PropertyPlaceHolderConfigure
bean引用它

弹簧配置

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

如果我没记错的话,你不能做这种事。可以使用静态字段创建类,但不能向该字段注入属性。
看看这个答案,也许对你有帮助:

谢谢你的回复。但是在这个解决方案中,类不是静态的,所以每次我需要它时,我都必须初始化它。。。
@Component
public class MyBean{
   //This must be a Spring Bean


   //Wiring the value to the field
   @Value("#{app.name}")
   private String name;
}