Spring - 如何单独继承属性

时间:2016-01-12 12:05:09

标签: java spring inheritance

<?xml version="1.0" encoding="UTF-8"?>

<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-3.0.xsd">

<bean id="ParentBean" class="com.sample.spring.Parent"
    init-method="init" destroy-method="destroy">
    <property name="message" value="Hello World!" />
</bean>

<bean id="ChildBean" class="com.sample.spring.Child"
    parent="ParentBean">
    <property name="message1" value="Good Morning"></property>
</bean>

<bean class="com.sample.spring.PostHelloWorld" />

在这里,我试图继承属性&#34; message&#34;从ParentBean到ChildBean,但在运行时它会抛出异常,如下所示,

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'ChildBean' defined in class path resource [Beans.xml]: 
Invocation of init method failed; nested exception is      org.springframework.beans.factory.support.BeanDefinitionValidationException: 
Couldn't find an init method named 'init' on bean with name 'ChildBean'

从上面我可以理解,ChildBean也继承了初始化和销毁​​方法。

问题,

无论如何我可以让它只继承属性吗?

0 个答案:

没有答案