0

I have a base abstract controller class that contains a generic functionality. I also have a set of subclasses. The abstract class has a property that i would like to Dependency inject. This property is common to all subclasses therefore i don't want it to be set on all the subclasses. but when I call abstract controller's function in subclass ,it turns out to be the property in abstract controller is null. I want to know why and how to fix it.Below is the code snippet:
Abstract Controller:

@Controller
public abstract class WebAPIBaseController {
    @Resource 
    private IPermissionService permissionService;

    public void validPermission(int user,String code){
       permissionService.valid(user,code);
    }
}

SubController

@Controller
@RequestMapping("/order")
public class OrderController extends WebAPIBaseController {

   public String XXX(){
    validPermission(1,"code");//it will throw a NullPointerException
}
}

besides,if I remove abstract controller(like below example) , it works good.
Remove abstract controller

@Controller
@RequestMapping("/order")
public class OrderController{
    @Resource 
    private IPermissionService permissionService;

    public void validPermission(int user,String code){
       permissionService.valid(user,code);
    }
    public String XXX(){
    validPermission(1,"code");//it works good 
    }
}
mengying.ye
  • 837
  • 2
  • 10
  • 21
  • Your code, even though there are some syntax errors, works as it is regarding the injection of the service. It does not give me any problem. Show the code to your Service, because maybe that is the problem. – dambros Apr 26 '16 at 07:41
  • In fact , I get permissionService by SOA, just like `permissionService = ClientUtils.getClient(IPermissionService.class);` in the abstract controller. The permissionService is good,because if I get permissionService in subController directly , it works good. – mengying.ye Apr 26 '16 at 07:45
  • Really hard to say, since it works just fine here. How do you call `XXX()`? – dambros Apr 26 '16 at 07:51
  • the XXX is just a webapi for frontEnd ,Is it possible some wrong with soa? – mengying.ye Apr 26 '16 at 07:54
  • Hard to tell. I am calling the endpoint from the frontend directly and using spring boot with your exact code (just changed tje service implementation of course). – dambros Apr 26 '16 at 07:55
  • Sorry, it's my fault , I use @Resource instead of soa, it works good. – mengying.ye Apr 26 '16 at 08:39

3 Answers3

0

I don't think you need to inject the permissionService in the subclass, doing this you are hiding that of the superclass.

as3rdaccount
  • 3,711
  • 12
  • 42
  • 62
0

Have a look at this thread Spring can you autowire inside an abstract class? . You'll also find two other threads in one of the replies about this topic.

Community
  • 1
  • 1
0

You could use @Autowired over the subclass constructor:

public abstract class WebAPIBaseController {

  private final IPermissionService permissionService;

  public WebAPIBaseController(IPermissionService permissionService) {
    this.permissionService = permissionService;
  }

  public void validPermission(int user, String code){
    permissionService.valid(user,code);
  }

}

@Controller
@RequestMapping("/order")
public class OrderController extends WebAPIBaseController {

  @Autowired
  public OrderController(IPermissionService permissionService) {
    super(permissionService);
  }

  public String XXX(){
    validPermission(1,"code");//it will throw a NullPointerException
  }

}
sp00m
  • 47,968
  • 31
  • 142
  • 252