您可以通过执行以下解决方案之一来做到这一点:
使用构造函数@Autowired这种方法将构造需要一些bean作为构造函数参数的bean。在构造函数代码中,您可以将静态字段的值设置为构造函数执行的参数。样品:
使用@PostConstruct将值移交给静态字段@Componentpublic class Boo { private static Foo foo; @Autowired public Boo(Foo foo) { Boo.foo = foo; } public static void randomMethod() { foo.doStuff(); }}
这里的想法是在通过spring配置bean之后将bean移交给静态字段。
@Componentpublic class Boo { private static Foo foo; @Autowired private Foo tFoo; @PostConstruct public void init() { Boo.foo = tFoo; } public static void randomMethod() { foo.doStuff(); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)