Spring Junit reads the configuration file under Web-inf

Source: Internet
Author: User

Assume that the spring configuration file is Applicationcontext.xml


One, the spring configuration file is below the Classpath

In spring Java applications, our spring configuration files are typically placed under the Classpath (which is compiled into the classes directory).

The following is my project because it is managed by MAVEN, so the configuration files are placed in the "src/main/resources" directory



In the code, you can

[Java]View Plaincopy
    1. ApplicationContext ApplicationContext = new Classpathxmlapplicationcontext ("Applicationcontext.xml");

Then get the appropriate bean.


If the code wants to test with the JUnit test framework, Spring provides a way for junit support, as well as annotations:

[Java]View Plaincopy
    1. @RunWith (Springjunit4classrunner. Class)
    2. @ContextConfiguration (locations={"Classpath:applicationContext.xml"})

Just precede the corresponding test class with these two annotations (the second annotation is used to indicate the location of the spring's configuration file), and you can use the dependency injection functionality provided by spring in the JUnit test class.

Second, spring configuration file under Web-inf

Of course, in the development of the Java EE, some people are used to put spring files in the Web-inf directory (although more people are accustomed to put under the classpath), or some spring configuration files are placed under the classpath, and some are placed in the

Under the Web-inf directory, such as.


At this point, you cannot use Classpathxmlapplicationcontext to load a configuration file in your code, but you should use Filesystemxmlapplicationcontext.

[Java]View Plaincopy
    1. ApplicationContext ApplicationContext = new Filesystemxmlapplicationcontext ("src/main/webapp/web-inf/  Applicationcontext.xml ");

Then get the appropriate bean.


If the code wants to test with the JUnit test framework, Spring provides a way for junit support, as well as annotations:

[Java]View Plaincopy
    1. @RunWith (Springjunit4classrunner. Class)
    2. @ContextConfiguration (locations={"File:src/main/webapp/web-inf/applicationcontext.xml"})

Just precede the corresponding test class with these two annotations (the second annotation is used to indicate the location of the spring's configuration file), and you can use the dependency injection functionality provided by spring in the JUnit test class.


Here's one of my spring-managed JUnit test classes:

[Java]View Plaincopy
    1. Package com.sohu.group.service.external;
    2. Import java.util.List;
    3. Import Org.junit.Test;
    4. Import Org.junit.runner.RunWith;
    5. Import org.springframework.beans.factory.annotation.Autowired;
    6. Import org.springframework.test.context.ContextConfiguration;
    7. Import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    8. @RunWith (Springjunit4classrunner. Class)
    9. @ContextConfiguration ({"File:src/main/webapp/web-inf/applicationcontext.xml"})
    10. Public class Sufriendserviceimploverrmitest {
    11. @Autowired
    12. private Sufriendservice Sufriendservice;
    13. @Test
    14. public void Getuserfollowerlisttest () {
    15. list<string> list = sufriendservice.getuserfollowerlist ("[email protected]");
    16. SYSTEM.OUT.PRINTLN ("------" +list);
    17. }
    18. }

Spring Junit reads the configuration file under Web-inf

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.