如果spring.xml里配置了几个bean,使用context的时候,会一起初始化这个配置文件里的所有bean?
老师好,如题。
老师好,如题。
2019-03-20
<?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);
}
}
举报