继承类如何在spring bean继承注入里注入

Spring高级应用之注入各类集合 - ImportNew
先定义一个测试类,由于本文将要介绍注入各种集合时如何配置,故这个类包含各种集合,类名和属性名不好取,没有特殊含义:
public class Test {
private List&String& listT
private Map&String, Object& mapT
private Set setT
private String[] arrayT
private Properties propertiesT
//下面是各个属性的setter,此处省略
Test类中,分别定义了List,Map,Set,Array等5种集合类型的属性,下面在Spring配置文件中,分别为这些类型的属性注入值:
&bean id=&test& class=&com.abc.Test&&
&!-- List类型的属性 --&
&property name=&listTest&&
&!-- 为list类型的属性注入值时,使用&list&元素 --&
&!-- 只要类型满足,每个&value&,&ref&,&bean&都可以配置一个list的元素 --&
&value&ListA&/value&
&value&ListB&/value&
&value&ListC&/value&
&/property&
&!-- Map类型的属性 --&
&property name=&mapTest&&
&!-- 为map类型的属性注入值时,使用&map&元素 --&
&!-- 每一个&entry&都为&map&配置一个K-V对,同样, --&
&entry key=&key1& value=&value1& /&
&!-- 下面这个value指向了在此Spring文件中定义的另一个叫object的Bean --&
&entry key=&key2& value-ref=&object& /&
&/property&
&!-- Set类型的属性 --&
&property name=&setTest&&
&!-- 为set类型的属性注入值时,使用&set&元素 --&
&!-- 只要类型满足,每个&value&,&ref&,&bean&都可以配置一个list的元素 --&
&value&SetA&/value&
&!-- 下面是一个嵌套Bean的定义。关于什么是嵌套Bean,请看:http://my.oschina.net/itblog/blog/204378 --&
&bean class=&com.abc.OtherBean1& /&
&!-- 下面引用了此Spring文件中定义的另一个Bean --&
&ref local=&com.abc.OtherBean2& /&
&/property&
&!-- Properties类型的属性 --&
&property name=&propertiesTest&&
&!-- 每个&prop&元素指定一个属性项,其中key指定属性名 --&
&prop key=&prop1&&value1&/prop&
&prop key=&prop2&&value2&/prop&
&/property&
&!-- 数组类型的属性 --&
&property name=&arrayTest&&
&!-- 为数组类型的属性注入值时,使用&list&元素 --&
&!-- 只要类型满足,每个&value&,&ref&,&bean&都可以配置一个list的元素 --&
&value&Array1&/value&
&bean class=&com.abc.OtherBean1& /&
&ref local=&com.abc.OtherBean2& /&
&/property&
由于集合元素有可以是基本类型值、引用容器中其他Bean、嵌套Bean或集合属性等,因此&list&、&set&、和&key&元素又可以接受如下的子元素:
value:指定集合元素是基本数据类型值或字符串类型值
ref:指定集合元素是容器中另一个Bean实例
bean:指定集合元素是一个嵌套Bean
list、set、map及props:指定集合元素值又是集合
&props&元素适用于配置Properties类型的属性,Properties类型时一种特殊的类型,其key和value都只能是字符串,故Spring配置Properties类型的属性都比较简单:每个属性项只要分别给出属性名和属性值就足够了。
当使用&map&元素配置Map类型属性时稍显复杂,因为Map类型的元素由多个&entry&组成,每个&entry&又需要配置key和value两个属性。其中&entry&属性支持以下几个属性:
key:当key是基本类型或字符串时使用
key-ref:当key类型是容器中定义的另一个Bean时使用
value:当value是基本类型或字符串时使用
value-ref:当value是容器中定义的另一个Bean时使用
Spring还提供了一个简化语法来支持Properties形参的setter方法,例如我们可以使用如下配置片段来配置Properties类型的属性:
&property name=&propertiesTest&&
key1=value1
key2=value2
&/property&
虽然这种配置方式更简单,但是它也有一个缺点:属性名、属性值都只能是英文或数字,不能包含中文字符
从Spring2开始,Spring IoC容器支持集合的合并:子Bean中的集合属性可以从其父Bean的集合属性继承而来,同时,子Bean中的属性会覆盖父Bean中属性名相同的值。也就是说,子Bean的集合属性的最终值是父Bean、子Bean合并后的最终结果,下面是一个示例:
&!-- 将parent类型定义成抽象类 --&
&bean id=&parent& abstract=&true& class=&com.abc.Parent&&
&!-- 定义Properties类型的集合属性 --&
&property name=&websites&&
&prop key=&baidu&&www.baidu.com&/props&
&prop key=&google&&www.google.com.hk&/prop&
&/property&
&bean id=&child& parent=&parent&&
&property name=&websites&&
&!-- 注意,这里子Bean在集合元素上加了属性&merge=true& --&
&props merge=&true&&
&prop key=&google&&www.google.com&/prop&
&prop key=&tencent&&www.qq.com&/prop&
&/property&
通过上面的配置后,child中的属性将变为:
baidu=www.baidu.com
google=www.google.com
tencent=www.qq.com
关于ImportNew
ImportNew 专注于 Java 技术分享。于日 11:11正式上线。是的,这是一个很特别的时刻 :)
ImportNew 由两个 Java 关键字 import 和 new 组成,意指:Java 开发者学习新知识的网站。 import 可认为是学习和吸收, new 则可认为是新知识、新技术圈子和新朋友……
新浪微博:
推荐微信号
反馈建议:ImportNew.
广告与商务合作QQ:
– 好的话题、有启发的回复、值得信赖的圈子
– 写了文章?看干货?去头条!
– 为IT单身男女服务的征婚传播平台
– 优秀的工具资源导航
– 活跃 & 专业的翻译小组
– 国内外的精选博客文章
– UI,网页,交互和用户体验
– JavaScript, HTML5, CSS
– 专注Android技术分享
– 专注iOS技术分享
– 专注Java技术分享
– 专注Python技术分享
& 2018 ImportNew没有更多推荐了,
不良信息举报
举报内容:
Spring注入实现接口的某个类
举报原因:
原文地址:
原因补充:
最多只允许输入30个字
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!博客分类:
1、写POJO类Car.java:
package com.xfc.test.
public class Car {
public int getHorsepower() {
public void setHorsepower(int horsepower) {
this.horsepower =
public String getColor() {
public void setColor(String color) {
this.color =
2.写bean类MyEditor.java:
package com.xfc.test.
* 对应spring配置文件中的bean
* @author xfc
public class MyEditor {
public String getName() {
public void setName(String name) {
this.name =
public Car getCar() {
public void setCar(Car car) {
this.car =
3.写配置文件bean.xml:
&?xml version="1.0" encoding="GB2312"?&
&!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"&
&beans default-autowire="byName" default-lazy-init="true"&
使用HessianProxyFactoryBean 连接远程Hessian服务--&
&bean id="myEditor" class="com.xfc.test.propertyedit.MyEditor"&
&property name="name" value="xfc"/&
&property name="car" value="1,shanghai"/&
4.写字符串转特定car对象类CarEditor.java:
package com.xfc.test.
import java.beans.PropertyEditorS
import org.springframework.util.StringU
* 将字符串转换成car对象的类
* @author xfc
public class CarEditor extends PropertyEditorSupport {
public void setAsText(String str) throws IllegalArgumentException {
if (str == null && "".equals(str)) {
throw new IllegalArgumentException("string is null");
String[] s = StringUtils.commaDelimitedListToStringArray(str);
if (s.length & 2) {
throw new IllegalArgumentException("string length is not right");
Car c = new Car();
c.setHorsepower(Integer.parseInt(s[0]));
c.setColor(s[1]);
setValue(c);
5.写测试类Test.java:
package com.xfc.test.
import org.springframework.context.ApplicationC
import org.springframework.context.support.ClassPathXmlApplicationC
public class Test {
* @param args
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
MyEditor me=(MyEditor)ac.getBean("myEditor");
System.out.println(me);
System.out.println(me.getCar().getHorsepower());
最大的疑问:为什么在配置文件中没指定字符串转特定对象的bean(如上CarEditor.java),Spring能自动调用该类呢?
spring可以自动的去检测PropertyEditor,其检测的规则是PropertyClassType-》
ProertyClassTypeEditor,比如说,bean的某个属性类型为Family,那么如果在classpath里存在一个类名字叫
FamilyEditor,那么spring会将这个editor认为是Family类的edtior,以后当给类型为Family的属性赋值时,都将使
用这个FamilyEditor。
具体实现可以看TypeConverter类的委托类TypeConverterDelegate的findDefaultEditor方法(BeanUtils.findEditorByConvention(requiredType)),当然,推荐是用
户自己进行配置,否则spring会在整个classpath里去搜寻,对性能有一定影响。
浏览: 8494 次
来自: 杭州
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'在 Spring,继承是用为支持bean设置一个 bean 来分享共同的值,属性或配置。
一个子 bean 或继承的bean可以继承其父 bean 的配置,属性和一些属性。另外,子 Bean 允许覆盖继承的值。
请参见下面的完整的例子来告诉你如何配置 bean 继承在 Spring 中工作。
package com.yiibai.
public class Customer {
private String C
Bean配置文件
&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 id="CustomerBean" parent="BaseCustomerMalaysia"&
&property name="action" value="buy" /&
&property name="type" value="1" /&
以上就是“BaseCustomerMalaysia”&Bean中含有的 country&属性的值,而“CustomerBean”&Bean&继承其父('BaseCustomerMalaysia')这个值。
package com.yiibai.
import org.springframework.context.ApplicationC
import org.springframework.context.support.ClassPathXmlApplicationC
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]
CustomerBean&Bean 只从它的父(“BaseCustomerMalaysia”)继承&country&属性。
在上面的例子中,'BaseCustomerMalaysia' 仍然能够实例化,例如,
Customer cust = (Customer)context.getBean("BaseCustomerMalaysia");
如果你要让这个&bean&作为一个基础模板,不允许别人来实例化它,可以在一个&bean&元素中添加一个“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" class="com.yiibai.common.Customer" abstract="true"&
&property name="country" value="Malaysia" /&
&bean id="CustomerBean" parent="BaseCustomerMalaysia"&
&property name="action" value="buy" /&
&property name="type" value="1" /&
现在,“BaseCustomerMalaysia'&Bean是一个纯粹的模板,因为Bean只能继承它,如果试图实例化它,你会遇到以下错误消息。
Customer cust = (Customer)context.getBean("BaseCustomerMalaysia");
org.springframework.beans.factory.BeanIsAbstractException:
Error creating bean with name 'BaseCustomerMalaysia':
Bean definition is abstract
纯继承模板
其实,父 bean 是不需要定义类的属性,很多时候,你可能只需要一个共同的属性共享。这里的是一个例子
&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 id="CustomerBean" parent="BaseCustomerMalaysia"
class="com.yiibai.common.Customer"&
&property name="action" value="buy" /&
&property name="type" value="1" /&
在这种情况下,“BaseCustomerMalaysia' Bean 是一个纯粹的模板,只分享其 ”country“属性。
但是,仍然可以指定的子bean的新值覆盖继承的值。让我们来看看这个例子
&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 id="CustomerBean" parent="BaseCustomerMalaysia"&
&property name="country" value="Japan" /&
&property name="action" value="buy" /&
&property name="type" value="1" /&
在“CustomerBean”&Bean只是覆盖父(“BaseCustomerMalaysia”)country&属性,从&‘Malaysia’&修改为&‘Japan’.
Customer [Country=Japan, action=buy, type=1]
Spring&bean配置继承是为了避免多个Bean有重复共同的值或配置是非常有用的。
下载代码 –
易百教程移动端:请扫描本页面底部(右侧)二维码并关注微信公众号,回复:"教程" 选择相关教程阅读或直接访问:http://m.yiibai.com 。
上一篇:下一篇:
加QQ群啦,易百教程官方技术学习群
注意:建议每个人选自己的技术方向加群,同一个QQ最多限加3个群。
Java技术群:
(人数:2000,等级:LV5,免费:否)
MySQL/SQL群:
(人数:2000,等级:LV5,免费:否)
大数据开发群:
(人数:2000,等级:LV5,免费:否)
Python技术群:
(人数:2000,等级:LV5,免费:否)
人工智能深度学习:
(人数:2000,等级:LV5,免费:否)
测试工程师(新群):
(人数:1000,等级:LV1,免费:是)
前端技术群(新群):
(人数:1000,等级:LV1,免费:是)
C/C++技术(新群):
(人数:1000,等级:LV1,免费:是)
Node.js技术(新群):
(人数:1000,等级:LV1,免费:是)
PostgreSQL数据库(新群):
(人数:1000,等级:LV1,免费:否)
Linux技术:
(人数:2000,等级:LV5,免费:否)
PHP开发者:
(人数:2000,等级:LV5,免费:是)
Oracle数据库:
(人数:2000,等级:LV5,免费:是)
C#/ASP.Net开发者:
(人数:2000,等级:LV5,免费:是)
数据分析师:
(人数:1000,等级:LV1,免费:是)R语言,Matlab语言等技术请教spring高手,spring注解继承疑问_spring吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:14,428贴子:
请教spring高手,spring注解继承疑问收藏
有一个服务类CommonService,配置了两个bean,第一个commonService1,第二个commonService2,再建提个业务服务类BusinessService继承CommonService,通过注解@Service注入,这时在BusinessService中调用CommonService是bean commonService1,还是commonService2,求高手指教
登录百度帐号

我要回帖

更多关于 spring 抽象类注入 的文章

 

随机推荐