Spring:如何向静态字段注入值?
对于此类
@Component
public class Sample {
@Value("${my.name}")
public static String name;
}
,如果我尝试 Sample.name
,它始终为“null”。所以我尝试了这个。
public class Sample {
public static String name;
@PostConstruct
public void init(){
name = privateName;
}
@Value("${my.name}")
private String privateName;
public String getPrivateName() {
return privateName;
}
public void setPrivateName(String privateName) {
this.privateName = privateName;
}
}
这段代码有效。 Sample.name
设置正确。这是好还是不好?如果不行的话还有什么更好的办法吗?以及如何做呢?
With this class
@Component
public class Sample {
@Value("${my.name}")
public static String name;
}
If I try Sample.name
, it is always 'null'. So I tried this.
public class Sample {
public static String name;
@PostConstruct
public void init(){
name = privateName;
}
@Value("${my.name}")
private String privateName;
public String getPrivateName() {
return privateName;
}
public void setPrivateName(String privateName) {
this.privateName = privateName;
}
}
This code works. Sample.name
is set properly. Is this good way or not? If not, is there something more good way? And how to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,
public static
非final
字段是邪恶的。 Spring 不允许注入此类字段是有原因的。您的解决方法是有效的,您甚至不需要 getter/setter,
private
字段就足够了。另一方面,试试这个:(与
@Autowired
/@Resource
一起使用)。但给你一些建设性的建议:创建第二个类,其中包含private
字段和 getter,而不是public static
字段。First of all,
public static
non-final
fields are evil. Spring does not allow injecting to such fields for a reason.Your workaround is valid, you don't even need getter/setter,
private
field is enough. On the other hand try this:(works with
@Autowired
/@Resource
). But to give you some constructive advice: Create a second class withprivate
field and getter instead ofpublic static
field.此信息的来源是:https://www.baeldung.com/ spring-inject-static-field
Spring 在找到 @Value 注解时使用依赖注入来填充特定值。但是,它不是将值传递给实例变量,而是传递给隐式设置器。然后,该设置器处理 NAME_STATIC 值的填充。
Soruce of this info is this: https://www.baeldung.com/spring-inject-static-field
Spring uses dependency injection to populate the specific value when it finds the @Value annotation. However, instead of handing the value to the instance variable, it's handed to the implicit setter instead. This setter then handles the population of our NAME_STATIC value.
这是我加载静态变量的示例代码
This is my sample code for load static variable
尽管可能需要实施变通办法,但如果可能的话,在大多数情况下应该尽量避免它们。 Spring 非常擅长处理依赖注入,并将大多数对象视为单例。这意味着 Spring 可以为您处理对象的创建,以及在运行时注入这些对象。当将此与 Spring 托管 bean 可能是 Singleton 的事实结合起来时,静态方法和变量的使用基本上是不必要的。您可以简单地在构造函数级别或变量级别自动装配您正在查找的对象的实例,并引用方法或变量的非静态版本。这是理想的情况,其行为与静态引用类似。非静态变量基本上是静态的,因为您在代码的每一部分中只使用了对象的一个实例,并且由于依赖注入,您永远不会处理对象的实例化,就像静态引用一样!伟大的!现在我确信在某些情况下您需要解决方法(即您没有使用依赖项注入或类不是单例),但如果可能的话尽量不要使用解决方法。而且这只是我的 2 美分。有人可能可以提供 3.(:
Although workarounds may need to be implemented, one should try to avoid them in most scenarios if possible. Spring is great at handling dependency injection and treats most objects as Singletons. This means that Spring can handle the creation of objects for you, and the injection of these objects at runtime. When combining this with the fact that your Spring managed bean is likely a Singleton, the use of static methods and variables is largely unnecessary. You can simply autowire in an instance of the object you are looking for at the constructor level or variable level and reference the non-static version of the method or variable. This is ideal and behaves similarly to a static reference. Non static variables are basically static because you are only ever using one instance of the object in every part of the code and because of dependency injection you are never handling the instantiation of the object, just like with a static reference! Great! Now I'm sure there are instances where you need the work around (i.e. you aren't using dependency injection or class is not a singleton), but try to not use workarounds if possible. Also this is just my 2 cents. Someone may be able to offer 3. (: