1. What is Spring?
When I first approached Java, I wondered why everyone was talking about spring, as if Java was spring, and what was spring? In fact, he is a container frame, you can think of it as a toolbox, it can help you quickly Java development.
2, how to use spring?
Spring is especially easy to use, download the jar package, import the project, write the corresponding configuration file, you can use it. Specific tutorial, Baidu a search a lot of, I will not repeat the writing. Here I'll talk about three ways to load the Spring app context:
①classpathxmlapplicationcontext: By loading the XML file under the Classpath, This is the most common , that is, loading the applicationcontext.xml file under the SRC directory;
ApplicationContext context=new classpathxmlapplicationcontext ("Applicationcontext.xml");
②filesystemxmlapplicationcontext: Load the configuration file with the specified absolute path;
ApplicationContext context=new filesystemxmlapplicationcontext ("D:***/***/***/applicationcontext.xml");
③xmlwebapplicationcontext: Read the configuration file load under Web application.
ApplicationContext context=new xmlwebapplicationcontext ("Applicationcontext.xml");
If this is a Web project, then you need to configure the following XML:
<!--load Spring container configuration--
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
If only the above configuration, the/web-inf/applicationcontext.xml configuration file is loaded by default to initialize the ApplicationContext instance, If you want to specify a configuration file, then add the following configuration information to Web. xml
<!--set the spring container to load all the configuration file paths--
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/applicationContext.xml</param-value>
</context-param>
Then write the code inside the project to get:
ApplicationContext context = Webappliacationcontextutils.getwebapplicationcontext (Getservletcontext ());
The Spring Learning Path