Spring bean配置继承
- package com.yiibai.common;
- public class Customer {
- private int type;
- private String action;
- private String Country;
- //...
- }
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
- <bean id="BaseCustomerMalaysia" class="com.yiibai.common.Customer">
- <property name="country" value="Malaysia" />
- </bean>
- <bean id="CustomerBean" parent="BaseCustomerMalaysia">
- <property name="action" value="buy" />
- <property name="type" value="1" />
- </bean>
- </beans>
执行它
- package com.yiibai.common;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class App
- {
- public static void main( String[] args )
- {
- ApplicationContext context =
- new ClassPathXmlApplicationContext("applicationContext.xml");
- Customer cust = (Customer)context.getBean("CustomerBean");
- System.out.println(cust);
- }
- }
输出结果
- Customer [type=1, action=buy, Country=Malaysia]
- Customer cust = (Customer)context.getBean("BaseCustomerMalaysia");
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
- <bean id="BaseCustomerMalaysia" class="com.yiibai.common.Customer" abstract="true">
- <property name="country" value="Malaysia" />
- </bean>
- <bean id="CustomerBean" parent="BaseCustomerMalaysia">
- <property name="action" value="buy" />
- <property name="type" value="1" />
- </bean>
- </beans>
- Customer cust = (Customer)context.getBean("BaseCustomerMalaysia");
- org.springframework.beans.factory.BeanIsAbstractException:
- Error creating bean with name 'BaseCustomerMalaysia':
- Bean definition is abstract
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
- <bean id="BaseCustomerMalaysia" abstract="true">
- <property name="country" value="Malaysia" />
- </bean>
- <bean id="CustomerBean" parent="BaseCustomerMalaysia"
- class="com.yiibai.common.Customer">
- <property name="action" value="buy" />
- <property name="type" value="1" />
- </bean>
- </beans>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
- <bean id="BaseCustomerMalaysia" class="com.yiibai.common.Customer" abstract="true">
- <property name="country" value="Malaysia" />
- </bean>
- <bean id="CustomerBean" parent="BaseCustomerMalaysia">
- <property name="country" value="Japan" />
- <property name="action" value="buy" />
- <property name="type" value="1" />
- </bean>
- </beans>
- Customer [Country=Japan, action=buy, type=1]
总结
Spring bean配置继承的更多相关文章
- Spring学习(九)-----Spring bean配置继承
在 Spring,继承是用为支持bean设置一个 bean 来分享共同的值,属性或配置. 一个子 bean 或继承的bean可以继承其父 bean 的配置,属性和一些属性.另外,子 Bean 允许覆盖 ...
- spring Bean配置的三种形式
Spring Bean配置有以下三种形式: 传统的xml配置 Spring 2.5 以后新增注解配置 Spring3.0以后新增JavaConfig 1. 传统的xml配置 <?xml vers ...
- [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring Bean配置
Spring 是什么 •Spring 为简化企业级应用开发而生. 使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能. •Spring 是一个 IOC(DI) 和 ...
- Spring Bean 定义继承
本例子源于:W3CSchool,在此作记录 bean 定义可以包含很多的配置信息,包括构造函数的参数,属性值,容器的具体信息例如初始化方法,静态工厂方法名,等等. 子 bean 的定义继承父定义的配置 ...
- spring—Bean配置
Spring是一个开源的框架,其目标是简化java的开发.为了降低Java开发的复杂性,Spring有如下的特性: >> 基于POJO的轻量级和最小侵入性编程 >> 通过依赖注 ...
- spring学习五:Spring Bean 定义继承
Bean 定义继承 bean 定义可以包含很多的配置信息,包括构造函数的参数,属性值,容器的具体信息例如初始化方法,静态工厂方法名,等等. 子 bean 的定义继承父定义的配置数据.子定义可以根据需要 ...
- [原创]java WEB学习笔记101:Spring学习---Spring Bean配置:IOC容器中bean的声明周期,Bean 后置处理器
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- [原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
随机推荐
- Java Eclipse 配置
1.清除多余记录 最近用eclipse打包jar的时候,需要指定一个main函数.需要先运行一下main函数,eclipse的Runnable JAR File Specification 下的Lau ...
- 1、CentOS 6 安装GitLab
1.安装和配置必需的依赖项 在CentOS上将系统防火墙打开HTTP和SSH访问. sudo yum install -y curl policycoreutils-python openssh-se ...
- 转:google测试分享-SET和TE
原文: http://blog.sina.com.cn/s/blog_6cf812be0102vbnb.html 前端时间看了google测试之道,收获了一些,在此总结下并打算写一个系列blog,顺 ...
- Debian系网络配置 /etc/network/interfaces
说Debian系的网卡配置跟Redhat系很不一样,Redhat是放在/etc/sysconfig/network-scripts目录下面的一大堆文件里面,要修改?你一个一个文件来过吧.Debian系 ...
- Java web项目中新建maven项目出现的问题
1.首先新建maven项目,新建Maven时出现了版本问题,报错 第一个错误:jdk版本与project facets不匹配(大概是这样,忘记截图了),那么解决办法是: 在项目右击--->Pro ...
- POJ 2349 Arctic Network(最小生成树+求第k大边)
题目链接:http://poj.org/problem?id=2349 题目大意:有n个前哨,和s个卫星通讯装置,任何两个装了卫星通讯装置的前哨都可以通过卫星进行通信,而不管他们的位置. 否则,只有两 ...
- 今日头条、Face++开发岗面经
今日头条.Face++开发岗面经 [头条] 两个栈实现一个队列.怎么优化 数组每一个元素找出数组右边第一个大于自己的数 实现LRU TCP四次握手 滑动窗口.窗口大小 线程与进程区别 什么是线程安全 ...
- scrapy保存csv文件有空行的解决方案
比如现在我有一个名为test的爬虫,运行爬虫后将结果保存到test.csv文件 默认情况下,我执行scrapy crawl test -o test.csv ,得到的结果可能就是下面这种情况,每两行中 ...
- springMVC返回modelmap跟new hashMap的区别
今天遇到了个坑. 在springboot中 平时写接口,newHashMap,@ResponseBody 返回json对象,没什么问题 @RequestMapping("url") ...
- onvif 开发中的一些重要函数介绍
➤soap结构中count(soap->count)成员 soap结构中count(soap->count)成员记录的是http协议中Content-Length的数值. ➤keep_al ...