If your applicationcontext. xml file is placed under the WEB-INF:
Use the classpathxmlapplicationcontext class!
ApplicationContext context = new ClassPathXmlApplicationContext("/WEB-INF/applicationContext.xml");
While classpathxmlapplicationcontext this class will eventually look for it under the WEB-INF/classes/directory, after your project is finally deployed, everything under SRC will be deployed under the WEB-INF/classes/directory, so the wood finds it!
Read using classpathxmlapplicationcontext: The following exception occurs and the specified file cannot be found!
Exception in thread "Main" org. springframework. beans. factory. beandefinitionstoreexception: ioexception parsing XML document from file [E: \ mycodes \ shop2 \ WEB-INF \ applicationcontext. XML]; Nested exception is Java. io. filenotfoundexception: WEB-INF \ applicationcontext. XML (the system cannot find the specified path .) At Org. springframework. beans. factory. XML. xmlbeandefinitionreader. loadbeandefinitions (xmlbeandefinitionreader. java: 349) at Org. springframework. beans. factory. XML. xmlbeandefinitionreader. loadbeandefinitions (xmlbeandefinitionreader. java: 310) at Org. springframework. beans. factory. support. abstractbeandefinitionreader. loadbeandefinitions (abstractbeandefinitionreader. java: 143) at Org. springframework. beans. fact Ory. support. abstractbeandefinitionreader. loadbeandefinitions (abstractbeandefinitionreader. java: 178) at Org. springframework. beans. factory. support. abstractbeandefinitionreader. loadbeandefinitions (abstractbeandefinitionreader. java: 149) at Org. springframework. beans. factory. support. abstractbeandefinitionreader. loadbeandefinitions (abstractbeandefinitionreader. java: 212) at Org. springframework. context. suppor T. abstractxmlapplicationcontext. loadbeandefinitions (abstractxmlapplicationcontext. java: 113) at Org. springframework. context. support. abstractxmlapplicationcontext. loadbeandefinitions (abstractxmlapplicationcontext. java: 80) at Org. springframework. context. support. abstractrefreshableapplicationcontext. refreshbeanfactory (abstractrefreshableapplicationcontext. java: 123) at Org. springframework. context. support. Abstractapplicationcontext. obtainfreshbeanfactory (abstractapplicationcontext. java: 422) at Org. springframework. context. support. abstractapplicationcontext. refresh (abstractapplicationcontext. java: 352) at Org. springframework. context. support. filesystemxmlapplicationcontext. <init> (filesystemxmlapplicationcontext. java: 140) at Org. springframework. context. support. filesystemxmlapplicationcontext. <init> (filesys Temxmlapplicationcontext. java: 84) at gzqh. wh. test. testproduct. main (testproduct. java: 18) caused by: Java. io. filenotfoundexception: WEB-INF \ applicationcontext. XML (the system cannot find the specified path .) At java. io. fileinputstream. open (native method) at java. io. fileinputstream. <init> (fileinputstream. java: 106) at Org. springframework. core. io. filesystemresource. getinputstream (filesystemresource. java: 108) at Org. springframework. beans. factory. XML. xmlbeandefinitionreader. loadbeandefinitions (xmlbeandefinitionreader. java: 336 )... 13 more
No way, it means you cannot read it. It's useless to write the path!
Okay. Read the absolute path of the file. Use the filesystemxmlapplicationcontext class!
ApplicationContext context=new FileSystemXmlApplicationContext("E:\\mycodes\\shop2\\WebRoot\\WEB-INF\\applicationContext.xml");
Bytes -----------------------------------------------------------------------------------------------------
public static void main(String[] args) { ApplicationContext context=new FileSystemXmlApplicationContext("E:\\mycodes\\shop2\\WebRoot\\WEB-INF\\applicationContext.xml"); ProductDAO dao =(ProductDAO) context.getBean("ProductDAO"); List<Product> list =dao.findAll();for (Product product : list) {System.out.println(product);System.out.println(product.getCode()); }}