Reputation: 23
I have seen other related questions but was not satisfied with the solution.
I am new to spring and trying an example from http://www.tutorialspoint.com/spring/spring_jdbc_example.htm
I have made my own changes to the above tutorial as per my requirement, like used city,country entity instead of Student.
My Beans.XMl looks like that
<?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>
and CountryJDBCTemplate.java looks like the below :
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;
}}
I get the following error when I run my main class
Dec 11, 2013 1:06:02 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1c7865b: startup date [Wed Dec 11 13:06:02 IST 2013]; root of context hierarchy Dec 11, 2013 1:06:02 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [Beans.xml] Dec 11, 2013 1:06:03 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1fcb00e: defining beans [dataSource,countryJDBCTemplate]; root of factory hierarchy Dec 11, 2013 1:06:03 PM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName INFO: Loaded JDBC driver: com.mysql.jdbc.Driver Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'CountryJDBCTemplate' is defined at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:570) at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1114) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:279) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1117) at nz.org.tcf.MainApp.main(MainApp.java:20)
MainApp.java is as follows:
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() );
}
}
Upvotes: 0
Views: 1623
Reputation: 49602
Your bean is named countryJDBCTemplate
:
<bean id="countryJDBCTemplate" ...>
But you are trying to access a bean named CountryJDBCTemplate
(note the upper case C
):
CountryJDBCTemplate CountryJDBCTemplate = (CountryJDBCTemplate)context.getBean("CountryJDBCTemplate");
Try retrieving the bean with using a lower case c
:
CountryJDBCTemplate CountryJDBCTemplate = (CountryJDBCTemplate)context.getBean("countryJDBCTemplate");
Upvotes: 0
Reputation: 77226
In your Spring context, you're naming the bean countryJDBCTemplate
with a lowercase C, but you're trying to retrieve a bean named CountryJDBCTemplate
with an uppercase C.
(It really is better if at all possible to inject your Spring beans instead of looking them up manually. Spring Boot is a new project that makes this much easier.)
Upvotes: 0