Reputation: 165
I am new to the concepts of Spring and Hibernate. I am using collections in my simple spring program. My idea is to store an author name, address and id into collection. It's a simple spring framework but I am struck with one error.
spring-model.xml
<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-2.5.xsd">
<bean id="book" class="com.vishal.Book">
<property name="author ">
<list>
<ref bean="book1 " />
<ref bean="book2 " />
<ref bean="book3 " />
</list>
</property>
</bean>
<bean id="book1" class="com.vishal.Book">
<property name="name" value="mark twain" />
<property name="address" value="London" />
<property name="id" value="230" />
</bean>
<bean id="book2" class="com.vishal.Book">
<property name="name" value="gutav friedman" />
<property name="address" value="Germany" />
<property name="id" value="231" />
</bean>
<bean id="book3" class="com.vishal.Book">
<property name="name" value=" Erastothe " />
<property name="address" value="spain" />
<property name="id" value="232" />
</bean>
</beans>
main class
package com.vishal;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Springmain {
@SuppressWarnings("resource")
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"spring-model.xml");
Book book = (Book) context.getBean("book");
book.authorName();
}
}
book.java
package com.vishal;
import java.util.List;
public class Book {
private List<Author> author;
public List<Author> getAuthor() {
return author;
}
public void setAuthor(List<Author> author) {
this.author = author;
}
public void authorName() {
for (Author authors : author)
System.out.println(authors.getAddress() + authors.getId()
+ authors.getName());
}
}
author class
package com.vishal;
public class Author {
private String name;
private String address;
private int id;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Following is the error which I a getting
Jun 10, 2013 11:29:36 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@161d36b: startup date [Mon Jun 10 11:29:36 IST 2013]; root of context hierarchy
Jun 10, 2013 11:29:36 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring-model.xml]
Jun 10, 2013 11:29:37 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1b5998f: defining beans [book,book1,book2,book3]; root of factory hierarchy
Jun 10, 2013 11:29:37 AM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1b5998f: defining beans [book,book1,book2,book3]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'book' defined in class path resource [spring-model.xml]: Cannot resolve reference to bean 'book1 ' while setting bean property 'author ' with key [0]; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'book1 ' is defined
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:329)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:107)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveManagedList(BeanDefinitionValueResolver.java:353)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:154)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1393)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1134)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.vishal.Springmain.main(Springmain.java:10)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'book1 ' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:568)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1102)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:278)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:323)
... 17 more
Upvotes: 2
Views: 20858
Reputation: 85781
In your Book
class you have a List<Author>
but in your spring-model.xml file you refer to book1
, book2
and book3
as instances of Book
, not from Author
probably due for copy/paste process and forgetting the most important step: adapt what you pasted.
Just to show an example:
<bean id="book1" class="com.vishal.Book">
It should be
<bean id="book1" class="com.vishal.Author">
Also, you're referring to other beans using
<ref bean="book1 " />
There's a white space in the end that should be trimmed. It should be
<ref bean="book1" />
Similar for other bookX
references in your spring-model.xml file.
Upvotes: 1
Reputation: 528
There is space in bean references:
whereas actual bean name has no space:
Also as Luiggi suggested Bean definition needs to corrected.
Please check this.
Upvotes: 3