pat3ck029
pat3ck029

Reputation: 273

Spring MVC Using Autowired Object Outside Controller Class

i'm using Spring MVC for my project. Is it possible to use the same autowired object on another class?

I have two controllers, one for navigation and one for the operations.

this is my sample navigation controller:

@Controller
public class NavigationController {

    @Autowired
    private DepartmentDAO deptDao;

    @RequestMapping("/")
    public ModelAndView panel() {
      ModelAndView mv = new ModelAndView("panel");
      Department dept = deptDao.getDept();
      mv.addObject("department",dept);
      return mv;
    }
}

then my operations controller:

 @Controller
 public class OperationController {

    @RequestMapping(value = "/save.do", method = RequestMethod.POST)
    public ModelAndView saveEvent(){
      ModelAndView mv = new ModelAndView("next");
      Event event = new EventCreator().createEvent();
      //some logic here
      return mv;
    }
 }

And this is my business delegate class :

public class EventCreator {
  public Event createEvent(){
   //logic here
   //I need to use the deptDao here.
  }
}

Thank You So Much!

Upvotes: 2

Views: 2922

Answers (2)

Dev
Dev

Reputation: 2325

You can @Autowired a spring bean object in another spring bean object

Assumption

I assume that you have declared DepartmentDAO with @Repository annotation as you haven't include code of DepartmentDAO in your question

There are two ways to solve your problem ,

  1. one is very well explain by @TimeTravel annotate EventCreator with @Component with create spring bean and you can easy autowire DepartmentDAO in the EventCreator class

  2. As You have two controller class which makes it spring beans as they are annotated with @Controller, what you can do you can Autowire departmentDAO in OperationController class and pass instance of DepartmentDAO as argument in EventCreator class constructor

@Controller
public class OperationController {
@Autowired
private DepartmentDAO deptDao;

@RequestMapping(value = "/save.do", method = RequestMethod.POST)
public ModelAndView saveEvent(){
  ModelAndView mv = new ModelAndView("next");
  Event event = new EventCreator(deptDao).createEvent();
  //some logic here
  return mv;
  }
 }


public class EventCreator {

 private DepartmentDAO deptDao=null; 

 public EventCreator(DepartmentDAO deptDaoAsArg){
  deptDao=deptDaoAsArg;
 }

 public Event createEvent(){
 //logic here
 //I need to use the deptDao here.
 }
}

Upvotes: 2

Omkar Puttagunta
Omkar Puttagunta

Reputation: 4156

You can simply Autowire DepartmentDAO in the EventCreator class, like how your autowired it in NavigationController class. Make sure that you annotate the EventCreator class with @Component and include it in the package where component scanning is done so that spring will autowire the DepartmentDao in your EventCreator class.

@Component
public class EventCreator {

  @Autowired
  private DepartmentDAO deptDao;

  public Event createEvent(){
   //logic here
   //I need to use the deptDao here.

  //deptDao.getAllDepartments();

  }
}

Upvotes: 4

Related Questions