春豆销毁方法、单例和原型示波器
我是春季框架的新手,从一些教程开始学习它。
我有以下文件,
# 主程序.java
package test.spring;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainProgram {
public static void main(String[] args) {
AbstractApplicationContext context =
new ClassPathXmlApplicationContext("Bean.xml");
HelloSpring obj = (HelloSpring) context.getBean("helloSpring");
obj.setMessage("My message");
obj.getMessage();
context.registerShutdownHook();
}
}
# 你好,弹簧.java
package test.spring;
public class HelloSpring {
private String message;
public void setMessage(String message){
this.message = message;
System.out.println("Inside setMessage");
}
public void getMessage(){
System.out.println("Your Message : " + this.message);
}
public void xmlInit() {
System.out.println("xml configured initialize");
}
public void xmlDestroy() {
System.out.println("xml configured destroy");
}
}
# 豆.xml
<?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="helloSpring" class="test.spring.HelloSpring"
scope="prototype" init-method="xmlInit" destroy-method="xmlDestroy">
</bean>
</beans>
当我采取我的输出是:scope="singleton"
xml configured initialize
Inside setMessage
Your Message : My message
xml configured destroy
当我采取我的输出是:scope="prototype"
xml configured initialize
Inside setMessage
Your Message : My message
xmlDestroy()
方法调用范围bean,但不是善意地帮助我进行以下操作,singleton
prototype
这是正确的吗?如果是这样,可能的原因是什么?
我也有一些疑问,
什么是区别或关系ApplicationContext , AbstractApplicationContext and ClassPathXmlApplicationContext