Spring @Autowired在类新实例上 [英] Spring @Autowired on a class new instance

查看:156
本文介绍了Spring @Autowired在类新实例上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对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永远都不会解决,并且始终为null.

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?

Is there a way to use @Autowired in this situation?

更好地说明我的处境: 我有一些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本身提供了一些用于在对象中进行自动装配的功能 是由newnewInstance()或其他任何方法创建的.

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;

然后使用其

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:

如果您确实需要上述方法,请考虑好. 建议不要在大多数用例中使用此接口:

Think well if you really need the approach above. The javadoc of AutowireCapableBeanFactory advises against using this interface for most use-cases:

BeanFactory的此子接口不适合在常规应用程序代码中使用:对于典型用例,请坚持使用BeanFactoryListableBeanFactory.

其他框架的集成代码可以利用此接口来连接和填充Spring无法控制其生命周期的现有bean实例.例如,这对于WebWork操作和Tapestry页面对象特别有用.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆