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 ...
随机推荐
- Win7怎么把运行添加到Win快捷菜单的右侧、、、
win7怎么把运行添加到Win快捷菜单的右侧... ------------------------------ 右键桌面任务栏--选择属性 ----------------------------- ...
- asp.net mvc 接入最新支付宝支付+退款 alipay-sdk-NET-20170615110549
第1步: https://openhome.alipay.com/developmentDocument.htm 第2步:下载sdk和demo https://docs.open.alipay.com ...
- .net 中常用的正则表达式整理
相信很多伙伴都跟我一样有关于正则表达式的爱和恨,怎么说呢? 因为正则表达式规则繁多且复杂,想一个一个学 全部精通,需要耗费很长时间和精力, 但是我们用的地方并不是很多,所以我觉得这类东西需要做成类似工 ...
- CRF技能词识别过程
最近在用CRF做未登录技能词识别,虽然艰难,但是感觉很爽,效率非常高. (1)数据准备: 选取30000行精语料作为训练数据.每一个br作为一条数据.使用已有的技能词典对数据进行无标注分词. (2)训 ...
- java语言基础(变量和运算符)
java八大基本数据类型: 整型{ int(整型) short(短整型) long(长整型)} 浮点型{ float(浮点型) double(双精度)} 布尔{boolean} ...
- Windows下mysql忘记root密码
1. 首先检查mysql服务是否启动,若已启动则先将其停止服务,可在开始菜单的运行,使用命令: net stop mysql 打开第一个cmd窗口,切换到mysql的bin目录,运行命令: mysql ...
- postgresql如何维护WAL日志/归档日志
WAL日志介绍 wal全称是write ahead log,是postgresql中的online redo log,是为了保证数据库中数据的一致性和事务的完整性.而在PostgreSQL 7中引入的 ...
- Java filter拦截器的使用
1.web.xml配置 <!-- 验证是否登录 拦截功能 --> <filter> <filter-name>isLogin</filter-name> ...
- String.equals()方法
public boolean equals(Object anObject) { if (this == anObject) { return true; } ...
- Java 优先队列
Java PriorityQueue 优先队列是一种重要的数据结构,其利用的是小/大顶堆来实现的. Java中提供了PriorityQueue,PriorityQueue是基于小顶堆实现的无界优先队列 ...