BeanFactoryがIOC容器を取得する2つの方法
1197 ワード
BeanFactoryがIOC容器を取得する2つの方法
前編では環境、dao、serviceなどをテストしました.ここはそのまま使いましたpackage spring;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
public class BenFactoryTest {
public static void main(String[] args) {
/**
* BeanFactory IOC :
* 1、[ ]ClassPathResource (class ) xml
* 2、FileSystemResource xml
*/
//
BeanFactory factory= new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
//BeanFactory factory= new XmlBeanFactory(new ClassPathResource("config/applicationContext.xml"));
//2
//BeanFactory factory= new XmlBeanFactory(new FileSystemResource("src/applicationContext.xml"));
//BeanFactory factory= new XmlBeanFactory(new FileSystemResource("src/config/applicationContext_2.xml"));
// service
UserService userService=(UserService)factory.getBean("userService");
//
userService.findUsers();
}
}
上一篇:アプリケーションContextとBeanFactoryがIOC容器を取得した比較
package spring;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
public class BenFactoryTest {
public static void main(String[] args) {
/**
* BeanFactory IOC :
* 1、[ ]ClassPathResource (class ) xml
* 2、FileSystemResource xml
*/
//
BeanFactory factory= new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
//BeanFactory factory= new XmlBeanFactory(new ClassPathResource("config/applicationContext.xml"));
//2
//BeanFactory factory= new XmlBeanFactory(new FileSystemResource("src/applicationContext.xml"));
//BeanFactory factory= new XmlBeanFactory(new FileSystemResource("src/config/applicationContext_2.xml"));
// service
UserService userService=(UserService)factory.getBean("userService");
//
userService.findUsers();
}
}