我见过其他相关问题,但对解决方案不满意。
我刚开始尝试从http://www.tutorialspoint.com/spring/spring_jdbc_example.htm
尝试示例我已根据我的要求对上述教程进行了自己的更改,例如二手城市,国家/地区 实体而不是学生。
My 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: 启动日期[Wed Dec 11 13:06:02 IST 2013];上下文层次结构的根 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信息:预先实例化单例 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 线程“main”中的异常 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() );
}
}
答案 0 :(得分:0)
在Spring上下文中,您使用小写C命名bean countryJDBCTemplate
,但是您尝试使用大写C检索名为CountryJDBCTemplate
的bean。
(如果可能的话,最好是注入你的Spring bean而不是手动查找它们.Spring Boot是一个新项目,可以让这更容易。)
答案 1 :(得分:0)
您的bean名为countryJDBCTemplate
:
<bean id="countryJDBCTemplate" ...>
但是您正在尝试访问名为CountryJDBCTemplate
的bean(请注意大写C
):
CountryJDBCTemplate CountryJDBCTemplate = (CountryJDBCTemplate)context.getBean("CountryJDBCTemplate");
尝试使用小写c
CountryJDBCTemplate CountryJDBCTemplate = (CountryJDBCTemplate)context.getBean("countryJDBCTemplate");