Spring @Autowired 在类新实例上 [英] Spring @Autowired on a class new instance
问题描述
我对Spring不太熟悉,有以下情况:
I'm not so familiar with Spring and I have the following situation:
存储库类:
@Repository
public class MyRepository {
// ...
}
使用存储库类的类:
public class MyClass extends AbstractClass {
@Autowired
private MyRepository myRepository;
//...
}
我知道如果我用 @Component
注释我的 MyClass
并将它与 @Autowired
一起使用,那么 @Autowired
MyRepository
解决得很好.问题是我需要使用反射创建 MyClass
的新实例.所以 MyRepository
永远不会被解析并且一直为空.
I know that if I annotate my MyClass
with @Component
and use it with an @Autowired
, then the @Autowired
MyRepository
is resolved just fine.
Problem is I am in a situation that I need to create new instances of MyClass
with reflection. So MyRepository
is never resolved and is null all the time.
有没有办法在这种情况下使用@Autowired
?
更好地解释我的情况:我有一些 AbstractClass
的实现.在我的应用程序的设置阶段,我创建了这些实现的 HashMap
.基本上:
Explaining better my situation:
I have some implementations of AbstractClass
.
In a setup phase of my application I create a HashMap
of these implementations. Basically:
{"MyClass", MyClass.class}
//...
然后我有一个通用的 Controller
映射到 url /{class}?options=...
使用 {class}
@PathVariable
、上面的 HashMap
和反射,我能够基于给定的 options
(这部分很重要).你们认为有更好的方法吗?
Then I have a generic Controller
that maps to the url /{class}?options=...
Using the {class}
@PathVariable
, the HashMap
above and reflection I am able to create a instance of a class based on the given options
(this part is important). Do you guys think there's a better way of doing this?
提前致谢
推荐答案
Spring 本身提供了一些在对象中进行自动装配的功能您通过 new
或 newInstance()
或其他方式创建的.
Spring itself offers some functionality for doing auto-wiring in your objects
which you created by new
or newInstance()
or whatever.
要使用它,您需要一个 AutowireCapableBeanFactory
您可以使用 @Autowired
通过 Spring 的常规依赖注入获得.
To use it you need an AutowireCapableBeanFactory
which you get by Spring's normal dependency injection with @Autowired
.
@Autowired
private AutowireCapableBeanFactory autowireCapableBeanFactory;
然后你使用它的 autowireBean(Object)
方法将 @Autowired
属性注入你的 bean.
Then you use its autowireBean(Object)
method
to inject the @Autowired
properties into your bean.
Object myBean = map.get(className).newInstance();
autowireCapableBeanFactory.autowireBean(myBean);
<小时>
设计说明:
Design note:
如果您真的需要上述方法,请三思.的javadocAutowireCapableBeanFactory
建议不要在大多数用例中使用此接口:
Think well if you really need the approach above.
The javadoc of AutowireCapableBeanFactory
advises against using this interface for most use-cases:
BeanFactory 的这个子接口不打算在正常的应用程序代码中使用:对于典型用例,坚持使用 BeanFactory
或 ListableBeanFactory
.
This subinterface of BeanFactory is not meant to be used in normal application code: stick to
BeanFactory
orListableBeanFactory
for typical use cases.
其他框架的集成代码可以利用这个接口来连接和填充 Spring 不控制其生命周期的现有 bean 实例.例如,这对于 WebWork Actions 和 Tapestry Page 对象特别有用.
Integration code for other frameworks can leverage this interface to wire and populate existing bean instances that Spring does not control the lifecycle of. This is particularly useful for WebWork Actions and Tapestry Page objects, for example.
这篇关于Spring @Autowired 在类新实例上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!