示例項目下載: https://github.com/yangyxd/SpringDemo
利用前面 SpringMVC 項目的配置方式,完成初步的項目創建。下面只講一些不同之處。
傳送門: [Java] Maven 建立 Spring MVC 工程
目錄結構

pom.xml
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.yxd.demo</groupId> <artifactId>SpringDemo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>SpringDemo Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <commons-lang.version>2.6</commons-lang.version> <slf4j.version>1.7.6</slf4j.version> <spring.version>4.3.3.RELEASE</spring.version> <jackson.version>2.8.3</jackson.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>${commons-lang.version}</version> </dependency> <!-- 防止 jsp 報錯 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!-- JSTL 定制標簽庫 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> <type>jar</type> <scope>compile</scope> </dependency> <!-- Json 庫 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <!-- 各種集合類和集合工具類 --> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.2</version> </dependency> <!-- Spring Begin --> <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-test</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-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- Spring End --> </dependencies> <build> <plugins> <!-- maven 編譯插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <!-- 源代碼使用的開發版本 --> <source>1.8</source> <!-- 需要生成的目標class文件的編譯版本 --> <target>1.8</target> <!-- 默認字符集編碼 --> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> <finalName>SpringDemo</finalName> </build> <packaging>war</packaging> </project>
因為要用到 JUnit ,所以在依賴中添加 spring-test。 並且將 junit 改為較新的 4.10 。
web.xml
<?xml version="1.0" encoding="UTF-8"?> <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_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Archetype Created Web Application</display-name> <!-- Spring配置 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> </init-param> <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>index.jsp</welcome-file> </welcome-file-list> </web-app>
dispatcher-servlet.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" 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"> <!-- 啟用Spring基於annotation的DI, 使用戶可以在Spring MVC中使用Spring的強大功能。 激活 @Required @Autowired,JSR 250's @PostConstruct, @PreDestroy and @Resource 等標注 --> <context:annotation-config /> <!-- DispatcherServlet上下文, 只管理@Controller類型的bean, 忽略其他型的bean, 如@Service --> <context:component-scan base-package="com.yxd.example.bean"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <!-- HandlerMapping, 無需配置, Spring MVC可以默認啟動。 DefaultAnnotationHandlerMapping annotation-driven HandlerMapping --> <!-- 擴充了注解驅動,可以將請求參數綁定到控制器參數 --> <mvc:annotation-driven /> <!-- 靜態資源處理, css, js, imgs --> <mvc:resources mapping="/resources/**" location="/resources/" /> <!-- 配置路徑擴展名映射的媒體類型 --> <bean name="pathExtensionContentNegotiationStrategy" class="org.springframework.web.accept.PathExtensionContentNegotiationStrategy"> <constructor-arg> <props> <prop key="json">application/json</prop> <prop key="xml">application/xml</prop> <prop key="htm">text/html</prop> <prop key="rss">application/rss+xml</prop> <prop key="atom">application/atom+xml</prop> </props> </constructor-arg> </bean> <!-- 配置映射媒體類型的策略 --> <bean name="mvcContentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManager"> <constructor-arg> <list> <ref bean="pathExtensionContentNegotiationStrategy" /> </list> </constructor-arg> </bean> <!-- 配置方法級別的@RequestMapping處理器 --> <bean name="requestMappingHandlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"> <property name="order" value="0" /> <property name="contentNegotiationManager" ref="mvcContentNegotiationManager" /> </bean> <!-- 配置數據轉換服務,默認使用格式化數據轉換服務,可以對日期和數字進行格式化 --> <bean name="conversionService" class="org.springframework.format.support.DefaultFormattingConversionService"> <constructor-arg index="0"> <null></null> </constructor-arg> <constructor-arg index="1"> <value>true</value> </constructor-arg> </bean> <!-- 配置ViewResolver。 可以用多個ViewResolver。 使用order屬性排序。 InternalResourceViewResolver放在最后。 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/jsps/" /> <property name="suffix" value=".jsp" /> </bean> <!--200*1024*1024即200M resolveLazily屬性啟用是為了推遲文件解析,以便捕獲文件大小異常 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="209715200" /> <property name="defaultEncoding" value="UTF-8" /> <property name="resolveLazily" value="true" /> </bean> </beans>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- 自動掃描web包 ,將帶有注解的類 納入spring容器管理 --> <context:component-scan base-package="com.yxd.example.*"></context:component-scan> <bean id="SpringApplicationContext" class="com.yxd.example.bean.ContextHelper"></bean> </beans>
maven 不能設置為 web3.0 解決方法
錯誤: Description Resource Path Location Type Cannot change version of project facet Dynamic Web Module to 3.0.
首先在硬盤下找到 \項目名\.setting\文件夾 下的 org.eclipse.wst.common.project.facet.core.xml xml文件。
<?xml version="1.0" encoding="UTF-8"?> <faceted-project> <fixed facet="wst.jsdt.web"/> <installed facet="jst.web" version="2.3"/> <installed facet="wst.jsdt.web" version="1.0"/> <installed facet="java" version="1.7"/> </faceted-project>
將 jst.web 的version 改成 3.0.
然后在eclipse里右鍵該工程--maven--updateProject (或按 Alt + F5)即可。
參考資料
Spring IOC--Bean的裝配(使用注解定義Bean)
