Spring4 基本使用
程序员文章站
2022-03-07 22:21:31
前言 虽然现在基本上是 springboot 的天下了,但是传统的 spring4 在广大的软件企业中仍然占据很大比例。一上手能用,但是要让我从无到有搭一个spring4的开发环境出来,可能会磕磕碰碰,耽误不少时间,所以这里从无到有搭一个 spring4的开发环境出来,一步步完善,也复习下 spri ......
前言
虽然现在基本上是 springboot 的天下了,但是传统的 spring4 在广大的软件企业中仍然占据很大比例。一上手能用,但是要让我从无到有搭一个spring4的开发环境出来,可能会磕磕碰碰,耽误不少时间,所以这里从无到有搭一个 spring4的开发环境出来,一步步完善,也复习下 spring 的基本用法,温故知新。
### 创建maven项目,引入spring4的一些核心包和常用扩展包。我用的是spring4最新版本 4.3.24.release
pom.xml
?xml version="1.0" encoding="utf-8"?> <project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupid>com.hz</groupid> <artifactid>admin</artifactid> <version>1.0-snapshot</version> <packaging>war</packaging> <name>admin maven webapp</name> <properties> <project.build.sourceencoding>utf-8</project.build.sourceencoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <spring.version>4.3.24.release</spring.version> <junit.version>4.11</junit.version> <jackson.version>2.9.8</jackson.version> </properties> <dependencies> <!-- junit 测试 --> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>${junit.version}</version> <scope>test</scope> </dependency> <!--spring 常用模块依赖 start--> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-beans</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-core</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-context</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-context-support</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-aop</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-aspects</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-jdbc</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-test</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-tx</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-web</artifactid> <version>${spring.version}</version> </dependency> <!-- spring 常用模块依赖 end --> <!-- jackson json转换配置,配置--> <!-- 这个配置对应 requestparams, requestbody, responsebody 等注解,原始请求参数其实都在 httpservletrequest 对象里, 但实际上我们用spring的时候可以直接在controller方法参数里得到请求数据,string,integer, map, list 和自定义bean 这个转换就要用到这个包, 不引入这个包,使用requestbody注解就会报错--> <dependency> <groupid>com.fasterxml.jackson.core</groupid> <artifactid>jackson-databind</artifactid> <version>${jackson.version}</version> </dependency> <dependency> <groupid>com.fasterxml.jackson.core</groupid> <artifactid>jackson-core</artifactid> <version>${jackson.version}</version> </dependency> <dependency> <groupid>com.fasterxml.jackson.core</groupid> <artifactid>jackson-annotations</artifactid> <version>${jackson.version}</version> </dependency> <!-- spring mvc 依赖 start--> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-webmvc</artifactid> <version>${spring.version}</version> </dependency> <!-- spring mvc 依赖 end--> </dependencies> <build> <finalname>admin</finalname> <pluginmanagement><!-- lock down plugins versions to avoid using maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactid>maven-clean-plugin</artifactid> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#plugin_bindings_for_war_packaging --> <plugin> <artifactid>maven-resources-plugin</artifactid> <version>3.0.2</version> </plugin> <plugin> <artifactid>maven-compiler-plugin</artifactid> <version>3.8.0</version> <configuration> <source>${maven.compiler.source}</source> <target>${maven.compiler.target}</target> </configuration> </plugin> <plugin> <artifactid>maven-surefire-plugin</artifactid> <version>2.22.1</version> </plugin> <plugin> <artifactid>maven-war-plugin</artifactid> <version>3.2.2</version> </plugin> <plugin> <artifactid>maven-install-plugin</artifactid> <version>2.5.2</version> </plugin> <plugin> <artifactid>maven-deploy-plugin</artifactid> <version>2.8.2</version> </plugin> <!-- jetty 插件--> <plugin> <groupid>org.eclipse.jetty</groupid> <artifactid>jetty-maven-plugin</artifactid> <version>9.4.18.v20190429</version> <configuration> <httpconnector> <port>8082</port> <!-- 监听的http 端口,默认8080 --> </httpconnector> <scanintervalseconds>2</scanintervalseconds> <webapp> <contextpath>/</contextpath> </webapp> </configuration> </plugin> </plugins> </pluginmanagement> </build> </project>
配置web.xml
web.xml文件是web项目的启动文件,要想在web项目中使用spring,需要在这里配置spring
<!doctype web-app public "-//sun microsystems, inc.//dtd web application 2.3//en" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>archetype created web application</display-name> <!-- spring配置文件位置--> <context-param> <param-name>contextconfiglocation</param-name> <param-value>classpath:/spring.xml</param-value> <!-- 这个文件主要配置spring beans,包括数据源,事务控制 </context-param> <!-- 启动web时加载spring的配置文件 --> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <!-- 添加springmvc支持,单独springmvc时也需要在web.xml文件中配置 --> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>contextconfiglocation</param-name> <param-value>classpath:/spring/spring-mvc.xml</param-value> <!-- spring-mvc.xml 主要配置mvc方面,包括controller注解扫描,视图解析器, controller参数绑定(这里就要用到pom里的jackson json依赖) --> </init-param> <!-- 启动web时就加载springmvc的servlet,即启动时就加载springmvc的配置文件 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <!-- 拦截所有请求 --> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list><!--指定欢迎页面--> <welcome-file>login.html</welcome-file> </welcome-file-list> <error-page> <!--当系统出现404错误,跳转到页面nopage.html--> <error-code>404</error-code> <location>/nopage.html</location> </error-page> <error-page> <!--当系统出现java.lang.nullpointerexception,跳转到页面error.html--> <exception-type>java.lang.nullpointerexception</exception-type> <location>/error.html</location> </error-page> <session-config><!--会话超时配置,单位分钟--> <session-timeout>360</session-timeout> </session-config> </web-app>
配置mvc
spring-mvc.xml
<?xml version="1.0" encoding="utf-8"?> <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" xmlns:p="http://www.springframework.org/schema/p" 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-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!-- spring扫描的包 --> <context:component-scan base-package="com.hz.*.controller"/> <!-- dispatcherservlet不处理静态资源,交给服务器默认的servlet处理 --> <mvc:default-servlet-handler /> <!-- 启用annotation --> <mvc:annotation-driven /> <!--json转换器配置, @responsebody,requestbody 将请求参数转换成bean需要这个类 --> <bean id="mappingjacksonhttpmessageconverter" class="org.springframework.http.converter.json.mappingjackson2httpmessageconverter"> <property name="supportedmediatypes"> <list> <value>text/html;charset=utf-8</value> </list> </property> </bean> <!-- 视图渲染器 ,如果搭建rest web应用,可以不配置这个,具体rest-web应用配置,后面再讲--> <!-- 视图xxx 将对应访问/web-inf/page/xxx.jsp文件 --> <bean class="org.springframework.web.servlet.view.internalresourceviewresolver" id="internalresourceviewresolver"> <!-- 前缀 --> <property name="prefix" value="/web-inf/page/" /> <!-- 后缀 --> <property name="suffix" value=".jsp" /> </bean> </beans>
项目结构
测试controller
package com.hz.common.controller; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.responsebody; import java.util.hashmap; import java.util.map; @controller() @requestmapping("test") public class testcontroller { @requestmapping("/info") @responsebody public object test(integer val){ // contenttype=application/x-www-form-urlencoded system.out.println(val == null ? "null" : val.tostring()); hashmap<string, object> map = new hashmap<>(); map.put("prop", "value"); map.put("prop1", "value1"); return map; } }
postman测试