Tôi mới vào khung công tác xuân, bắt đầu với một số hướng dẫn để tìm hiểu nó.Phương pháp tiêu diệt hạt đậu mùa xuân, singleton và nguyên mẫu
tôi có các file sau,
# MainProgram.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();
}
}
# HelloSpring.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");
}
}
# Bean.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>
Khi tôi mất scope="singleton"
của tôi đầu ra là:
xml configured initialize
Inside setMessage
Your Message : My message
xml configured destroy
Khi tôi mất scope="prototype"
đầu ra của tôi là:
xml configured initialize
Inside setMessage
Your Message : My message
xmlDestroy()
phương pháp được gọi với đậu singleton
phạm vi nhưng không phải với prototype
vui lòng giúp tôi cho sau này,
Is Điều này đúng? nếu có, lý do có thể là gì?
Ngoài ra tôi có một số thắc mắc như thế nào,
chênh lệch hoặc mối quan hệ là gì giữa ApplicationContext , AbstractApplicationContext and ClassPathXmlApplicationContext
Để phát hành tài nguyên, DestructionAwareBeanPostProcessor có thể được triển khai thay vì một BeanPostProcessor thông thường. –