【发布时间】:2017-03-11 07:07:09
【问题描述】:
在 spring 作用域中,如果我使用单例,它会在执行逻辑之前加载配置文件时初始化所有 bean。但是如果我使用原型作为范围,bean 将根据调用进行初始化。对吗?单例是否作为静态变量和原型作为实例变量工作?
MainJava.java:
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainJava {
public static void main(String arg[]){
AbstractApplicationContext applicationContext=new ClassPathXmlApplicationContext("Beans.xml");
HelloWorld helloWorld=(HelloWorld) applicationContext.getBean("helloworld");
applicationContext.registerShutdownHook();
System.out.println(helloWorld.getMessage());
System.out.println("_________________________");
HelloWorld helloindia=(HelloWorld) applicationContext.getBean("helloindia");
System.out.println(helloindia.getMessage());
helloindia.setMessage("hello vishwa,thisa Bharat");
System.out.println(helloindia.getMessage());
System.out.println("_________________________");
HelloWorld hellonation=(HelloWorld) applicationContext.getBean("helloindia");
System.out.println(hellonation.getMessage());
}
}
HelloWorld.java:
public class HelloWorld {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public void init(){
System.out.println("initializing bean");
}
public void destroy(){
System.out.println("destroying bean");
}
}
初始化HelloWorld.java:
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class InitHelloWord implements BeanPostProcessor{
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("after initalizing the bean "+beanName);
return bean;
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("before initalizing the bean "+beanName);
return bean;
}
}
Beans.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 name="helloworld" class="com.vishwa.bhat.HelloWorld" init- method="init" destroy-method="destroy">
<property name="message" value="hi vishwa,welcome to this world"> </property>
</bean>
<bean name="helloindia" class="com.vishwa.bhat.HelloWorld" scope="prototype" init-method="init" destroy-method="destroy">
<property name="message" value="hi vishwa,welcome to India"></property>
</bean>
<bean class="com.vishwa.bhat.InitHelloWord"/>
</beans>
如果作用域是单例则输出:
before initalizing the bean helloworld
initializing bean
after initalizing the bean helloworld
before initalizing the bean helloindia
initializing bean
after initalizing the bean helloindia
hi vishwa,welcome to this world
_________________________
hi vishwa,welcome to India
hello vishwa,thisa Bharat
_________________________
hello vishwa,thisa Bharat
destroying bean
destroying bean
范围原型的输出:
before initalizing the bean helloworld
initializing bean
after initalizing the bean helloworld
hi vishwa,welcome to this world
_________________________
before initalizing the bean helloindia
initializing bean
after initalizing the bean helloindia
hi vishwa,welcome to India
hello vishwa,thisa Bharat
_________________________
before initalizing the bean helloindia
initializing bean
after initalizing the bean helloindia
hi vishwa,welcome to India
destroying bean
【问题讨论】: