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 所以不能被实例化
Spring 级联属性的更多相关文章
- JAVA Spring JavaBean 引入 JavaBean ( 外部引用, 内部定义, 级联属性 )
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- Spring的引用内部Bean属性和给级联属性
第一个是内部Bean的配置: 首先是要理解其中的原理,再去操作就很简单了,下面老表就给大家说一下自己的观点(有点简单,但是老表我第一次学习的时候看着视频上的代码确实有点懵逼 ...
- spring 配置属性的详细信息
摘要(这篇文章讲的红,蓝说这话节) 字面值 字面值:可用字符串表示的值,能够通过<value>元素标签或value属性进行注入 基本数据类型及其封装类.String等类型都能够採取字面值注 ...
- @Validated和@Valid校验参数、级联属性、List
@Validated和@Valid的区别 在Controller中校验方法参数时,使用@Valid和@Validated并无特殊差异(若不需要分组校验的话): @Valid:标准JSR-303规范的标 ...
- spring 配置属性细节
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/qilixiang012/article/details/28233811 概要(红色为上一篇所讲,蓝 ...
- spring配置属性的两种方式
spring配置属性有两种方式,第一种方式通过context命名空间中的property-placeholder标签 <context:property-placeholder location ...
- Spring中属性文件properties的读取与使用
实际项目中,通常将一些可配置的定制信息放到属性文件中(如数据库连接信息,邮件发送配置信息等),便于统一配置管理.例中将需配置的属性信息放在属性文件/WEB-INF/configInfo.propert ...
- Spring MVC 属性文件读取注入到静态字段
目录(?)[-] servlet-contextxml configproperties 示例属性 ConfigInfo 对应的配置bean 使用 在项目中,有些参数需要配置到属性文件xxx.pr ...
- Spring Boot 属性配置和使用
Spring Boot 属性配置和使用 Spring Boot 允许通过外部配置让你在不同的环境使用同一应用程序的代码,简单说就是可以通过配置文件来注入属性或者修改默认的配置. Spring Boot ...
随机推荐
- js小知识-数组去重
查看zepto源码时,看到它的数组去重写法非常简单.下面是代码 var uq = function(array){ return [].filter.call(array,function(item, ...
- 奇货商城重构——webpack自动化工程
近几年,前端各种框架工具层出不穷,从两三年前还是一个jQuery搞定全站,到之后requirejs/seajs,node,gulp/webpack,Angular/React/Vue,RN/weex的 ...
- 采用OCR识别自动识别财务报表
一. 财务报表有什么作用 财务报表又叫会计报表,包含:资产负债表.损益表.现金流量表三表.财务报表对企业经营状况有重要的参考意义: n 全面系统地揭示企业一定时期的财务状况.经营成果 ...
- 安装node/npm/webpack步骤
nodejs软件的下载地址:https://nodejs.org/en/ 1.只要安装好了nodejs,就自动安装好了npm包. 2.在cmd中通过命令node -version查看是否安装好node ...
- Jmeter常用功能详解
嘻嘻,忙碌的一周,马上就到周四了~明天就是周五了,可以去嗨了! 这几天正式成立了一个微信订阅号,旨在免费帮助需要入门软件测试的小白! 各位走过路过的亲,欢迎订阅哦:扫描二维码即可订阅
- STL中set的用法
set,顾名思义,就是数学上的集合——每个元素最多只出现一次,并且set中的元素已经从小到大排好序. 头文件:#include<set> 常用的函数: begin() 返回set容 ...
- mapper.xml是怎样实现Dao层接口
上午写了一个简单的 从xml读取信息实例化一个Bean对象.下午就开始想mybatis是怎么通过xml文件来实现dao层接口的,一开始想直接用Class.forName(String name)然后调 ...
- 初识SVN-版本控制工具
慕课网安装教程链接:http://www.imooc.com/video/1924
- 简单易学的SSM(Spring+SpringMVC+MyBatis)整合
SSM(Spring+SpringMVC+MyBatis)的整合: 具体执行过程:1.用户在页面向后台发送一个请求 2.请求由DispatcherServlet 前端控制器拦截交给SpringMVC管 ...
- 汇编指令-bic(位清除)、orr(位或)(3)
1. bic (Bit Clear)位清除指令bic指令的格式为:bic{条件}{S} Rd,Rn,operand bic指令将Rn 的值与操作数operand2 的反码按位逻辑"与&q ...