[英]How to make spring inject value into a static field
我知道这可能看起来像以前问过的问题,但我在这里面临不同的问题。
我有一个实用程序类,它只有静态方法。 我没有,我也不会从中举个例子。
public class Utils{
private static Properties dataBaseAttr;
public static void methodA(){
}
public static void methodB(){
}
}
现在我需要 Spring 用数据库属性 Properties 填充 dataBaseAttr。Spring 配置是:
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<util:properties id="dataBaseAttr"
location="file:#{classPathVariable.path}/dataBaseAttr.properties" />
</beans>
我已经在其他 bean 中完成了,但是这个类 (Utils) 中的问题不是 bean,如果我把它变成一个 bean 没有任何改变我仍然不能使用变量,因为类不会被实例化并且变量总是等于空。
你有两种可能性:
org.springframework.beans.factory.config.MethodInvokingFactoryBean
调用静态设置器。在第一个选项中,您有一个带有常规 setter 的 bean,但您设置的是静态属性/字段,而不是设置实例属性。
public void setTheProperty(Object value) {
foo.bar.Class.STATIC_VALUE = value;
}
但是为了做到这一点,您需要有一个 bean 实例来公开此 setter(它更像是一种解决方法)。
在第二种情况下,它将按如下方式完成:
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="staticMethod" value="foo.bar.Class.setTheProperty"/> <property name="arguments"> <list> <ref bean="theProperty"/> </list> </property> </bean>
在你的情况下,你将在Utils
类上添加一个新的 setter:
public static setDataBaseAttr(Properties p)
在您的上下文中,您将使用上面举例说明的方法对其进行配置,或多或少类似于:
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="staticMethod" value="foo.bar.Utils.setDataBaseAttr"/> <property name="arguments"> <list> <ref bean="dataBaseAttr"/> </list> </property> </bean>
我有一个类似的要求:我需要将一个 Spring 管理的存储库 bean 注入我的Person
实体类(“实体”如“具有身份的东西”,例如 JPA 实体)。 一个Person
实例有朋友,为了让这个Person
实例返回它的朋友,它应该委托给它的存储库并在那里查询朋友。
@Entity
public class Person {
private static PersonRepository personRepository;
@Id
@GeneratedValue
private long id;
public static void setPersonRepository(PersonRepository personRepository){
this.personRepository = personRepository;
}
public Set<Person> getFriends(){
return personRepository.getFriends(id);
}
...
}
.
@Repository
public class PersonRepository {
public Person get Person(long id) {
// do database-related stuff
}
public Set<Person> getFriends(long id) {
// do database-related stuff
}
...
}
那么我如何将PersonRepository
单例注入到Person
类的静态字段中呢?
我创建了一个@Configuration
,它在Spring ApplicationContext 构建时被获取。 这个@Configuration
被注入了我需要作为静态字段注入到其他类中的所有bean。 然后使用@PostConstruct
注释,我抓住了一个钩子来执行所有静态字段注入逻辑。
@Configuration
public class StaticFieldInjectionConfiguration {
@Inject
private PersonRepository personRepository;
@PostConstruct
private void init() {
Person.setPersonRepository(personRepository);
}
}
由于这些答案很旧,我找到了这个替代方案。 它非常干净,仅适用于 java 注释:
要修复它,请创建一个“非静态设置器”来为静态变量分配注入的值。 例如:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class GlobalValue {
public static String DATABASE;
@Value("${mongodb.db}")
public void setDatabase(String db) {
DATABASE = db;
}
}
https://www.mkyong.com/spring/spring-inject-a-value-into-static-variables/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.