网上搜了好久没有找到一个完整的Spring配置与开发教程,折腾了半天才搞定,下面把成果记录下来,如有谬误,还请读者朋友们及时指出,以免误导其它人。
注意,这个过程最终弄出来的是一个最简单的REST Service。
* jdk
* springsource-tool-suite-2.9.2.RELEASE-e3.7.2-win32-x86_64.zip
* apache-tomcat-7.0.28-windows-x64.zip
* spring-framework-3.1.1.RELEASE.zip
* spring-framework-3.1.0.CI-1162-dependencies.zip
2. 配置文件
* New "Dynamic Web Project", spring-books
* 新建 WebContent/WEB-INF/web.xml 并添加内容 (见后面)
* 新建 WebContent/WEB-INF/applicationContext.xml 并添加内容
* 新建 WebContent/WEB-INF/spring-servlet.xml 并添加内容
3. 配置jar
* 把 spring-framework-VERSION.zip 中的部分jar复制到 WebContent/WEB-INF/lib下 (后面有列表)
* 把第三方jar放到 WebContent/WEB-INF/lib 下
* 如果需要输出json,还需要json库
4. 编码
* src/com/springbooks/Book.java
* src/com/springbooks/BookServiceProvider.java
5. 验证
* 在 restclient (firefox插件) 中输入 http://localhost:8080/spring-books/rest/book/a
* Headers 中的 Accept 设置为 "application/xml" 或者 "application/json"
附件:
============================================================================
[web.xml]
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
============================================================================
[applicationContext.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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- Activates various annotations to be detected in bean classes -->
<context:annotation-config/>
<!-- Scans the classpath for annotated components that will be auto-registered as Spring beans. For example @Controller and @Service. Make sure to set the correct base-package-->
<context:component-scan base-package="com.springbooks"/>
<!-- Configures the annotation-driven Spring MVC Controller programming model. Note that, with Spring 3.0, this tag works in Servlet MVC only! -->
<mvc:annotation-driven/>
</beans>
============================================================================
[spring-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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Declare a view resolver -->
<!-- bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/ -->
</beans>
============================================================================
[spring 的 jar]
org.springframework.aop-3.1.1.RELEASE.jar
org.springframework.asm-3.1.1.RELEASE.jar
org.springframework.beans-3.1.1.RELEASE.jar
org.springframework.context.support-3.1.1.RELEASE.jar
org.springframework.context-3.1.1.RELEASE.jar
org.springframework.core-3.1.1.RELEASE.jar
org.springframework.expression-3.1.1.RELEASE.jar
org.springframework.web.servlet-3.1.1.RELEASE.jar
org.springframework.web-3.1.1.RELEASE.jar
============================================================================
[第三方 jar]
aopalliance.jar
aspectjrt.jar
aspectjweaver.jar
commons-logging-1.1.jar
============================================================================
[json jar]
com.springsource.org.codehaus.jackson.mapper-1.4.2.jar
com.springsource.org.codehaus.jackson-1.4.2.jar