2016-09-03 114 views
1

我不知道我在做什麼錯誤,我試圖執行存儲過程使用jdbc調用,但是當我執行它的打擊異常 java。 lang.IllegalArgumentException:屬性'dataSource'是必需的。java.lang.IllegalArgumentException:屬性'dataSource'是必需的Spring JDBC

控制器類:

@Controller 
    public class GpsController { 

     @RequestMapping(value="h",method=RequestMethod.POST)  
     public void show(@ModelAttribute("PredefinedPath") PredefinedPath predefinedPath) 

     { 

      PredefinedPathService service=new PredefinedPathService(); 
      //passing dto obj to service 
      boolean res= service.savePredefinedPath(predefinedPath); 
     } 

DTO CLASS:

//Entity Class Getters and setters 
    public class PredefinedPath { 

     public int getPredefinedPath_Id() { 
      return PredefinedPath_Id; 
     } 
     public void setPredefinedPath_Id(int predefinedPath_Id) { 
      PredefinedPath_Id = predefinedPath_Id; 
     } 
     public String getPredefinedPath_Name() { 
      return PredefinedPath_Name; 
     } 
     public void setPredefinedPath_Name(String predefinedPath_Name) { 
      PredefinedPath_Name = predefinedPath_Name; 
     } 
     public String getSource() { 
      return Source; 
     } 
     public void setSource(String source) { 
      Source = source; 
     } 
     public String getDestination() { 
      return Destination; 
     } 
     public void setDestination(String destination) { 
      Destination = destination; 
     } 
     public String getEstimate_km() { 
      return Estimate_km; 
     } 
     public void setEstimate_km(String estimate_km) { 
      Estimate_km = estimate_km; 
     } 
     public double getSource_Latitude() { 
      return Source_Latitude; 
     } 
     public void setSource_Latitude(double source_Latitude) { 
      Source_Latitude = source_Latitude; 
     } 
     public double getSource_Longtitude() { 
      return Source_Longtitude; 
     } 
     public void setSource_Longtitude(double source_Longtitude) { 
      Source_Longtitude = source_Longtitude; 
     } 
     public double getDestination_Latitude() { 
      return Destination_Latitude; 
     } 
     public void setDestination_Latitude(double destination_Latitude) { 
      Destination_Latitude = destination_Latitude; 
     } 
     public double getDestination_Longtitude() { 
      return Destination_Longtitude; 
     } 
     public void setDestination_Longtitude(double destination_Longtitude) { 
      Destination_Longtitude = destination_Longtitude; 
     } 
     public String getEffectiveFromDate() { 
      return EffectiveFromDate; 
     } 
     public void setEffectiveFromDate(String effectiveFromDate) { 
      EffectiveFromDate = effectiveFromDate; 
     } 
     public String getEffectiveToDate() { 
      return EffectiveToDate; 
     } 
     public void setEffectiveToDate(String effectiveToDate) { 
      EffectiveToDate = effectiveToDate; 
     } 
     public int getStatus_Id() { 
      return Status_Id; 
     } 
     public void setStatus_Id(int status_Id) { 
      Status_Id = status_Id; 
     } 
     private int PredefinedPath_Id; 
     private String PredefinedPath_Name; 
     private String Source; 
     private String Destination; 
     private String Estimate_km; 
     private double Source_Latitude; 
     private double Source_Longtitude; 
     private double Destination_Latitude; 
     private double Destination_Longtitude; 
     private String EffectiveFromDate; 
     private String EffectiveToDate; 
     private int Status_Id; 


    } 

DAO CLASS:

 import javax.sql.DataSource; 

     import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; 
     import org.springframework.jdbc.core.namedparam.SqlParameterSource; 
     import org.springframework.jdbc.core.simple.SimpleJdbcCall; 
     import com.Ss.App.dto.PredefinedPath; 

     public class PredefinedPathDaoImpl { 

      private DataSource dataSource; 
       private SimpleJdbcCall jdbcCall; 

       public void setDataSource(DataSource dataSource) { 
        this.dataSource = dataSource; 

       } 


      public boolean savePredefined(PredefinedPath predefinedPathDto) 
      { 


        System.out.println("INSIDE DAO"); 
        this.jdbcCall = new SimpleJdbcCall(dataSource).withProcedureName("PredefinedPath_Insert"); 
        SqlParameterSource sqlParameterSource = new MapSqlParameterSource() 
        .addValue("PredefinedPath_Name", predefinedPathDto.getPredefinedPath_Name()) 
        .addValue("Source", predefinedPathDto.getSource()) 
        .addValue("Destination", predefinedPathDto.getDestination()) 
        .addValue("Source_Latitude", predefinedPathDto.getSource_Latitude()) 
        .addValue("Source_Longtitude", predefinedPathDto.getSource_Longtitude()) 
        .addValue("Destination_Latitude", predefinedPathDto.getDestination_Latitude()) 
        .addValue("Destination_Longtitude", predefinedPathDto.getDestination_Longtitude()) 
        .addValue("EffectiveFromDate",null) 
        .addValue("EffectiveToDate",null) 
        .addValue("Status_Id", 4); 
        jdbcCall.execute(sqlParameterSource); 






       return true; 
      } 
     } 

服務類:

import com.Ss.App.dto.PredefinedPath; 

    import com.Ss.App.model.dao.PredefinedPathDaoImpl; 

    public class PredefinedPathService { 

     PredefinedPathDaoImpl predefinedPathDao=new PredefinedPathDaoImpl(); 
     public boolean savePredefinedPath(PredefinedPath predefinedPathdto) 
     { 

      boolean result=predefinedPathDao.savePredefined(predefinedPathdto); 

      return result; 


     } 
    } 

Spring配置:

<?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:context="http://www.springframework.org/schema/context" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xsi:schemaLocation="http://www.springframework.org/schema/mvc 
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
      http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 


    <context:component-scan base-package="com.Ss.App"></context:component-scan> 

      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
      <property name="prefix" value="/view/"/> 
      <property name="suffix" value=".jsp"/> 

      </bean> 
      <!-- Definition for studentJDBCTemplate bean --> 
     <bean id="PredefinedPathDaoImpl" class="com.Ss.App.model.dao.PredefinedPathDaoImpl"> 
      <property name="DataSource" ref="dataSource" />  
     </bean> 
     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
      <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property> 
      <property name="url" value="jdbc:sqlserver://localhost:1433;DatabaseName=test"></property> 
      <property name="username" value="sa"></property> 
      <property name="password" value="pass"></property> 
     </bean> 




    <mvc:annotation-driven /> 



      </beans> 

的web.config

<?xml version="1.0" encoding="UTF-8"?> 
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> 
     <display-name>SpringAngularjs</display-name> 
     <welcome-file-list> 
     <welcome-file>page.jsp</welcome-file> 

     </welcome-file-list> 
     <servlet> 
     <servlet-name>spring</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     </servlet> 

     <servlet-mapping> 
     <servlet-name>spring</servlet-name> 
     <url-pattern>/</url-pattern> 
     </servlet-mapping> 
    </web-app>  
+0

請加堆棧跟蹤 – benbenw

+0

這裏是堆棧跟蹤:http://pastebin.com/YA7KsBnA – Broderick

回答

2

在控制器的服務爲你自己動手實例化不是由Spring管理。

這就是爲什麼數據源未在此bean中設置的原因。

替換:

@Controller 
public class GpsController { 

     @RequestMapping(value="h",method=RequestMethod.POST)  
     public void show(@ModelAttribute("PredefinedPath") PredefinedPath predefinedPath) 
     { 

      PredefinedPathService service=new PredefinedPathService(); 
      //passing dto obj to service 
      boolean res = service.savePredefinedPath(predefinedPath); 
     } 
} 

通過

@Controller 
public class GpsController { 
    @Autowired 
    private PredefinedPathService service; 

    @RequestMapping(value="h",method=RequestMethod.POST)  
    public void show(@ModelAttribute("PredefinedPath") PredefinedPath predefinedPath) 
     { 
      //passing dto obj to service 
      boolean res = service.savePredefinedPath(predefinedPath); 
     } 
} 
+0

由於它現在是固定的。 – Broderick

+0

然後接受我的回答。 – benbenw

相關問題