In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, the editor will share with you the relevant knowledge points about how to use the ClassPathXmlApplicationContext class in Spring. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.
The use of the ClassPathXmlApplicationContext class 1. Simply test with ApplicationContext
Get the Bean instance (object) defined in Spring. You can use:
ApplicationContext ac = new ClassPathXmlApplicationContext ("applicationContext.xml"); RegisterDAO registerDAO = (RegisterDAO) ac.getBean ("RegisterDAO")
If there are more than two:
ApplicationContext ac = new ClassPathXmlApplicationContext (new String [] {"applicationContext.xml", "dao.xml"})
Or use wildcards:
ApplicationContext ac = new ClassPathXmlApplicationContext ("classpath:/*.xml"); II. ClassPathXmlApplicationContext
[only read configuration files placed in the web-info/classes directory] and the difference between FileSystemXmlApplicationContext
Classpath: the prefix is not required. The default is under the classpath path of the project.
If you want to use an absolute path, you need to add a file: prefix to indicate that it is an absolute path
For FileSystemXmlApplicationContext:
The default is two:
1. What does not have a drive letter is the project work path, that is, the root directory of the project
two。 A drive letter indicates the absolute path of the file.
If you want to use the classpath path, you need the prefix classpath:
Public class HelloClient {protected static final Log log = LogFactory.getLog (HelloClient.class); public static void main (String [] args) {/ / Resource resource = new ClassPathResource ("appcontext.xml"); / / BeanFactory factory = new XmlBeanFactory (resource); / / classpath path / / ApplicationContext factory = new ClassPathXmlApplicationContext ("classpath:appcontext.xml"); / / ApplicationContext factory = new ClassPathXmlApplicationContext ("appcontext.xml") / / ClassPathXmlApplicationContext uses the file prefix to use the absolute path / / ApplicationContext factory = new ClassPathXmlApplicationContext ("file:F:/workspace/example/src/appcontext.xml"); / / the path to the file system, which defaults to the project's root path / / ApplicationContext factory = new FileSystemXmlApplicationContext ("src/appcontext.xml"); / / ApplicationContext factory = new FileSystemXmlApplicationContext ("webRoot/WEB-INF/appcontext.xml") / / use the classpath: prefix so that FileSystemXmlApplicationContext can also read the relative path under classpath / / ApplicationContext factory = new FileSystemXmlApplicationContext ("classpath:appcontext.xml"); / / ApplicationContext factory = new FileSystemXmlApplicationContext ("file:F:/workspace/example/src/appcontext.xml"); / / without the file prefix ApplicationContext factory = new FileSystemXmlApplicationContext ("F:/workspace/example/src/appcontext.xml"); IHelloWorld hw = (IHelloWorld) factory.getBean ("helloworldbean") Log.info (hw.getContent ("luoshifei"));}} problems encountered in using ClassPathXmlApplicationContext
There is an error in executing the main method above, because the applicationContext.xml should be found according to the BeforeAdvice.class path, so it is normal as shown below:
It can also be written as follows:
These are all the contents of the article "how to use ClassPathXmlApplicationContext classes in Spring". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.