기술과 산업/언어 및 프레임워크

XML로 정의된 Bean 호출하기

B컷개발자 2012. 12. 5. 12:02
728x90

정의 된 빈 호출 하기


1. BeanFactory 사용 (XmlBeanFactory 사용은 deprecated 되었음)


예1) 파일 시스템 기반

Resource resource = new FileSystemResource("src/main/resources/spring/app-context.xml");
BeanFactory factory = new XmlBeanFactory(resource); 



예2) 클래스 패스 기반

ClassPathResource resource2 = new ClassPathResource("spring/app-context.xml");

BeanFactory factory2 = new XmlBeanFactory(resource2);



2. ApplicationContext 사용


예1) 하나의 설정 파일 사용할 경우

ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/app-context.xml");

예2) 여러개의 설정 파일 사용할 경우

ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] {"spring/app-context.xml"});


환경

Eclipse + Spring 3.1 + Maven


codesnippet-helloworld.7z



728x90