Spring Junit reads the configuration file under Web-inf

Source: Internet
Author: User

Suppose the spring configuration file is Applicationcontext.xml


the spring configuration file is below the Classpath

In spring's Java application, our spring configuration files are placed below the classpath (or, after compilation, into the classes directory).

Here's my project, because it's managed with MAVEN, so the configuration files are all placed under the "src/main/resources" directory



At this point in the code, you can pass the

ApplicationContext ApplicationContext = new Classpathxmlapplicationcontext ("Applicationcontext.xml");
Then get the appropriate bean.


If your code wants to test with the JUnit test framework, Spring provides junit support and the way you can use annotations:

@RunWith (Springjunit4classrunner.class)
@ContextConfiguration (locations={"Classpath:applicationContext.xml "})

Just precede the corresponding test class with these two annotations (the second annotation is used to indicate the configuration file location of spring), 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, when doing Java EE development, some people are accustomed to put the spring file 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

Web-inf directory below, as shown below.


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

ApplicationContext ApplicationContext = new Filesystemxmlapplicationcontext ("src/main/webapp/web-inf/ Applicationcontext.xml ");

Then get the appropriate bean.


If your code wants to test with the JUnit test framework, Spring provides junit support and the way you can use annotations:

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


Below is my one of the JUnit test classes under Spring management:

Package com.sohu.group.service.external;

Import java.util.List;

Import Org.junit.Test;
Import Org.junit.runner.RunWith;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.test.context.ContextConfiguration;
Import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith (Springjunit4classrunner.class)
@ContextConfiguration ({"file:src/main/webapp/web-inf/ Applicationcontext.xml "}) public
class Sufriendserviceimploverrmitest {

    @Autowired
    private Sufriendservice Sufriendservice;
    
    @Test public
    void Getuserfollowerlisttest () {
        list<string> List = Sufriendservice.getuserfollowerlist ("liug_talk@163.com");
        SYSTEM.OUT.PRINTLN ("------" +list);
    }




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.