ska*_*man 110
简而言之,没有.您无法在Spring中自动装配或手动连接静态字段.你必须编写自己的逻辑才能做到这一点.
Sed*_*şar 99
@Component("NewClass")
public class NewClass{
private static SomeThing someThing;
@Autowired
public void setSomeThing(SomeThing someThing){
NewClass.someThing = someThing;
}
}
Run Code Online (Sandbox Code Playgroud)
vic*_*ugo 66
@Autowired 可以与setter一起使用,这样你就可以让setter修改静态字段.
只有一个最后的建议...... 不要
在@PostConstruct方法中初始化自动装配的组件
@Component
public class TestClass {
private static AutowiredTypeComponent component;
@Autowired
private AutowiredTypeComponent autowiredComponent;
@PostConstruct
private void init() {
component = this.autowiredComponent;
}
public static void testMethod() {
component.callTestMethod();
}
}
Run Code Online (Sandbox Code Playgroud)
想要添加自动接线静态字段(或常量)将被忽略的答案,但也不会产生任何错误:
@Autowired
private static String staticField = "staticValue";
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
121340 次 |
| 最近记录: |