Human Being
Human Being

Reputation: 8387

how to access applicationContext.xml in Spring controller?

My controller class is in com.tps.sphbIntegration.controllers package

My applicationContext.xml file is in WEB-INF/spring/applicationContext.xml

In the controller class:

@Controller
@RequestMapping("jsp")
public class SpringController {

@RequestMapping(value="register.html" , method = RequestMethod.POST)
public String enterSucess(@Valid Login login , BindingResult result , Map model,HttpSession session){

    if(result.hasErrors()){
        System.out.println("Error happened...");
        return "register";
    }else{

    System.out.println("I am an controller for get method of jsp/success.html ");
    login = (Login) model.get("login");
    session.setAttribute("empId", login.getEmpId()) ;
    session.setAttribute("empName", login.getEmpName()) ;
    session.setAttribute("empPassword", login.getEmpPassword()) ;
    //session.setAttribute("empGender", login.getGender()) ;
    //session.setAttribute("empType", login.getEmpType()) ;

    ApplicationContext factory = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
    EmployeeDao dao=(EmployeeDao)factory.getBean("d");
    dao.saveEmployee(login);

    return "registerCheck";
    }

  }
}

When execution I got the exception as

java.io.FileNotFoundException: class path resource [spring/applicationContext.xml] cannot be opened because it does not exist

Please help me to set the path of applicationContext.xml in the controller or give some example that how to access the applicationContext.xml in controller.

Upvotes: 0

Views: 5417

Answers (4)

Solubris
Solubris

Reputation: 3763

I think this is the recommend way to gain access to the applicationContext:

@Autowired
private ApplicationContext applicationContext;

Dont need to use the "*Aware" classes this way.

Upvotes: 2

duffymo
duffymo

Reputation: 309008

You have to tell the servlet context loader listener where to find your Spring application context XML files in the web.xml. Your error suggests to me that you didn't do that.

If you do have it in your web.xml, check the paths to see if they're correct.

If the paths are correct, open the WAR file and see if the XML is missing. Perhaps you have a deployment and packaging issue.

A web app should NOT be calling this:

ApplicationContext factory = new ClassPathXmlApplicationContext("spring/applicationContext.xml");

This will work if there's a spring/applicationContext.xml in your WEB-INF/classes directory, but the preferred idiom is to use the ContextLoaderListener:

Use a ContextLoaderListener in accordance with DispatchServlet

You ought to be loading the application context for your entire app on startup, not for one controller and certainly not every time this URL is called by clients. Load it once on startup.

Upvotes: 3

Sam.Ge.CN
Sam.Ge.CN

Reputation: 1

no no no, you can do it like this! as will, I see there is an ANNOTATION(@Controller) in your class,that means your class has been managed by spring, if you create another Application in your method,there is two instance of ApplicationContext in memory. let your class implement the interface of ApplicationContextAware and override the method setApplication,and add a private member ApplicationContext, and then assign the value in that method

Upvotes: -1

Marko Topolnik
Marko Topolnik

Reputation: 200296

  1. Your controller can implement BeanFactoryAware, an interface through which it will get access to the already existing instance of the application context. You must not create the application context yourself.

  2. It is not quite clear from your code that you really need to access the context: it looks like you need the DAO injected into your Controller, through the standard dependency-injection mechanism of Spring.

Upvotes: 3

Related Questions