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