java - Service injected into spring controller is not available in one of the functions -
i writing spring controller, injects bean.
the bean added in config(we use java config everything):
@bean public notificationservice notificationservice() { return new notificationservice(); }
the service has few injected dependencies , few functions:
public class notificationservice { @inject notificationrepository notificationrepository; @inject projectrepository projectrepository; @inject modelmapper modelmapper; public notificationdto create(notificationdto notificationdto) { //convert domain object, save, return dto updated id return notificationdto; } public void markasread(long id, string recipientnip) { //find notification, update status } }
model mapper has no configuration, set strict. meanwhile repositoriers interfaces extending jparepository
no custom functions. found @enablejparepositories
.
finally have controller tries use code above:
@restcontroller @requestmapping("/notifications") public class notificationcontroller extends exceptionhandlercontroller { @autowired private notificationservice notificationservice; @preauthorize("isfullyauthenticated() , hasrole('create_notification')") @requestmapping(method = requestmethod.post, consumes = mediatypeextension.application_json_utf8_value) public responseentity<?> createnotification(@valid @requestbody(required = true) final notificationdto notification) { this.notificationservice.create(notification); final httpheaders headers = new httpheaders(); return new responseentity<>(headers, httpstatus.created); } @preauthorize("isfullyauthenticated() , hasrole('update_notification')") @requestmapping(value = "/{id}/read", method = requestmethod.put) private responseentity<?> marknotificationasread(@pathvariable("id") long id, @authenticatedcontractor contractordto contractor) { this.notificationservice.markasread(id, contractor.getnip()); final httpheaders headers = new httpheaders(); return new responseentity<>(headers, httpstatus.ok); } }
all controllers added trough @componentscan, based on package.
as can see both functions use notificationservice
. when send post
create
on /notifications
notificationservice
injected. in same controller, when put
request on /{id}/read
, notificationservice
null
.
i think has spring putting things container, , reason not being able 1 function. have few more functions in controller , in of them notificationservice
injected. don't see real difference between createnotification
, marknotificationasread
functions , couldn't find remotely related on google/stack. in cases service wouldn't inject @ because of configuration mistake.
edit have tried changing things around in function until has started working. final code looks this:
@preauthorize("isfullyauthenticated() , hasrole('update_notification')") @requestmapping(value = "{id}/read", method = requestmethod.put) public responseentity<?> read(@pathvariable("id") long id, @authenticatedcontractor contractordto contractor) { this.notificationservice.markasread(id, contractor.getnip()); final httpheaders headers = new httpheaders(); return new responseentity<>(headers, httpstatus.ok); }
and works. can't see difference original code, , have been staring @ last hour or so. imports same too.
i have noticed(on unworking code) while functions controller on debug stack marked as
notificationcontroller.functionname(arguments) line: x
the non working function was:
notificationcontroller$$enhancerbyspringcglib$$64d88bfe(notificationcontroller).marknotificationasread(contractordto) line: 86
why single function enhanced spring cglib have no idea. have tried looking up, came empty handed. though code started work leaving question open in order find underlying cause.
your method marknotificationasread
private , causes issue. i've had same issue final method - message appeared in log:
2016-11-28 17:19:14.186 info 97079 --- [ main] o.s.aop.framework.cglibaopproxy : unable proxy method [public final java.util.map com.package.controller.mycontroller.somemethod(javax.servlet.http.httpservletresponse)] because final: calls method via proxy not routed target instance.
looks in 1 case see cglib proxy, , in - actual class. 1 of has fields injected, looks proxy has fields nulls. doesn't matter - point method should public , not final in order proxied @preauthorize methods.
Comments
Post a Comment