我知道这可能看起来像是一个先前提出的问题,但我在这里遇到了一个不同的问题.
我有一个只有静态方法的实用程序类.我没有,我不会从中得到一个实例.
public class Utils{
private static Properties dataBaseAttr;
public static void methodA(){
}
public static void methodB(){
}
}
Run Code Online (Sandbox Code Playgroud)
现在我需要Spring用数据库属性填充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>
Run Code Online (Sandbox Code Playgroud)
我已经在其他bean中完成了它,但是这个类(Utils)中的问题不是bean,如果我把它变成bean没什么变化我仍然不能使用变量,因为类不会被实例化并且变量总是等于null.