Question

I "inherited" a large Spring application with a spring-ws service in it.

The service is the typical Spring-WS code:

package service;

@Endpoint
public class ServiceEndpoint {
    @Resource EntityDao entityDao;

    @PayloadRoot(localPart=...)
    @ResponsePayload
    public ResponseWrapperClass getServiceMethod(@RequestPayload RequestWrapperClass request) {
        return new ResponseProcessorClass(request).generateResponse();
    }
}

The service is massive, and the entityDao injected with the @Resource annotation is used widely in the class.

Because the specific method I'm working on is a bit complex, I have encapsulated it in a separate class in a subpackage.

The ResponseProcessorClass looks like this:

package service.business;
@Component
public class ResponseProcessorClass {
    @Resource EntityDao entityDao;

    public ResponseWrapperClass generateResponse() {
        entityDao.getSomeData(); //encapsulates hibernate logic -> Null Pointer Exception
    }
}

As you can see by my comments above, I get a Null Pointer Exception when using the @Resource annotation in my new class.

I haven't used this annotation before, but for what I understood from the documentation the type should be inferred and injected. I could not find any configuration in any XML file for it either.

Could anyone tell me why am I getting the NPE?

Était-ce utile?

La solution

Spring can only inject Spring managed beans into Spring managed beans. Therefore, if you create the object yourself, Spring can't do anything about it. This

return new ResponseProcessorClass(request).generateResponse();

is the problem. You are expecting Spring to inject a field of the ResponseProcessorClass object you created.

Try injecting a prototype bean instead of creating it yourself. This depends on your ResponseProcessorClass class.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top