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 ...
随机推荐
- 【Spring】XML配置整合Mybatis
注意:项目开发使用了mybatis的mapper代理! 首先是mybatis自己的配置文件,被spring整合之后,只有typeAliases存在了,其他都整合在了spring-mybatis.xml ...
- Git 初学
记录git与远成仓库建立连接日志 gitbub上创建远程仓库 https://github.com/ 创建登陆账号进入主页 , 选择右上角的加号 新建rep Repository name 为你创建的 ...
- Windows10-安装笔记
Windows10-安装笔记... ------------------------------ ---------------------------------------- 加载镜像.加载完后. ...
- Servlet知识点
如果请求采用Get方式,则重写doGet()方法,如果请求采用Post方式,则重写doPost()方法. 下面是重写doGet()方法的servlet例子. servlet继承如下类: 整体结构: 在 ...
- HK2使用详解
HK2介绍 组件 生命周期 定义服务 注入服务 HK2介绍 HK2是一个轻量级动态依赖注入框架,它是JSR-330的实现. 组件 在HK2组件模型中,一个组件的功能是通过服务接口-服务实现的模式声明的 ...
- FileDetector-基于java开发的照片整理工具
1. 项目背景 开发这个功能的主要原因如下: 1. 大学期间拍摄了约50G的照片,照片很多 2. 存放不规范,导致同一张照片出现在不同的文件夹内,可读性差,无法形成记忆线. 3. 重复存放过多,很多照 ...
- Spring Cloud在国内中小型公司能用起来吗?
今天吃完饭休息的时候瞎逛知乎,突然看到这个一个问题Spring Cloud在国内中小型公司能用起来吗?,吸引了我的注意.仔细的看了题主的问题,发现这是一个好问题,题主经过了一番思考,并且用图形全面的将 ...
- box-sizing怪异盒子模型在移动端应用
盒子模型不必多少,公认的盒子模型 总宽度=width + padding(padding-left,padding-right) + border(border-left,border-right) ...
- 用CAS方案解决高并发一致性问题
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt395 缘起:在高并发的分布式环境下,对于数据的查询与修改容易引发一致性问题, ...
- 解决MVC模式文件下载附件中文名称乱码
解决如下: 进行url编码:Server.UrlPathEncode(file.AttachmentName) return File(file.TempWorkPath, CommonTools.G ...