Spring查找配置文件有三种方式。前两种为xml配置文件,后一种通过将bean配置放置在类中。这三种方式查找的根节点各有不同
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWord helloWorld=(HelloWord) context.getBean("hello");
helloWord.sayHello();
HelloWord helloWorld=(HelloWord) context.getBean("hello");
helloWord.sayHello();
注:这种方式是从src目录下查找
<--------------------------------------------------------------------------------------------------------------------------------------------------------------------->
Resource resource=new FileSystemResource("helloMessage.xml");
BeanFactory factory=new XmlBeanFactory(resource);
HelloWord helloWorld=(HelloWord)factory.getBean("hello");
String string=helloWorld.sayHello();
BeanFactory factory=new XmlBeanFactory(resource);
HelloWord helloWorld=(HelloWord)factory.getBean("hello");
String string=helloWorld.sayHello();
注:这种方式是从工程目录下查找
<--------------------------------------------------------------------------------------------------------------------------------------------------------------------->
ApplicationContext context=new AnnotationConfigApplicationContext(HelloConfig.class);
HelloWorld helloWorld=(HelloWorld) context.getBean("hello");
helloWorld.sayHello();
HelloWorld helloWorld=(HelloWorld) context.getBean("hello");
helloWorld.sayHello();
注:这种方式是从当前包目录下查找
650

被折叠的 条评论
为什么被折叠?



