2017-08-28 18 views
0

私はSpring Tool Suite IDEで作業しています。 私のプロジェクトの構造をここに示します。STSで定義されたコントローラを作成中にエラーが発生する

project structure

しかし、私は、コントローラを実行しようとすると、私はこの次のエラーを取得します。

WARN : org.springframework.web.context.support.XmlWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'textEditorController' defined in file [F:\STS\sts-bundle\pivotal-tc-server-developer-3.2.4.SR1\base-instance\wtpwebapps\PostGres\WEB-INF\classes\com\postgres\controllers\TextEditorController.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.postgres.controllers.TextEditorController]: Constructor threw exception; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [Spring-Module.xml]; nested exception is java.io.FileNotFoundException: class path resource [Spring-Module.xml] cannot be opened because it does not exist ERROR: org.springframework.web.servlet.DispatcherServlet - Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'textEditorController' defined in file [F:\STS\sts-bundle\pivotal-tc-server-developer-3.2.4.SR1\base-instance\wtpwebapps\PostGres\WEB-INF\classes\com\postgres\controllers\TextEditorController.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.postgres.controllers.TextEditorController]: Constructor threw exception; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [Spring-Module.xml]; nested exception is java.io.FileNotFoundException: class path resource [Spring-Module.xml] cannot be opened because it does not exist

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; 



} 

} 

このエラーのため、私のブラウザは以下の例外をスローします。

error

この点で任意の提案は非常に

+0

これはjava.io.FileNotFoundExceptionです:クラスパスのリソース[Spring-Module.xml]。あなたのconfigはファイルSpring-Module.xml –

+0

を見つけることができない可能性があります。もしそうなら、修正は正確には何ですか? –

+0

ファイル "Spring-Module.xml"のパスを表示 –

答えて

0

を理解されるであろう私は、コードに存在する根本的な誤解があると思います。あなたのコントローラでは、いくつかのXMLファイルに対してClassPathXmlApplicationContextをインスタンス化し、それを使って特定のBeanを取得する必要はありません。これは、依存性注入がすべてであるわけではありません。

SpringアプリケーションでXML定義を使用したい場合は、そこにBeanを定義して@Autowiredを使用するなどして、CustomerDAOインスタンスをコントローラに挿入する必要があります。

もっと良い解決策は、XML設定を一括して忘れて、あなたのWebアプリケーションを実装するためにSpring Bootを使い始めることです。数多くの例がそこにあります。多くのガイド(https://spring.io/guides)と始めるには素晴らしいプロジェクトページが含まれていますhttp://projects.spring.io/spring-boot/

関連する問題