【问题标题】:org.springframework.beans.factory.NoSuchBeanDefinitionException Error while running spring applicationorg.springframework.beans.factory.NoSuchBeanDefinitionException 运行spring应用程序时出错
【发布时间】:2013-12-29 02:29:51
【问题描述】:

我看过其他相关问题,但对解决方案不满意。

我是 spring 新手,正在尝试来自 http://www.tutorialspoint.com/spring/spring_jdbc_example.htm 的示例

我已根据自己的要求对上述教程进行了更改,例如使用的城市、国家/地区 实体而不是学生。

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

<!-- Initialization for data source -->
<bean id="dataSource" 
  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  <property name="url" value="jdbc:mysql://localhost:3306/testdb"/>
  <property name="username" value="root"/>
  <property name="password" value="password"/>
</bean>

<!-- Definition for countryJDBCTemplate bean -->
<bean id="countryJDBCTemplate" 
  class="nz.org.tcf.CountryJDBCTemplate">
  <property name="dataSource"  ref="dataSource" />    
</bean>

</beans>

CountryJDBCTemplate.java 如下所示:

package nz.org.tcf;

import java.util.List;
import javax.sql.DataSource;

import nz.org.tcf.v0_0_1.bif.dao.CountryDAO;
import nz.org.tcf.v0_0_1.bif.pojo.Country;

import org.springframework.jdbc.core.JdbcTemplate;

public class CountryJDBCTemplate implements CountryDAO {
private DataSource dataSource;
private JdbcTemplate jdbcTemplateObject;

public void setDataSource(DataSource dataSource) {
  this.dataSource = dataSource;
  this.jdbcTemplateObject = new JdbcTemplate(dataSource);}

public void create(Integer population,String district,String countrycode,String name,      Integer id) {
String SQL = "insert into Student (population,district,countrycode,name,id) values   (?,?,?,?,?)";

jdbcTemplateObject.update( SQL, population,district,countrycode,name,id);
System.out.println("Created Record Name = " + name + " Countrycode = " + countrycode);
return;}

public Country getCountry(Integer id) {
String SQL = "select * from Student where id = ?";
Country country = jdbcTemplateObject.queryForObject(SQL, 
                    new Object[]{id}, new CountryMapper());
return country;}

public List<Country> listCountries() {
String SQL = "select * from city";
List <Country> countries = jdbcTemplateObject.query(SQL, 
                            new CountryMapper());
return countries;}

public void delete(Integer id){
String SQL = "delete from city where id = ?";
jdbcTemplateObject.update(SQL, id);
System.out.println("Deleted Record with ID = " + id );
return;}

public void update(Integer id, Integer population){
String SQL = "update city set population = ? where id = ?";
jdbcTemplateObject.update(SQL, population, id);
System.out.println("Updated Record with ID = " + id );
 return;
}}

运行主类时出现以下错误

2013 年 12 月 11 日下午 1:06:02 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO:正在刷新 org.springframework.context.support.ClassPathXmlApplicationContext@1c7865b: 启动日期 [2013 年 12 月 11 日星期三 13:06:02 IST];上下文层次的根 2013 年 12 月 11 日下午 1:06:02 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO:从类路径加载 XML bean 定义 资源 [Beans.xml] 2013 年 12 月 11 日下午 1:06:03 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons INFO:预实例化单例 org.springframework.beans.factory.support.DefaultListableBeanFactory@1fcb00e: 定义bean [dataSource,countryJDBCTemplate];工厂根 层次结构 2013 年 12 月 11 日下午 1:06:03 org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName INFO:加载的 JDBC 驱动程序:com.mysql.jdbc.Driver 线程“主”中的异常 org.springframework.beans.factory.NoSuchBeanDefinitionException: 否 名为“CountryJDBCTemplate”的 bean 定义在 org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:570) 在 org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1114) 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:279) 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 在 org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1117) 在 nz.org.tcf.MainApp.main(MainApp.java:20)


MainApp.java如下:

public class MainApp {
public static void main(String[] args) {
   System.out.println("In Main...");
  ApplicationContext context = 
         new ClassPathXmlApplicationContext("Beans.xml");     
  CountryJDBCTemplate CountryJDBCTemplate = 
  (CountryJDBCTemplate)context.getBean("CountryJDBCTemplate");

     System.out.println("------Listing Multiple Records--------" );
  List<Country> Countrys = CountryJDBCTemplate.listCountries();
  for (Country record : Countrys) {
     System.out.print("ID : " + record.getId() );
     System.out.print(", Name : " + record.getName() );

  }

  System.out.println("----Updating Record with ID = 2 -----" );
  CountryJDBCTemplate.update(2, 20);

  System.out.println("----Listing Record with ID = 2 -----" );
  Country Country = CountryJDBCTemplate.getCountry(2);
  System.out.print("ID : " + Country.getId() );
  System.out.print(", Name : " + Country.getName() );
   }
}

【问题讨论】:

    标签: java spring


    【解决方案1】:

    你的 bean 被命名为 countryJDBCTemplate:

    <bean id="countryJDBCTemplate" ...>
    

    但是你正试图访问一个名为CountryJDBCTemplate的bean(注意大写C):

    CountryJDBCTemplate CountryJDBCTemplate = (CountryJDBCTemplate)context.getBean("CountryJDBCTemplate");
    

    尝试使用小写 c 检索 bean:

    CountryJDBCTemplate CountryJDBCTemplate = (CountryJDBCTemplate)context.getBean("countryJDBCTemplate");
    

    【讨论】:

    • 工作就像一个魅力:)
    【解决方案2】:

    在您的 Spring 上下文中,您使用小写 C 命名 bean countryJDBCTemplate,但您尝试使用大写 C 检索名为 CountryJDBCTemplate 的 bean。

    (如果可以注入您的 Spring bean 而不是手动查找它们,那真的会更好。Spring Boot 是一个新项目,它使这变得更容易。)

    【讨论】:

      猜你喜欢
      • 2017-02-18
      • 2015-04-18
      • 1970-01-01
      • 2017-10-08
      • 2021-06-17
      • 2021-04-19
      相关资源
      最近更新 更多