由于项目需求,自行搭建了一套spring4.1的空框架,下面将搭建过程中遇到的问题以及配置列举出来做个记录。
首先创建新的web工程,导入spring所依赖的jar包,如下:
这些jar包是根据需求进行测试后的结果,我使用的是spring自带的连接池,因此未进行合并,根据以后需要再进行合并ibatis或者hibernate
包导入以后,进行配置文件的创建,在src路径下,创建了springservlet-config.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" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd" > <!-- 使用默认的注解映射 --> <mvc:annotation-driven/> <!-- 自动扫描controller包中的控制器 --> <context:component-scan base-package="springMVC"/> <!-- 基于注释的事务,当注释中发现@Transactional时,使用id为“txManager”的事务管理器 --> <tx:annotation-driven transaction-manager="txManager"/> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <util:list id="beanList"> <ref bean="stringHttpMessageConverter" /> <ref bean="mappingJacksonHttpMessageConverter" /> </util:list> </property> </bean> <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter" /> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" /> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="50000000" /> <property name="defaultEncoding" value="UTF-8" /> </bean> <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="mediaTypes"> <map> <entry key="json" value="application/json" /> <entry key="html" value="text/html" /> <entry key="file" value="application/octet-stream" /> <entry key="apk" value="application/octet-stream"/> </map> </property> <property name="viewResolvers"> <list> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/" /> <property name="suffix" value=".jsp" /> </bean> </list> </property> <property name="defaultViews"> <list> <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" /> </list> </property> </bean> </beans>
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.1.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8" /> <property name="username" value="test" /> <property name="password" value="test" /> <property name="maxActive" value="100" /> <property name="maxIdle" value="30" /> <property name="maxWait" value="1000" /> <property name="defaultAutoCommit" value="true" /> <property name="removeAbandoned" value="true" /> <property name="removeAbandonedTimeout" value="60" /> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name = "dataSource" ref="dataSource"/> </bean> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <tx:annotation-driven transaction-manager="txManager"/> <bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /> </beans>
<?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_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>springMVC</display-name> <!-- spring MVC配置 --> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springservlet-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 载入spring --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/client/*</url-pattern> </servlet-mapping> <!-- 配置post表单编码格式(避免中文乱码) --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>这些就是所有的配置文件,我把我的目录结构展示下:
由于spring4.1中,注解非常的强大和方便,因此对应代码中全部使用了注解的方式,我写了一个小的例子
首先是controller层:
package springMVC.controller; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import springMVC.service.TestService; @Controller public class TestController { @Resource private TestService testService; @RequestMapping(value = "/test.{ext}", method = { RequestMethod.GET,RequestMethod.POST }) public ModelAndView test() throws Exception { ModelAndView mv = new ModelAndView(); String result = testService.printResult(); mv.addObject("dataMap", result); return mv; } }
package springMVC.service; public interface TestService { String printResult(); }
package springMVC.serviceimpl; import javax.annotation.Resource; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import springMVC.service.TestService; @Service public class TestServiceImpl implements TestService { @Resource private JdbcTemplate jdbcTemplate; @Transactional @Override public String printResult() { String result = "阿啊啊"; StringBuffer sql = new StringBuffer(); sql.append("update interfaceplan set last_excute_status = '20' where interface_plan_id = '1'"); jdbcTemplate.update(sql.toString()); return result; } }
其中,事务处理耗费了部分时间。主要是由于service注入的事务@Transactional不起作用,最后调查和实验结果,是由于我在springservlet-config.xml中,
没有进行查找到事务以后调用声明,即声明如下:
<!-- 基于注释的事务,当注释中发现@Transactional时,使用id为“txManager”的事务管理器 --> <tx:annotation-driven transaction-manager="txManager"/>
给自己留个记录,也分享给大家。