java.lang.IllegalArgumentException: 属性 'dataSource' 是必需的 Spring JDBC
java.lang.IllegalArgumentException: Property 'dataSource' is required Spring JDBC
我不确定我做错了什么,我正在尝试使用 jdbc 调用来执行存储过程,但是每当我执行它的命中异常时
java.lang.IllegalArgumentException: 属性 'dataSource' 是必需的。
控制器Class:
@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;
}
}
服务Class:
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>
您控制器中的服务不受 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);
}
}
我不确定我做错了什么,我正在尝试使用 jdbc 调用来执行存储过程,但是每当我执行它的命中异常时 java.lang.IllegalArgumentException: 属性 'dataSource' 是必需的。
控制器Class:
@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;
}
}
服务Class:
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>
您控制器中的服务不受 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);
}
}