简体   繁体   English

如何在Spring控制器中访问applicationContext.xml?

[英]how to access applicationContext.xml in Spring controller?

My controller class is in com.tps.sphbIntegration.controllers package 我的控制器类在com.tps.sphbIntegration.controllers包中

My applicationContext.xml file is in WEB-INF/spring/applicationContext.xml 我的applicationContext.xml文件位于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. 请帮助我在控制器中设置applicationContext.xml的路径,或举一些示例说明如何在控制器中访问applicationContext.xml

You have to tell the servlet context loader listener where to find your Spring application context XML files in the web.xml. 您必须告诉servlet上下文加载器侦听器在web.xml中的何处可以找到您的Spring应用程序上下文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. 如果确实在web.xml中包含了它,请检查路径以查看它们是否正确。

If the paths are correct, open the WAR file and see if the XML is missing. 如果路径正确,请打开WAR文件,然后查看XML是否丢失。 Perhaps you have a deployment and packaging issue. 也许您有部署和打包问题。

A web app should NOT be calling this: Web应用程序不应调用此命令:

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 : 如果您的WEB-INF/classes目录中有spring/applicationContext.xml ,这将起作用 ,但是首选的惯用法是使用ContextLoaderListener

Use a ContextLoaderListener in accordance with DispatchServlet 根据DispatchServlet使用ContextLoaderListener

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. 您应该在启动时为整个应用程序加载应用程序上下文,而不是为一个控制器加载,当然也不是每次客户端调用此URL时。 Load it once on startup. 在启动时加载一次。

  1. Your controller can implement BeanFactoryAware , an interface through which it will get access to the already existing instance of the application context. 您的控制器可以实现BeanFactoryAware ,通过该接口,它将可以访问应用程序上下文的现有实例。 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. 从代码中还不清楚您是否真的需要访问上下文:看起来您需要通过Spring的标准依赖项注入机制将DAO 注入到Controller中。

I think this is the recommend way to gain access to the applicationContext: 我认为这是获得对applicationContext的访问的推荐方法:

@Autowired
private ApplicationContext applicationContext;

Dont need to use the "*Aware" classes this way. 不需要通过这种方式使用“ * Aware”类。

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. 就像我将要看到的那样,我在您的类中看到一个ANNOTATION(@Controller),这意味着您的类已由spring管理,如果在您的方法中创建另一个Application,则内存中有两个ApplicationContext实例。 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 让您的类实现ApplicationContextAware的接口并覆盖方法setApplication,添加一个私有成员ApplicationContext,然后在该方法中分配值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM