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
- 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
- @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 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
- 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
- @RunWith (Springjunit4classrunner. Class)
- @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
- 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 ("[email protected]");
- SYSTEM.OUT.PRINTLN ("------" +list);
- }
- }
Spring Junit reads the configuration file under Web-inf