java 创建名为“..”的 bean 在文件 ..xml 中定义时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12165208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error creating bean with name '..' defined in file ..xml
提问by user1631032
can you help me please? 1.My Dao
你能帮我吗?1.我的道
package user.dao.impl;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import user.domain.DboBean;
public class DboDao {
private DataSourceTransactionManager transactionManager;
private JdbcTemplate jdbcTemplate;
public DboDao() {
// TODO Auto-generated constructor stub
super();
DataSource datatSource = transactionManager.getDataSource();
jdbcTemplate = new JdbcTemplate(datatSource);
}
//Spring setter injector used
public void setTransactionManager(DataSourceTransactionManager transactionManager){
this.transactionManager = transactionManager;
}
public DboBean read() {//String dboId
// TODO Auto-generated method stub
String sql = "SELECT ins_ID, ins_name FROM da_tracking "
+ "WHERE ins_ID = 3";
final Object[] params = new Object[] {};//dboId
//Creating object being queried
final DboBean dboBean = new DboBean();
//Process query Results
jdbcTemplate.query(sql, params, new RowCallbackHandler(){
public void processRow(ResultSet rs) throws SQLException{
dboBean.setDboId(rs.getString("dboId"));
}
});
return dboBean;//returns queried objects
}
}
2. My Bean:
2.我的豆子:
package user.domain;
public class DboBean {
private String dboId;
private String dboDevName;
public DboBean(){
this.setDboId(dboId);
this.setDboDevName(dboDevName);
}
public String getDboId() {
return dboId;
}
public void setDboId(String dboId) {
this.dboId = dboId;
}
public String getDboDevName() {
return dboDevName;
}
public void setDboDevName(String dboDevName) {
this.dboDevName = dboDevName;
}
}
My 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:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />
<property name="url"
value="jdbc:jtds:sqlserver://localhost:1433;databaseName=XYDB" />
<property name="username" value="user" />
<property name="password" value="xyz" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="objDbo" class="user.dao.impl.DboDao">
<property name="transactionManager" ref="transactionManager" />
</bean>
</beans>
And my Junit:
还有我的 Junit:
package user.dao.JUnitTest;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.log4j.xml.DOMConfigurator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import java.lang.Object;
import user.dao.impl.DboDao;
import user.domain.DboBean;
public class JUnitDaoTest extends TestCase {
private static final String OID = "dboId";
private static final String DDN = "dboDevName";
protected static ApplicationContext appContext;
private DboDao objDao;
public JUnitDaoTest(String arg) {
super(arg);
}
public static Test suite(){
TestSuite suite = new TestSuite();
suite.addTest(new JUnitDaoTest("testDboDAO"));
return suite;
}
public void setUp() throws Exception{
init();
objDao = (DboDao) getBean("objDao");
}
public static void main(String[] args){
junit.textui.TestRunner.run(suite());
}
//@Override
public synchronized void init(){
if (appContext == null){
try{
String path = System.getProperty("user.dir");
String slash = System.getProperty("file.separator");
String configDir = path + slash + "config" + slash;
DOMConfigurator.configure(configDir + "log4j.xml");
appContext = new FileSystemXmlApplicationContext(new String[] {configDir + "appContext.xml"});
}catch(Exception e){
System.err.println(e.getMessage());
}
}
}
protected Object getBean(String beanName){
return appContext.getBean(beanName);
}
public void testDboDAO(){
DboBean record = new DboBean();
record.setDboId(OID);//setDboId(dboId);
record.setDboDevName(DDN);
DboBean bean = objDao.read();
assertEquals(DDN, bean.getDboDevName());
System.out.println("User selected [" + bean.getDboId() + ", " + bean.getDboDevName() +
"]");
//objDao.delete(dboId);
//bean = objDao.read(dboId);
//assertEquals(null, bean.getDboDevName());
//System.out.println("User is deleted [" + bean.getDboId() + ", " + bean.getDboDevName() +
// "]");
}
}
}
I get this Error: Error creating bean with name 'objDbo' defined in file [...\config\appContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [user.dao.impl.DboDao]: Constructor threw exception; nested exception is java.l
我收到此错误:在文件 [...\config\appContext.xml] 中定义名称为“objDbo”的 bean 创建时出错:bean 实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 bean 类 [user.dao.impl.DboDao]:构造函数抛出异常;嵌套异常是 java.l
Have you an idea?? blob
你有什么想法吗??斑点
回答by matt b
The stacktrace will explain the exact problem, it will be in whatever follows the "nested exception is java.l..." part of the stacktrace you cut off. Read the whole error message.
堆栈跟踪将解释确切的问题,它将出现在您切断的堆栈跟踪的“嵌套异常是 java.l ...”部分之后的任何内容中。阅读整个错误消息。
If I were to take a guess, your DboDao
class has a null pointer exception in the constructor because you are referring to a field transactionManager
that is not set yet.
如果我猜测一下,您的DboDao
类在构造函数中存在空指针异常,因为您指的transactionManager
是尚未设置的字段。
I'd suggest writing a unit test of this DboDao
class - which doesn't involve Spring at all - to test what happens when you call new DboDao()
.
我建议编写此类的单元测试DboDao
- 根本不涉及 Spring - 以测试调用new DboDao()
.
Also you might find it convenient to have your DAO class just extend the Spring utility class JdbcDaoSupport
.
此外,您可能会发现让您的 DAO 类只是扩展 Spring 实用程序类很方便JdbcDaoSupport
。
回答by smp7d
Your DataSourceTransactionManager is not set when it is used in the constructor. This will result in an NPE when Spring is setting up your context.
您的 DataSourceTransactionManager 在构造函数中使用时未设置。当 Spring 设置您的上下文时,这将导致 NPE。
You can alternately just dependency inject the JdbcTemplate as your transaction manager is not being used except to set up your JdbcTemplate. If, for whatever reason, you dont want to configure your JdbcTemplate, you can use an init methodto build it (as it will be called after Spring supplies all dependencies).
您也可以只依赖注入 JdbcTemplate,因为除了设置 JdbcTemplate 之外,您的事务管理器没有被使用。如果出于某种原因,您不想配置 JdbcTemplate,则可以使用init 方法来构建它(因为它将在 Spring 提供所有依赖项后调用)。
回答by Nandkumar Tekale
public class DboDao {
private DataSourceTransactionManager transactionManager;
private JdbcTemplate jdbcTemplate;
public DboDao() {
// TODO Auto-generated constructor stub
super();
// problem exists here, as transactionManager is not set yet and
// you are using Setter Based injection so `NPE`
// you can move this code in setter method of transactionManager, to make it work.
DataSource datatSource = transactionManager.getDataSource();
jdbcTemplate = new JdbcTemplate(datatSource);
}.............
Also, I see wrong things in your code. Use transaction manager at service layer. And inject dataSource in DboDao. And create jdbcTemplate like as below:
另外,我在您的代码中看到了错误的内容。在服务层使用事务管理器。并在 DboDao 中注入数据源。并创建 jdbcTemplate 如下所示:
public class DboDao {
private DataSource dataSource; // either use @Autowire or provide xml config
private JdbcTemplate jdbcTemplate;
public DboDao() {
super();
}
....// getter and setter for jdbcTemplate and dataSource
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
jdbcTemplate = new JdbcTemplate(dataSource);
}
Looking at your configuration file, if you want to make your app run: intialize dataSource and jdbcTemplate in setter methods, as you are using setter based injection.
查看您的配置文件,如果您想让您的应用程序运行:在 setter 方法中初始化 dataSource 和 jdbcTemplate,因为您正在使用基于 setter 的注入。
public class DboDao {
private DataSourceTransactionManager transactionManager;
private JdbcTemplate jdbcTemplate;
public DboDao() {
super();
}
//Spring setter injector used
public void setTransactionManager(DataSourceTransactionManager transactionManager){
this.transactionManager = transactionManager;
// moved code from constructor
DataSource datatSource = transactionManager.getDataSource();
jdbcTemplate = new JdbcTemplate(datatSource);
}