使用@Autowired将依赖关系注入到使用“new ...”创建的对象中。 [英] Injecting dependencies using @Autowired into objects created with "new ..."
问题描述
将bean注入helper类时遇到问题。它基本上是这样的:我在页面构造函数中创建了一个对象,它可以完成一些工作,返回一些数据并在页面上显示这些数据。在此辅助对象中,应通过 @Autowired
注释注入服务。但是,当我使用它时,我总是得到一个空指针异常。我也试过 @SpringBean
但它没有帮助。另一方面,当我使用 @SpringBean
将此服务直接注入页面时,它可以访问并且正常工作。你知道问题出在哪里吗?
I have a problem with injecting a bean into a helper class. It works basically like this: I create an object in the page constructor that does some work, returns some data and I show these on the page. In this helper object, a service should be injected via @Autowired
annotation. However, I always get a null pointer exception when I use it. I also tried @SpringBean
but it didn't help. On the other hand, when I inject this service directly into the page with @SpringBean
, it's accessible and works fine. Do you know where the problem is?
这是页面:
public class Page extends BasePage {
public Page() {
HelperObject object = new HelperObject(new Application("APP_NAME"));
String result = object.getData();
add(new Label("label", result));
}
}
帮助对象:
public class HelperObject {
private Application app;
@Autowired
private Service service;
public HelperObject(Application app) {
this.app = app;
}
public String getData() {
// use service, manipulate data, return a string
}
}
推荐答案
@SpringBean
仅注入依赖于继承自Wicket的组件
的类。 @Autowired
仅将依赖项注入Spring自己创建的类中。这意味着您不能自动将依赖项注入您使用 new
创建的对象。
@SpringBean
only injects dependencies into classes that inherit from Wicket's Component
. @Autowired
only injects dependencies into classes created by Spring itself. That means you can't automatically inject a dependency into an object you create with new
.
(编辑:你也可以通过在构造函数中注入来向您的类添加 @SpringBean
注入:
InjectorHolder.getInjector()。inject(this);
)
( you can also add a @SpringBean
injection to your class by injecting in the constructor:
InjectorHolder.getInjector().inject(this);
)
我的正常解决方法是使用我的应用程序类来提供帮助。 (我对你使用新应用程序(...)
感到有些困惑。我认为这实际上不是 org.apache.wicket .Application
。)例如:
My normal workaround for this is to use my application class to help. (I'm a little puzzled by your use of new Application(...)
. I assume this isn't actually org.apache.wicket.Application
.) For example:
public class MyApplication extends AuthenticatedWebApplication implements
ApplicationContextAware {
private ApplicationContext ctx;
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.ctx = applicationContext;
}
public static MyApplication get() {
return (MyApplication) WebApplication.get();
}
public static Object getSpringBean(String bean) {
return get().ctx.getBean(bean);
}
public static <T> T getSpringBean(Class<T> bean) {
return get().ctx.getBean(bean);
}
....
}
在我的Spring应用程序上下文中:
In my Spring application context:
<!-- Set up wicket application -->
<bean id="wicketApplication" class="uk.co.humboldt.Project.MyApplication"/>
我的帮助对象然后按需查找服务:
My helper object then looks up the service on demand:
public class HelperObject {
private Service getService() {
return MyApplication.getSpringBean(Service.class);
}
这篇关于使用@Autowired将依赖关系注入到使用“new ...”创建的对象中。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!