0

I have a controller class :

@Controller
public class MyController {

@AutoWired
Service myservice

@RenderMapping
public display(){
    //do work with myservice
}

}

I want to invoke the method display() from an external class but I am a null pointer exception.

Here is how I am invoking the display method from an external class :

new MyController.display()

But the instance myservice is being set to null.

How can I invoke MyController.display() and ensure the instance of myservice is not set to null ?

I think the problem is since I am creating a new instance of the controller the service is then not autowired ? But since the Spring controllers are singletons perhaps I can get access to the current instance of the controller ?

Update :

The reason I am trying this is I'm adding a config option to determine which controller display method should be implemented. Perhaps I should be using a super controller to determine which controller should be implemented ?

3
  • You need to get the Mycontroller using Spring Context so that all the dependencies are injected. The whole point of having spring is nullified if you are creating the instance on your own using new. Also do check if the setters and getters for myservice are in place Commented Oct 1, 2013 at 10:56
  • You cannot create instance of this class manually, it is managed by spring, there is dirty way to achieve this, but it is not recommended. at best you tell us, what is your use case, then we may help to find out a solution Commented Oct 1, 2013 at 11:09
  • We have similar case in our project, we had used abstract parent class, I will post you our solution, hope it can help you Commented Oct 1, 2013 at 12:24

2 Answers 2

1

The idea is: use a abstract parent class!

// this class has no mapping
public abstract class MyAbstractController() {
  @Autowired
  MyService service

  public String _display(Model model, ...) {
    // here is the implementation of display with all necessary parameters
    if(determine(..)){...}
    else {...}
  }

  // this determines the behavior of sub class
  public abstract boolean determin(...);
}

@Controller
@RequestMapping(...)
public class MyController1 extends MyAbstractController {

  @RequestMapping("context/mapping1")
  public String display(Model model, ...) {
    // you just pass all necessary parameters to super class, it will process them and give you the view back.
    return super._display(model, ...);
  }

  @Override
  public boolean determine(...) {
  // your logic for this 
  }
}

@Controller
@RequestMapping(...)
public class MyController2 extends MyAbstractController {

  @RequestMapping("context/mapping2")
  public String display(Model model, ...) {
    // you just pass all necessary parameters to super class, it will process them and give you the view back.
    return super._display(model, ...);
  }

  @Override
  public boolean determine(...) {
  // your logic for this 
  }
}

Hope this can help you...

Sign up to request clarification or add additional context in comments.

Comments

0

I think the problem is since I am creating a new instance of the controller the service is then not autowired ?

Yes. You can get access to your beans using BeanFactory API in Spring. But invoking controllers directly sounds fishy. Can you tell us what are you trying to achieve and may be we can see if there is a standard way of doing it?

1 Comment

Can you do a request forwarding? If yes, you can do DispatcherServlet forward to the other controller based on the config option. Something like this: stackoverflow.com/a/7366199/2231632

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.