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  所以不能被实例化

Spring 级联属性的更多相关文章

  1. JAVA Spring JavaBean 引入 JavaBean ( 外部引用, 内部定义, 级联属性 )

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  2. Spring的引用内部Bean属性和给级联属性

    第一个是内部Bean的配置:               首先是要理解其中的原理,再去操作就很简单了,下面老表就给大家说一下自己的观点(有点简单,但是老表我第一次学习的时候看着视频上的代码确实有点懵逼 ...

  3. spring 配置属性的详细信息

    摘要(这篇文章讲的红,蓝说这话节) 字面值 字面值:可用字符串表示的值,能够通过<value>元素标签或value属性进行注入 基本数据类型及其封装类.String等类型都能够採取字面值注 ...

  4. @Validated和@Valid校验参数、级联属性、List

    @Validated和@Valid的区别 在Controller中校验方法参数时,使用@Valid和@Validated并无特殊差异(若不需要分组校验的话): @Valid:标准JSR-303规范的标 ...

  5. spring 配置属性细节

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/qilixiang012/article/details/28233811 概要(红色为上一篇所讲,蓝 ...

  6. spring配置属性的两种方式

    spring配置属性有两种方式,第一种方式通过context命名空间中的property-placeholder标签 <context:property-placeholder location ...

  7. Spring中属性文件properties的读取与使用

    实际项目中,通常将一些可配置的定制信息放到属性文件中(如数据库连接信息,邮件发送配置信息等),便于统一配置管理.例中将需配置的属性信息放在属性文件/WEB-INF/configInfo.propert ...

  8. Spring MVC 属性文件读取注入到静态字段

    目录(?)[-] servlet-contextxml configproperties 示例属性 ConfigInfo 对应的配置bean 使用   在项目中,有些参数需要配置到属性文件xxx.pr ...

  9. Spring Boot 属性配置和使用

    Spring Boot 属性配置和使用 Spring Boot 允许通过外部配置让你在不同的环境使用同一应用程序的代码,简单说就是可以通过配置文件来注入属性或者修改默认的配置. Spring Boot ...

随机推荐

  1. APUE 2 - 进程组(process group) 会话(session) job

    进程组(process group) 进程组顾名思义是指一个或多个进程的集合.他们通常与同一个job(可以从同一个终端接收信号)相关联.每个进程组拥有一个唯一的Process Group Id.可以使 ...

  2. 新版TP-Link无线路由器怎么设置

    TP-Link路由器的设置和无线WIFI的设置.. -------------- 一.准备工作: 1.首先是路由器的安装,将路由器电源接上,并通电,然后网线的连接.如果是拨号上网用户,请将猫引出的网线 ...

  3. Ta-lib函数功能列表

    import tkinter as tk from tkinter import ttk import matplotlib.pyplot as plt import numpy as np impo ...

  4. STL中set的用法

    set,顾名思义,就是数学上的集合——每个元素最多只出现一次,并且set中的元素已经从小到大排好序. 头文件:#include<set> 常用的函数: begin()     返回set容 ...

  5. 扩展Python模块系列(四)----引用计数问题的处理

    承接上文,发现在使用Python C/C++ API扩展Python模块时,总要在各种各样的地方考虑到引用计数问题,稍不留神可能会导致扩展的模块存在内存泄漏.引用计数问题是C语言扩展Python模块最 ...

  6. Django数据库操作(增删改查)

    Django数据库操作(增删改查) 创建数据库中的一个表 class Business(models.Model): #自动创建ID列 caption = models.CharField(max_l ...

  7. Python初学——pickle & set

    pickle 存放数据 保存和提取python运算完的结果 首先import pickle模块 定义一个字典: a_dict={'da':111,2:[23,1,4],'23':{1:2,'d':'s ...

  8. SpringMVC + Mybatis bug调试 SQL正确,查数据库却返回NULL

    今天碰到个bug,有点意思 背景是SpringMVC + Mybatis的一个项目,mapper文件里写了一条sql 大概相当于 select a from tableA where b = &quo ...

  9. Vue相关(过渡动画)

    Vue 过渡 && 动画 一.CSS过渡 1.transition标签可以用来封装需要过渡的元素,添加entering/leaving 过渡, 条件是: (1)使用条件渲染语句 v-i ...

  10. 【完整资料】TC358779XBG:HDMI转MIPI DSI芯片方案

    TC358779XBG是一颗HDMI1.4转MIPI  DSI带缩放功能的芯片,分辨率1920*1080,封装BGA80.通信方式:IIC,电源3.3/1.8/2.2,应用领域:平板,广告机,VR,显 ...