老师好,如题。
容器启动的时候,会读取配置文件或者类,实例化所有非懒加载的类
<?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.xsd">
<bean id="bean" class="com.example.demo.ioc.Bean"/>
<bean id="bean2" class="com.example.demo.ioc.Bean2Factory" factory-method="getBean2"/>
<bean id="bean3Factory" class="com.example.demo.ioc.Bean3Factory" />
<bean id="bean3" class="com.example.demo.ioc.Bean3Factory" factory-method="getBean3" factory-bean="bean3Factory"/>
<bean id="kobe" class="com.example.demo.human.KobeBean" init-method="onInit" destroy-method="onDestroy"/>
</beans>
public class KobeTest {
@Test
public void test(){
AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
KobeBean bean = context.getBean("kobe",KobeBean.class);
System.out.println("bean:"+bean);
context.close();
// ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
// Bean1 bean1 = context.getBean("bean1",Bean1.class);
// System.out.println("bean1:"+bean1);
}
}
你是指init-method和destory-method吗?如果是全局配置的default-init-method和default-destory-method如果每个bean都有则一起初始化,没有这两个方法或者其中之一都会报错,如果在每个bean里单独配置了init-method,则会调用这个bean配置的init-method。自己理解,如有错误希望指出!