1

jdbcTemplateを介してMySqlデータベースへの単純なリクエストを実行しようとしましたが、フレームワークがxmlファイルをロードして解析し、データソースを定義するとエラーが発生します。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
      <property name="url" value="jdbc:mysql://localhost:3306/spring_training"/>
      <property name="username" value="root"/>
      <property name="password" value="pass"/>
    </bean>
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SpringTrainingTemplate</display-name>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-config.xml /WEB-INF/jdbc-config.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

およびそれを呼び出すコントローラー:

@Controller
public class HomeController {

@Autowired
private ExampleService exampleService;

@RequestMapping(value = "/details", method = RequestMethod.GET)
public String details(Model model) {

    ApplicationContext context = new ClassPathXmlApplicationContext("jdbc-config.xml");
    ExampleDao dao = (ExampleDao) context.getBean("ExampleDao");
    List<Application> list = dao.getAllApplications();

    model.addAttribute("application", list.get(0).getName());
    model.addAttribute("descriptionOfApplication", list.get(0).getDescription());

    return "details";
}
}

public class ExampleDao {

private String request = "select * from application";

private JdbcTemplate jdbcTemplate;

@Autowired
private DataSource dataSource;

public ExampleDao(DataSource dataSource) {
    this.jdbcTemplate = new JdbcTemplate(dataSource);
}

public List<Application> getAllApplications() {
    List<Application> applications = this.jdbcTemplate.query(request, new RowMapper<Application>() {
        @Override
        public Application mapRow(ResultSet rs, int i) throws SQLException {
            Application application = new Application();
            application.setName(rs.getString("name"));
            application.setType(rs.getString("type"));
            application.setDescription(rs.getString("description"));
            application.setDownloads(rs.getInt("downloads"));
            return application;
        }
    });
    return applications;
}
}

ここに画像の説明を入力してください

それを実行して入力するとhttp://localhost:8080/details、次のメッセージを含むスタックトレースで500の例外が発生します。

root cause
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [jdbc-config.xml]; nested exception is java.io.FileNotFoundException: class path resource [jdbc-config.xml] cannot be opened because it does not exist

jdbc接続を厳密に構成する方法、または問題の解決策を探す必要がある場所で私のアプローチが正しいかどうかを説明できますか?すべての助けをいただければ幸いです。ありがとう。

4

2 に答える 2

3

Springはjdbc-config.xml構成ファイルを見つけることができません。

WEB-INFフォルダーの代わりにクラスパスに配置して、次のようにweb.xmlにロードできます。

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-config.xml,classpath:jdbc-config.xml</param-value>
</context-param>

srcフォルダーにmainフォルダーとresourcesフォルダーを作成し、それらをクラスパスに追加することをお勧めします。次に、spring構成ファイルをsrc/resourcesフォルダーに配置できます。

于 2012-06-05T09:44:42.607 に答える
1

クラスパスリソース[jdbc-config.xml]が存在しないため、開くことができません

ファイル名は正しいですか、どこにありますか?db接続を指定するファイルは、クラスパス上で、本来あるべき場所にありません。

于 2012-06-05T09:32:51.840 に答える