Spring和Abstract类 - 在抽象类中注入属性 [英] Spring and Abstract class - injecting properties in abstract classes

查看:2548
本文介绍了Spring和Abstract类 - 在抽象类中注入属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个抽象基类,其中包含一个名为mailserver的属性,我希望从spring ioc容器中注入该属性。但是,当我运行抽象类的具体实现时,我得到了一个null的mailserver属性。

I have an abstract base class with a property called "mailserver" which I wish to inject from the spring ioc container. However when I run the concreted implementations of the abstract class I get a null for the mailserver property.

这样做的正确方法是什么?你有没有试过这样做并取得成功?请分享。

What is the correct way of doing this? Have you tried doing someting like this and been successful? Please share.

关注khush。

推荐答案

标记抽象基础通过使用 abstract 属性将类定义为抽象,并在具体的类定义中,将 parent 属性作为名称抽象类的bean名称

Mark the abstract base class definition as abstract by using the abstract attribute , and in the concrete class definition , make the parent attribute be the name of the abstract class 's bean name

这样的东西:

<bean id="abstractBaseClass" abstract="true" class="pacakge1.AbstractBaseClass">
  <property name="mailserver" value="DefaultMailServer"/>
</bean>

<bean id="concreteClass1" class="pacakge1.ConcreteClass1" parent="abstractBaseClass">     
  <!--Override the value of the abstract based class if necessary-->
  <property name="mailserver" value="AnotherMailServer"/>
</bean>

这篇关于Spring和Abstract类 - 在抽象类中注入属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆