Developing a Web backend server can be a delicate business, but there are always some problems, and often some of them are quite annoying
Crack is when something that you think is right goes wrong in your development process, and I attribute this kind of problem to my own knowledge
Not solid enough, or simply do not understand the knowledge caused by, good nonsense not to say, directly on the theme.
This problem is caused by applicationContext. XML. When you use Spring, you have to deal with the applicationContext. XML file.
So you have to understand what this document means.
As we know, the location of the applicationContext.xml file is not fixed; it can be in Java Resources, or SRC
Path, can also be placed in the web-INF path, but have you ever wondered what the difference is? If you want to understand how this works, you
Learn how configuration files are loaded in Spring.
Several containers support assembly bean in the Spring, including XMLBeanFactory, ClassPathXMLApplicationContext,
FileSystemXMLApplicationContext XMLWebApplicationContext.
The first: XMLBeanFactory
XMLBeanFactory references the Resource Resource Resource =new ClassPathResource("appcontext.XML");
BeanFactory factory = new XMLBeanFactory(resource);
Copy the code
The second: ClassPathXMLApplicationContext
// Compile path
ApplicationContext factory=new ClassPathXMLApplicationContext("classpath:appcontext.XML");
// SRC directory
ApplicationContext factory=new ClassPathXMLApplicationContext("appcontext.XML");
// SRC /conf directory
ApplicationContext factory=new ClassPathXMLApplicationContext("conf/appcontext.XML");
ApplicationContext factory=new ClassPathXMLApplicationContext("file:G:/Test/src/appcontext.XML");
Copy the code
Three: use the file system path
ApplicationContext factory=new FileSystemXMLApplicationContext("src/appcontext.XML");
/ / using the classpath: prefix, as a symbol, in this way, FileSystemXMLApplicationContext will also be able to read in the relative path under the classpath
ApplicationContext factory = new FileSystemXMLApplicationContext("classpath:appcontext.XML");
ApplicationContext factory = new FileSystemXMLApplicationContext("file:G:/Test/src/appcontext.XML");
ApplicationContext factory = new FileSystemXMLApplicationContext("G:/Test/src/appcontext.XML");
Copy the code
Four: XMLWebApplicationContext is customized for Web projects.
ServletContext servletContext = request.getSession().getServletContext();
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext );
Copy the code
Example:
How to load the applicationContext.xml file in Spring1.Using the ClassPathXmlApplicationContext can be read from the classpath XML file (1)
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao = (UserDao)context.getBean("userDao");
(2)
ClassPathXmlApplicationContext resource = new ClassPathXmlApplicationContext(new String[]{"applicationContext-ibatis-oracle.xml"."applicationContext.xml"."applicationContext-data-oracle.xml"});
BeanFactory factory = resource; UserDao userDao = (UserDao) factory.getBean("userDao");
2.The ClassPathResource is used to read the XML file Resource cr = from the classpathnew ClassPathResource("applicationContext.xml");
BeanFactory bf=new XmlBeanFactory(cr);
UserDao userDao = (UserDao)bf.getBean("userDao"); Load an XML file org. Springframework. Beans. Factory. Config. The accomplished doesn't work3. Read XmlWebApplicationContext CTX = using XmlWebApplicationContextnew XmlWebApplicationContext();
ctx.setConfigLocations(new String[] {"/WEB-INF/ applicationContext.xml"); ctx.setServletContext(pageContext.getServletContext());
ctx.refresh(); UserDao userDao = (UserDao ) ctx.getBean("userDao ");
4.Resource rs = =new FileSystemResource("D:/tomcat/webapps/test/WEB-INF/classes/ applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(rs);
UserDao userDao = (UserDao )factory.getBean("userDao"); Note: With FileSystemResource, the configuration file must be in the project direct directory or specify an absolute path, otherwise it will throw an exception that the file cannot be found.5. FileSystemXmlApplicationContext reads can be used to specify the XML definition file relative path or absolute path to read the definition file. String[] path={"WebRoot/WEB-INF/applicationContext.xml"."WebRoot/WEB-INF/applicationContext_task.xml"};
ApplicationContext context = newFileSystemXmlApplicationContext(path); Method 2: String path="WebRoot/WEB-INF/applicationContext*.xml";
ApplicationContext context = newFileSystemXmlApplicationContext(path); ApplicationContext CTX =new FileSystemXmlApplicationContext("The classpath: address"); If you don't have the classpath, it's from the current working directoryCopy the code
Summary: Of course, in addition to using ApplicationContext to assemble Spring, we can also use ContextLoaderListener.