How to access applicationContext from controller

I am using the Spring MVC project from Netbeans and I moved the applicationContext.xml file to / src / conf because I read WEB-INF, this is not the right folder. I cannot access the application context from the controller in / src / java / web / controller. I tried several ways and it does not deploy the project.

I need a link to learn more about paths in a web project, please.

I think this may help us understand:

public class TasksController implements Controller { private TaskManager taskManager; protected final Log logger = LogFactory.getLog(getClass()); public TaskController() { ApplicationContext context = new FileSystemXmlApplicationContext("/WEB-INF/applicationContext.xml"); taskManager = (TaskManager)context.getBean("taskManager"); } @Override public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { logger.info("Returning view from TaskController"); Map<String,Object> tasks = new HashMap<String,Object>(); // Get tasks from model return new ModelAndView("tasks","tasks",tasks); } 

Bye!

+6
source share
1 answer

Uch. OK, you are not creating new contexts from your controller. The context is already configured using Spring, you just need to set Spring for it.

Make your controller an implementation of BeanFactoryAware , and Spring then inserts the context for you, automatically calling setBeanFactory :

 public class TasksController implements Controller, BeanFactoryAware { private TaskManager taskManager; public void setBeanFactory(BeanFactory context) { taskManager = (TaskManager)context.getBean("taskManager"); } // handleRequest as before } 
+13
source

Source: https://habr.com/ru/post/905464/


All Articles