我是Spring和Hibernate概念的新手。 我在简单的弹簧程序中使用集合。我的想法是将作者姓名,地址和ID存储到集合中。这是一个简单的弹簧框架,但我有一个错误。
弹簧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>
主要班级
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());
}
}
作家类
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;
}
}
以下是我得到的错误
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
答案 0 :(得分:3)
bean引用中有空格:
而实际的bean名称没有空格:
此外,Luiggi建议Bean定义需要更正。
请检查一下。
答案 1 :(得分:1)
在Book
课程中,您有List<Author>
,但在 spring-model.xml 文件中,您引用了book1
,book2
和book3
的实例Book
,而不是来自Author
的复制/粘贴过程,并且忘记了最重要的步骤:调整您粘贴的内容。
只是为了展示一个例子:
<bean id="book1" class="com.vishal.Book">
应该是
<bean id="book1" class="com.vishal.Author">
此外,您指的是其他使用
的bean<ref bean="book1 " />
最后应该修剪一个空白区域。它应该是
<ref bean="book1" />
与 spring-model.xml 文件中的其他bookX
引用类似。