2015-11-20 74 views
1

我試圖在Spring中使用命名參數在db中插入值JdbcTemplate。我使用setter注入NamedParameter,但它顯示異常。如果我使用構造函數注入,然後它會工作fine.i不知道我做錯了任何一個可以幫助我解決這個Spring命名參數異常

代碼

Employee.java

public class Employee { 
    String name,pwd; 
    int en; 

    public Employee(String name,String pwd,int en){ 
     this.en=en; 
     this.name=name; 
     this.pwd=pwd; 
    } 

    public String getName(){ 
     return name; 
    } 

    public void setName(String name){ 
     this.name=name; 
    } 

    public String getPwd(){ 
     return pwd; 
    } 
    public void setPwd(String pwd){ 
     this.pwd=pwd; 
    } 

    public int getEn(){ 
     return en; 
    } 

    public void setEn(int en){ 
     this.en=en; 
    } 

} 

EmployeeDao.java

import java.sql.*; 

import org.springframework.jdbc.core.JdbcTemplate; 
import org.springframework.jdbc.core.PreparedStatementCallback; 
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; 

import java.util.*; 

import org.springframework.dao.DataAccessException; 

public class EmployeeDao { 

    NamedParameterJdbcTemplate template; 

    public void setTemplate(NamedParameterJdbcTemplate template){ 
     this.template=template; 
    } 

    public void save(Employee emp){ 
     String q="insert into users values(:name,:pwd,:en)"; 
     Map<String, Object> m=new HashMap<String, Object>(); 
     m.put("name",emp.getName()); 
     m.put("pwd",emp.getPwd()); 
     m.put("en",emp.getEn()); 

     template.execute(q,m,new PreparedStatementCallback() { 

      public Object doInPreparedStatement(PreparedStatement ps) throws SQLException,DataAccessException{ 
       return ps.executeUpdate(); 
      } 

     }); 
    } 

} 

Test.java

import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
public class Test { 

    public static void main(String args[]){ 
     ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationcontext.xml"); 

     EmployeeDao emp=(EmployeeDao)ctx.getBean("s"); 
     emp.save(new Employee("vijay","perfect",123)); 
    } 

} 

的applicationContext.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:p="http://www.springframework.org/schema/p" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 

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

<bean id="jtemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate"> 
<property name="dataSource" ref="ds"/> 
</bean> 
<bean id="s" class="EmployeeDao"> 
<property name="template" ref="jtemplate"/> 
</bean> 
</beans> 

堆棧跟蹤:

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment). 
log4j:WARN Please initialize the log4j system properly. 
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jtemplate' defined in class path resource [applicationcontext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate]: No default constructor found; nested exception is java.lang.NoSuchMethodException: org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.<init>() 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1013) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:959) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:490) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479) 
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139) 
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83) 
    at Test.main(Test.java:6) 
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate]: No default constructor found; nested exception is java.lang.NoSuchMethodException: org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.<init>() 
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:83) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1006) 
    ... 13 more 
Caused by: java.lang.NoSuchMethodException: org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.<init>() 
    at java.lang.Class.getConstructor0(Class.java:2892) 
    at java.lang.Class.getDeclaredConstructor(Class.java:2058) 
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:78) 
    ... 14 more 

回答

4

由於您使用默認構造函數來爲NamedParameterJdbcTemplate創建bean並且它不可用,所以發生錯誤。

修改你的bean定義爲:

​​

希望它可以幫助

0

修改您的XML。更改

<bean id="s" class="EmployeeDao"> 
    <property name="template" ref="jtemplate"/> 
</bean> 

<bean id="s" class="EmployeeDao"> 
</bean> 
在EmployeeDao類

然後,添加@Autowired註解你是NamedParameterJdbcTemplate對象

變化

NamedParameterJdbcTemplate template; 

@Autowired 
private NamedParameterJdbcTemplate template; 

現在你讓Spring爲你設置它。您也將不再需要EmployeeDao類中的setTemplate(..)方法