SpringでapplicationContext.xmlを読み込む方法

方法1 classpathからxmlを読み込む
ApplicationContext context = new ClassPathXmlApplicationContext(“applicationContext.xml");
UserDao dao = (UserDao)context.getBean(“userDao");

方法2 ClassPathResourceを使って配置ファイルを読む
Resource cr = new ClassPathResource(“applicationContext.xml");
BeanFactory bf=new XmlBeanFactory(cr);
UserDao userDao = (UserDao)bf.getBean(“userDao");

方法3.FileSystemResourceを読み込む
Resource rs = new FileSystemResource(“C:/tomcat /webapps/cftpj/
WEB-INF/classes/applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(rs);
UserDao userDao = (UserDao )factory.getBean(“userDao");
方法4.FileSystemXmlApplicationContextを読み込む
String[] path={“WebRoot/WEB-INF/applicationContext.xml",
“WebRoot/WEB-INF/applicationContext_task.xml"};
ApplicationContext context = new FileSystemXmlApplicationContext(path);

Java

Posted by arkgame