我正在尝试使用jdbc和spring连接到数据库并获得以下异常的示例。请指教。
异常:
Aug 17, 2017 10:44:51 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6422b8ff: startup date [Thu Aug 17 22:44:51 IST 2017]; root of context hierarchy
Aug 17, 2017 10:44:51 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring.xml]
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'daoClass' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:638)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1159)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:282)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:979)
at org.manjosh.main.JdbcMain.main(JdbcMain.java:14)
主要课程:
package org.manjosh.main;
import org.manjosh.dao.DaoClass;
import org.manjosh.model.Circle;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class JdbcMain {
public static void main(String[] args){
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
DaoClass dao = ctx.getBean("daoClass",DaoClass.class);
Circle circle = dao.getCirle(1);
System.out.println(circle.getName());
}
}
dao class:
package org.manjosh.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.manjosh.model.Circle;
import org.springframework.stereotype.Component;
@Component
public class DaoClass {
public Circle getCirle(int circleId){
String driver = "oracle.jdbc.driver.OracleDriver";
Connection conn = null;
try{
//step1 load the driver class
Class.forName(driver).newInstance();
//step2 create the connection object
conn =DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
//step3 create the statement object
PreparedStatement stmt=conn.prepareStatement("SELECT * FROM circle where ID =?");
stmt.setInt(1, circleId);
Circle circle = null;
//step4 execute query
ResultSet rs=stmt.executeQuery();
while(rs.next()){
circle = new Circle(rs.getInt(circleId),rs.getString("name"));
}
rs.close();
stmt.close();
return circle;
}
catch(Exception e){
throw new RuntimeException(e);
}
finally {
try{
conn.close();
}
catch(SQLException e){}
}
}
}
spring 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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="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:annotation-config/>
<context:component-scan base-package = "org.manjosh.main"/>
</beans>
答案 0 :(得分:3)
在spring.xml
中,您已将组件扫描配置定义为<context:component-scan base-package = "org.manjosh.main"/>
,它将扫描org.manjosh.main
下的所有类。由于DaoClass
位于org.manjosh.dao
,因此spring不会创建DaoClass
的bean,因此会抛出异常NoSuchBeanDefinitionException
。
将<context:component-scan base-package = "org.manjosh.main"/>
更改为<context:component-scan base-package = "org.manjosh.main,org.manjosh.main"/>
答案 1 :(得分:1)
<context:component-scan>
扫描包以在应用程序上下文中查找和注册bean。
要解决NoSuchBeanDefinitionException问题,请在下面进行更改 spring.xml文件
替换
<context:component-scan base-package = "org.manjosh.main"/>
与
<context:component-scan base-package = "org.manjosh.dao,org.manjosh.main"/>
OR
<context:component-scan base-package = "org.manjosh.dao"/>
OR
<context:component-scan base-package = "org.manjosh.*"/>
OR
<context:component-scan base-package = "*"/>