关于spring中bean的init-method/destroy和继承之间的问题

modestaow 2017-07-14 03:50:59
<?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="helloworld" class="InheritanceBean.HelloWorld"
init-method="init" destroy-method="destroy">
<property name="message1" value="hello world"></property>
<property name="message2" value="hello world2"></property>
</bean>
<bean id="hellochina" class="InheritanceBean.HelloChina" parent="helloworld"
init-method="init" destroy-method="destroy">
<!-- <property name="message1" value="Hello China!"></property> -->
<property name="message3" value="Greate China!"></property>
</bean>
</beans>


不知道为什么,去掉了init-method和destroy-method后程序就不会报错了


package InheritanceBean;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {

public static void main(String[] args) {
// TODO Auto-generated method stub
AbstractApplicationContext context=new ClassPathXmlApplicationContext("InheritanceBean/MyXml.xml");

HelloWorld objA=(HelloWorld)context.getBean("helloworld");
objA.getMessage1();
objA.getMessage2();

HelloChina objB=(HelloChina)context.getBean("hellochina");

objB.getMessage1();
objB.getMessage2();
objB.getMessage3();

context.registerShutdownHook();

}

}
...全文
190 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
110成成 2017-07-14
  • 打赏
  • 举报
回复
报什么错,另外init方法和destroy 你定义了吗?

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧