Spring 命名参数异常
Spring NamedParameter Exception
我正在尝试使用 spring JdbcTemplate 中的命名参数在数据库中插入值。我正在对 NamedParameter 使用 setter 注入,但它显示异常。如果我使用构造函数注入那么它会起作用 fine.i 不知道我哪里做错了有人帮我解决这个问题
代码
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.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
修改您的xml。更改
<bean id="s" class="EmployeeDao">
<property name="template" ref="jtemplate"/>
</bean>
到
<bean id="s" class="EmployeeDao">
</bean>
然后在您的 EmployeeDao class 中,向您的 NamedParameterJdbcTemplate 对象添加一个 @Autowired 注释
改变
NamedParameterJdbcTemplate template;
到
@Autowired
private NamedParameterJdbcTemplate template;
现在让 Spring 为您设置。您也将不再需要 EmployeeDao class
中的 setTemplate(..) 方法
它给出了错误,因为您正在使用默认构造函数为 NamedParameterJdbcTemplate
创建 bean,但它不可用。
将您的 bean 定义修改为:
<bean id="jtemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="ds"/>
</bean>
希望对您有所帮助
我正在尝试使用 spring JdbcTemplate 中的命名参数在数据库中插入值。我正在对 NamedParameter 使用 setter 注入,但它显示异常。如果我使用构造函数注入那么它会起作用 fine.i 不知道我哪里做错了有人帮我解决这个问题
代码
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.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
修改您的xml。更改
<bean id="s" class="EmployeeDao">
<property name="template" ref="jtemplate"/>
</bean>
到
<bean id="s" class="EmployeeDao">
</bean>
然后在您的 EmployeeDao class 中,向您的 NamedParameterJdbcTemplate 对象添加一个 @Autowired 注释
改变
NamedParameterJdbcTemplate template;
到
@Autowired
private NamedParameterJdbcTemplate template;
现在让 Spring 为您设置。您也将不再需要 EmployeeDao class
中的 setTemplate(..) 方法它给出了错误,因为您正在使用默认构造函数为 NamedParameterJdbcTemplate
创建 bean,但它不可用。
将您的 bean 定义修改为:
<bean id="jtemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="ds"/>
</bean>
希望对您有所帮助