我正在使用Spring Tool Suite IDE。 我的项目结构如下所示。
但是当我尝试运行控制器时,出现以下错误。
警告:org.springframework.web.context.support.XmlWebApplicationContext - 在上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.BeanCreationException:创建文件[F]中定义名为'textEditorController'的bean时出错:\ STS \ sts-bundle \ pivotal-tc-server-developer-3.2.4.SR1 \ base-instance \ wtpwebapps \ PostGres \ WEB-INF \ classes \ com \ postgres \ controllers \ TextEditorController.class]:bean的实例化失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化[com.postgres.controllers.TextEditorController]:构造函数抛出异常;嵌套异常是org.springframework.beans.factory.BeanDefinitionStoreException:IOException从类路径资源解析XML文档[Spring-Module.xml];嵌套异常是java.io.FileNotFoundException:类路径资源[Spring-Module.xml]无法打开,因为它不存在 错误:org.springframework.web.servlet.DispatcherServlet - 上下文初始化失败 org.springframework.beans.factory.BeanCreationException:在文件[F:\ STS \ sts-bundle \ pivotal-tc-server-developer-3.2.4.SR1 \ base-instance \ wtpwebapps中定义的名称为'textEditorController'的bean创建时出错\ PostGres \ WEB-INF \ classes \ com \ postgres \ controllers \ TextEditorController.class]:bean的实例化失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化[com.postgres.controllers.TextEditorController]:构造函数抛出异常;嵌套异常是org.springframework.beans.factory.BeanDefinitionStoreException:IOException从类路径资源解析XML文档[Spring-Module.xml];嵌套异常是java.io.FileNotFoundException:无法打开类路径资源[Spring-Module.xml],因为它不存在
TextEditorController.java
课程如下所示。
package com.postgres.controllers;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.postgres.dao.CustomerDao;
import com.postgres.models.Customer;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
/**
* Handles requests for the application home page.
*/
@Controller
public class TextEditorController {
ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml");
CustomerDao customerDAO = (CustomerDao) context.getBean("CustomerDao");
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/editor", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
//ApplicationContext context =
//new ClassPathXmlApplicationContext("Spring-Module.xml");
// CustomerDao customerDAO = (CustomerDao) context.getBean("CustomerDao");
// Customer customer = new Customer(5, "mkyong",28);
// customerDAO.insert();
// Customer customer1 = customerDAO.findByCustomerId(1);
// System.out.println(customer1);
logger.info("Welcome home! The client locale is {}.", locale);
return "TextEditor";
}
@RequestMapping(value= "/add", method = RequestMethod.POST)
public @ResponseBody
String add(HttpServletRequest request, HttpServletResponse response)
throws Exception {
String queryString = request.getParameter("query");
String msg = customerDAO.insert(queryString);
System.out.println(msg);
return msg;
}
@RequestMapping(value= "/result", method = RequestMethod.POST)
public @ResponseBody
String result(HttpServletRequest request, HttpServletResponse response)
throws Exception {
Map<Integer, List<String>> map = new HashMap<Integer, List<String>>();
String queryString = request.getParameter("query");
map = customerDAO.findByCustomerId(queryString);
Gson gson = new Gson();
String element = gson.toJson(map);
return element;
}
@RequestMapping(value= "/tables", method = RequestMethod.POST)
public @ResponseBody
String tables(HttpServletRequest request, HttpServletResponse response)
throws Exception {
Map<String, List<String>> alvalues = new HashMap<String, List<String>>();;
String queryString = request.getParameter("query");
// ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml");
//CustomerDao customerDAO = (CustomerDao) context.getBean("CustomerDao");
alvalues = customerDAO.getAllTables();
Gson gson = new Gson();
String element = gson.toJson(alvalues);
System.out.println(element);
return element;
}
@RequestMapping(value= "/queryplan", method = RequestMethod.POST)
public @ResponseBody
String queryplan(HttpServletRequest request, HttpServletResponse response)
throws Exception {
ArrayList<String> alvalues = new ArrayList<String>();
String queryString = request.getParameter("query");
//ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml");
//CustomerDao customerDAO = (CustomerDao) context.getBean("CustomerDao");
alvalues = customerDAO.getQueryPlans(queryString);
customerDAO.getQueryPlans(queryString);
Gson gson = new Gson();
String response1= gson.toJson(alvalues);
System.out.println(response1);
return response1;
}
@RequestMapping(value= "/setDatabase", method = RequestMethod.POST)
public @ResponseBody
String setDatabase(HttpServletRequest request, HttpServletResponse response)
throws Exception {
String database = request.getParameter("dataBase");
//ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml");
//CustomerDao customerDAO = (CustomerDao) context.getBean("CustomerDao");
String statement = customerDAO.setDefaultDatabse(database);
return statement;
}
}
因此,由于此错误,我的浏览器会抛出以下异常。
这方面的任何建议都将受到高度赞赏
答案 0 :(得分:0)
我认为代码中存在一个基本的误解。在您的控制器中,不需要为某个XML文件实例化ClassPathXmlApplicationContext,然后使用它来获取特定的bean。这不是依赖注入的全部内容。
如果你肯定想为你的Spring应用程序使用XML定义,你应该在那里定义你的bean(如果你愿意)并使用@Autowired,例如,将CustomerDAO实例注入你的控制器。
更好的解决方案是忘记所有XML配置并开始使用Spring Boot来实现您的Web应用程序。有数以百万计的例子如何做到这一点,包括一些指南(https://spring.io/guides)和一个很棒的项目页面,以便开始http://projects.spring.io/spring-boot/
希望有所帮助!!!