As a basic development tool, everyone must have downloaded the Spring framework . However, just downloading can only get one frame, and we also need to do some construction on it, which is somewhat similar to the variable settings we often do in Java, but there are some small differences. I believe that everyone here already wants to introduce only the specific Spring construction method. The following steps will be explained.
1. Configure web.xml file
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <!--Set forwarding--> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!--Load configuration file--> <param-value> classpath:applicationContext.xml</param-value> </init-param> <!--Mark whether the container loads this servlet when it starts. When the value is 0 or greater than 0, it means that the container loads this servlet when the application starts; When it is a negative number or not specified, it instructs the container to load only when the servlet is selected. The smaller the positive value, the higher the priority of starting the servlet. --> <load-on-startup> 1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <!--Accept all requests--> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
2. Configure the applicationContext.xml file
<beans xmlns= "http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <!--This can be removed, because the code for automatically scanning packages below contains the function of this line--> <context:annotation-config/> <!-- Automatically scan web packages and incorporate annotated classes into spring container management --> <context:component-scan base-package="com.zds"></context:component-scan> </beans>
3. Create a new Controller file
package com.zds; /** * @author zds * @date March 6, 2018 */ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/test") public class TestController { @RequestMapping(value = "hello", method = RequestMethod.GET) @ResponseBody public String helloWorld(@RequestParam("user") String userName) { String string = ""; string.split(","); return "Hello " + userName + " !"; } }
4. Put the required jar packages into the WEB-INF/lib folder . I put these jar packages and the built project here. If you are interested, you can download them.
5. The configuration is completed here. Add the project to tomcat in eclipse, start it, and enter in the browser:
http://localhost:8080/SpringWebProject/test/hello?user=world
expand
Spring core container: The core container provides the basic functions of the Spring framework. The main component of the core container is BeanFactory, which is the implementation of the factory pattern. BeanFactory uses the Inversion of Control (IOC) pattern to separate the application's configuration and dependency specifications from the actual application code.
The above is the construction of the java Spring framework, which is divided into five steps. As long as you follow the above operations, there will be no big problems. After learning, quickly build the downloaded Spring framework.