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?
This is the page:
public class Page extends BasePage {
public Page() {
HelperObject object = new HelperObject(new Application("APP_NAME"));
String result = object.getData();
add(new Label("label", result));
}
}
Helper object:
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
}
}
@SpringBeanonly injects dependencies into classes that inherit from Wicket’sComponent.@Autowiredonly injects dependencies into classes created by Spring itself. That means you can’t automatically inject a dependency into an object you create withnew.(Edit: you can also add a
@SpringBeaninjection to your class by injecting in the constructor:InjectorHolder.getInjector().inject(this);)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 actuallyorg.apache.wicket.Application.) For example:In my Spring application context:
My helper object then looks up the service on demand: