SPRINGJUNIT4 configuration files under the Load Class directory (SRC) and Wef-inf directories

Source: Internet
Author: User

Path Description:

The configuration file under the Load class directory

@RunWith (Springjunit4classrunner.class) @ContextConfiguration ("Classpath:applicationContext_test.xml")  Public classMyTest1 {@AutowiredPrivateType T;//Gets the type instance that was injected in the Applicationcontext_test.xml@AutowiredPrivateHibernatetemplate hibernatetemplate;//Gets the hibernatetemplate instance that was injected in the Applicationcontext_test.xml//get an instance of type@Test Public voidgettypeinstance () {ApplicationContext ApplicationContext=NewFilesystemxmlapplicationcontext ("Classpath:applicationContext_test.xml"); System.out.println (Applicationcontext.getbean ("type"));//Type [Tid=0, Tname=testname1, tcontent= none]    }    //get an instance of Hibernatetemplate@Test Public voidgethibernatetemplateinstance () {System.out.println (hibernatetemplate);//[email protected]    }    }

Second, load the configuration file under the Wef-inf directory

@RunWith (Springjunit4classrunner.class) @ContextConfiguration (Locations={"File:webcontent/web-inf/config/applicationcontext.xml"})     Public classmytest2{@AutowiredPrivateHibernatetemplate hibernatetemplate;@AutowiredPrivateBasicdatasource DataSource;//Get Hibernatetemplate@Test Public voidgethibernatetemplate () {System.out.println (hibernatetemplate);//[email protected]    }    //Get DataSource@Test Public voidGetdatasource () {System.out.println (dataSource);//[email protected]System.out.println (Datasource.getmaxwait ());// +System.out.println (Datasource.getmaxactive ());// -    }}

Focus: 1. If the development tool is MyEclipse, the test SPRINGJUNIT4 cannot load the configuration file under the Wef-inf directory. 2. Even eclipse, when loading a configuration file in the Wef-inf directory, the Jdbc.properties (Tomcat container initialization applicationcontext loading is normal) cannot be loaded, so move it to the class directory.

Acknowledgement: http://blog.csdn.net/yeohcooller/article/details/7631202

Attached: The reason and analysis about the SPRINGJUNIT4 can't load the jdbc.properties under the Wef-inf directory

The answer of the Netizen

A June

This problem is due to the structure of your project, you put abc.properties in the Web-inf directory, no road in the development environment or in the product use environment, this abc.properties loading is a problem.
The solution:
If you can move the abc.properties file to your source file directory, if you use MAVEN to manage the project, then the default is Src/main/resources. This allows you to load properties in your Applicationcontext.xml file with the
Classpath:path/abc.properties.

b June

Sweat. You can do that under SRC. Published in the project. He'll put it in the Web-inf. Landlord, you have to consider. SRC The following code is put in there. The relationship between the source code and the published code. You get to know that. The problem is solved.
C:\Tomcat 5.5\bin\web-inf\jdbc.properties This was started in the bin when Tomcat was started. He'll use bin as a directory to find the Web-inf\jdbc.properties file below the bin you specified. And classpath is not the same. This is the relative path. According to your project address to find the web-inf below

SPRINGJUNIT4 configuration files under the Load Class directory (SRC) and Wef-inf directories

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.