Spring 级联属性

Spring 级联属性是当两个bean 关联时  从一个bean 给 另一个bean 赋值

Application xml  配置如下

<bean id="ZhangSan" class="com.myth.springMVC.bean.Person">
 <constructor-arg value="Zhangsan" index="0"></constructor-arg>
 <constructor-arg value="male" index="1"></constructor-arg>
 <!-- 可以用ref属性  建立bean之间的引用 -->
 <constructor-arg ref="Europe" index="2"></constructor-arg>
 <!-- 为级联属性赋值 -->
 <property name="World.plainZone" value="NorthPole"></property>
</bean>

Person 类

public class Person {
 private String name;
 private String sex;
 private World world;

//必须要有getter 和 setter 方法  如果通过Person给World 分类 使用Property 赋值的话
 public void setWorld(World world) {
  this.world = world;
 }
 public World getWorld() {
  return world;
 }

//构造器要跟XML 的构造方法匹配
 public Person(String name, String sex, World world) {
  super();
  this.name = name;
  this.sex = sex;
  this.world = world;
 }


 @Override
 public String toString() {
  return "Person [name=" + name + ", sex=" + sex + ", world=" + world + "]";
 }
}

World 类

package com.myth.springMVC.bean;

public class World {
// 大地
 private String Plain;
// 大地区域
 private String PlainZone;
// 风
 private String Wind;
// 风速
 private float WindSpeed;
 //必须有Getter 和 Setter 方法  让world  获取PlainZone 属性
 public void setPlainZone(String plainZone) {
  this.PlainZone = plainZone;
 }
 
 
 public String getPlainZone() {
  return PlainZone;
 }

 public World(String plain, String plainZone, String wind) {
  super();
  Plain = plain;
  PlainZone = plainZone;
  Wind = wind; 
 }

 public World(String plain, String wind, float windSpeed) {
  super();
  Plain = plain;
  Wind = wind;
  WindSpeed = windSpeed;
 }

 @Override
 public String toString() {
  return "WorldBean [Plain=" + Plain + ", PlainZone=" + PlainZone + ", Wind=" + Wind + ", WindSpeed=" + WindSpeed
    + "]";
 }
 
}

 大部分的错误都是因为自己的马虎所导致的,所以在写配置文件的时候需要小心注意

遇到的错误:1) Bean property 'Europe' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

可以看到Europe 是一个读不到的Bean  Europe 是World 的一个实例化 ,  这里应该使用World   而不是Europe

八月 10, 2017 6:30:54 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@300ffa5d: startup date [Thu Aug 10 18:30:54 CST 2017]; root of context hierarchy
八月 10, 2017 6:30:54 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
八月 10, 2017 6:30:55 下午 org.springframework.context.support.ClassPathXmlApplicationContext refresh
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'ZhangSan' defined in class path resource [applicationContext.xml]: Error setting property values;
nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'Europe.plainZone' of bean class [com.myth.springMVC.bean.Person]:
Nested property in path 'Europe.plainZone' does not exist; nested exception is org.springframework.beans.NotReadablePropertyException:
Invalid property 'Europe' of bean class [com.myth.springMVC.bean.Person]: Bean property 'Europe' is not readable or has an invalid getter method:
Does the return type of the getter match the parameter type of the setter?
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ZhangSan' defined in class path resource [applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'Europe.plainZone' of bean class [com.myth.springMVC.bean.Person]: Nested property in path 'Europe.plainZone' does not exist; nested exception is org.springframework.beans.NotReadablePropertyException: Invalid property 'Europe' of bean class [com.myth.springMVC.bean.Person]: Bean property 'Europe' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1568)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1276)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
 at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
 at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
 at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
 at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
 at com.myth.springMVC.test.HelloMain.main(HelloMain.java:21)
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'Europe.plainZone' of bean class [com.myth.springMVC.bean.Person]: Nested property in path 'Europe.plainZone' does not exist; nested exception is org.springframework.beans.NotReadablePropertyException: Invalid property 'Europe' of bean class [com.myth.springMVC.bean.Person]: Bean property 'Europe' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
 at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:273)
 at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:95)
 at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:75)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1564)
 ... 13 more
Caused by: org.springframework.beans.NotReadablePropertyException: Invalid property 'Europe' of bean class [com.myth.springMVC.bean.Person]: Bean property 'Europe' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
 at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:633)
 at org.springframework.beans.AbstractNestablePropertyAccessor.getNestedPropertyAccessor(AbstractNestablePropertyAccessor.java:850)
 at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyAccessorForPropertyPath(AbstractNestablePropertyAccessor.java:827)
 at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:270)
 ... 16 more

2)Bean 'ZhangSan'; nested exception is org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: <property> element for property 'Europe.plainZone' must specify a ref or value

构造器的方法没有匹配,可以看构造器 的方法

八月 10, 2017 5:52:24 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@300ffa5d: startup date [Thu Aug 10 17:52:24 CST 2017]; root of context hierarchy
八月 10, 2017 5:52:24 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unexpected failure during bean definition parsing
Offending resource: class path resource [applicationContext.xml]
Bean 'ZhangSan'; nested exception is org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: <property> element for property 'Europe.plainZone' must specify a ref or value
Offending resource: class path resource [applicationContext.xml]
Bean 'ZhangSan'
 -> Property 'Europe.plainZone'
 at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:70)
 at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
 at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.error(BeanDefinitionParserDelegate.java:308)
 at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseBeanDefinitionElement(BeanDefinitionParserDelegate.java:562)
 at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseBeanDefinitionElement(BeanDefinitionParserDelegate.java:460)
 at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseBeanDefinitionElement(BeanDefinitionParserDelegate.java:429)
 at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.processBeanDefinition(DefaultBeanDefinitionDocumentReader.java:299)
 at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseDefaultElement(DefaultBeanDefinitionDocumentReader.java:190)
 at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:169)
 at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:142)
 at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:94)
 at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:508)
 at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:392)
 at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)
 at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
 at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)
 at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)
 at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
 at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:252)
 at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
 at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
 at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
 at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:614)
 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:515)
 at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
 at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
 at com.myth.springMVC.test.HelloMain.main(HelloMain.java:21)
Caused by: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: <property> element for property 'Europe.plainZone' must specify a ref or value
Offending resource: class path resource [applicationContext.xml]
Bean 'ZhangSan'
 -> Property 'Europe.plainZone'
 at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:70)
 at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
 at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:80)
 at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.error(BeanDefinitionParserDelegate.java:301)
 at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parsePropertyValue(BeanDefinitionParserDelegate.java:1000)
 at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parsePropertyElement(BeanDefinitionParserDelegate.java:894)
 at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parsePropertyElements(BeanDefinitionParserDelegate.java:755)
 at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseBeanDefinitionElement(BeanDefinitionParserDelegate.java:547)
 ... 23 more

3) Error creating bean with name 'Xian': Bean definition is abstract

由于Bean被设置为abstract  所以不能被实例化

原文地址:https://www.cnblogs.com/mythdoraemon/p/7341150.html