I encountered a relatively basic problem when I was learning Spring yesterday. I set a bean in applicationContext.xml:
<bean id="HelloWorld" class="action.HelloWorld">
<property name="msg">
<value >HelloWorld in applicationContext.xml</value>
</property>
</bean>
test code:
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import action.HelloWorld;
public class TestHelloWorld {
public static void main(String[] args){
ApplicationContext actx = new FileSystemXmlApplicationContext("applicationContext.xml");
HelloWorld HelloWorld = (HelloWorld)actx.getBean("HelloWorld");
System.out.println(HelloWorld.getMsg())
}
}
When testing with test, the compilation fails, and the prompt is that the system cannot find the specified file. The code is as follows:
Exception in thread "main" org.springframework.beans. factory.BeanDefinitionStoreException: IOException parsing XML document from file [D:\university\senior\laboratory tasks\workspace\SpringTest\applicationContext.xml]; nested exception is java.io.FileNotFoundException: applicationContext.xml (the system cannot find to the specified file.)
Later, I changed ApplicationContext actx = new FileSystemXmlApplicationContext("applicationContext.xml"); to ApplicationContext actx = new FileSystemXmlApplicationContext("src/applicationContext.xml");, added a src/, and it worked normally .
Since I didn't encounter the problem of adding the src path in the previous reference to my sister's code, I couldn't figure it out last night. When I looked at the code again this morning, I found that my sister was not using FileSystemXmlApplicationContext, but ClassPathXmlApplicationContext, so I understood something.
For ClassPathXmlApplicationContext(), the classpath: prefix is not required, the default is below the classpath path of the project (below src); if you want to use the absolute ClassPathXmlApplicationContext() path, you need to add the file: prefix to indicate that this is an absolute path;
for FileSystemXmlApplicationContext ( ), which means two types by default:
1. The one without a drive letter is the working path of the project, that is, the root directory of the project;
2, the one with a drive letter is the absolute path of the file.
3. If you want to use the classpath path, you need to prefix the classpath:
For example:
use ClassPathXmlApplicationContext():
1. ApplicationContext factory=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
2. ApplicationContext factory=new ClassPathXmlApplicationContext("applicationContext.xml") ;
3. ApplicationContext factory=new ClassPathXmlApplicationContext("file:D:\University study\senior\laboratory tasks\workspace\SpringTest\src\applicationContext.xml");
1. ApplicationContext factory=new FileSystemXmlApplicationContext("src/applicationContext.xml");
2. ApplicationContext factory=new FileSystemXmlApplicationContext("classpath:applicationContext.xml");
3. ApplicationContext factory=new FileSystemXmlApplicationContext("file:D:\University study \Senior year\laboratory tasks\workspace\SpringTest\src\applicationContext.xml");
4. ApplicationContext factory=new FileSystemXmlApplicationContext("D:\university study\senior\laboratory tasks\workspace\SpringTest\src\ applicationContext.xml");
In order to maintain unity with my sister, I still use ClassPathXmlApplicationContext honestly~
About the applicationContext.xml path problem in Spring
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=326834750&siteId=291194637
Ranking