Test the hibernate method by using SSH to read the applicationcontext. xml configuration file

Source: Internet
Author: User

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()); }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.