Spring: How to define database config? -


i try execute simple request mysql database via jdbctemplate have error when framework load , parse xml file whicj define datasource:

<?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> 

and controller invoke it:

@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 * 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; } } 

enter image description here

whe run , input http://localhost:8080/details have got 500 exception stacktrace message:

root cause org.springframework.beans.factory.beandefinitionstoreexception: ioexception parsing xml document class path resource [jdbc-config.xml]; nested exception java.io.filenotfoundexception: class path resource [jdbc-config.xml] cannot opened because not exist 

can explain me how configure jdbc connection in rigth way or if approach correct should solution of issue? appreciated. thanks.

spring cannot find jdbc-config.xml configuration file.

you can put in classpath instead of web-inf folder , load in web.xml this:

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

a practice create folders main , resources in src folder , add them in classpath. can put spring config file in src/resources folder.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -