Background
Just knowing spring, it gives me the feeling that the encapsulated tool class will make our development easier, more energy will be put on business logic, of course, some excellent ideas of spring, what control inversion, face-to-face and so on. I don't know exactly how it works. Later, slowly understand.. try to build a spring framework, here is a record.
II. Construction
The project is relatively simple, using only spring core and webMvc packages, which is enough for requests to return to the page ____________.
Environment: idea+maven
Configuring the pom.xml file
This is the name of several nodes that need to be entered manually when creating a project
<! - Project Package - > <groupId>com.huimin</groupId> <! - Project name - > <artifactId>first_spring</artifactId> <! - Packed files are in war format - > <packaging>war</packaging> <! - Version - > <version>1.0-SNAPSHOT</version>
The following configures several jar packages that maven depends on
<!--Unified Management Version--> <properties> <!--Coding format--> <project.build.sourceEncoding>utf-8</project.build.sourceEncoding> <!--spring format--> <spring.version>4.3.5.RELEASE</spring.version> <!--Store the version number of some packages--> </properties> <dependencies> <!--Dependence on unit testing--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!--mvc--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!--spring Core package--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> </dependencies>
2.spring configuration
After the introduction of spring framework, the whole project is built on spring, so spring configuration should be loaded at the entrance of the program, so it should be configured in web.xml first.
(1) Configure a spring listener first. When a web project starts, start loading the spring configuration file through this listener.
<!--spring Monitor ApplicationContext Loading--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
(2) Specify the spring configuration file path
(3) Specify spring management mapping relationships<!--To configure applicationContext.xml Global Name--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/applicationContext.xml</param-value> </context-param>
<servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--Definition spring Separate configuration, servlet Relevant content in spring-servlet In separate management--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!--Set the mapping relationship by spring Administration--> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
Configuration of web.xml is over.
(4) spring-servlet configuration content
<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:aop="http://www.springframework.org/schema/aop" 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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!--Open Annotation Driver--> <mvc:annotation-driven></mvc:annotation-driven> <!--Scanning Pack--> <context:component-scan base-package="com.huimin.*"></context:component-scan> <!--jsp Page Designated Location optional content--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--File path--> <property name="prefix" value="/WEB-INF/jsp/"/> <!--file extension--> <property name="suffix" value=".jsp"/> </bean> </beans>
A bunch of links on the beans tag seem to have their origins
The above addresses are xsi, context, aop, and mvc namespaces respectively. As for why they are so named, because of the regulations, haha, the name is not very deep.xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
Just remember that he's a namespace.
The.xsd file in schemaLocation is a specification of xml. Introducing the xsd file of context, the context tag can be written in the configuration.
The xsd of beans is introduced, so the beans tag can be introduced into the configuration.
There must be two configurations in the spring configuration file: open annotation driver and scan package
java code (annotations)
@Controller public class RegistController { @RequestMapping("/regist") public String regist(){ return "firstJsp"; } }Okay, the simplest spring framework is built up... the process runs, and other configurations and usages are much easier to say...
Summary
As for why to use spring framework, I don't know much about it yet. I've talked about how good spring is, but I haven't personally realized it yet. Everyone uses it first. If you know more, you will realize the benefits of spring.