Spring框架所提供的各類好處(如AOP、註解加強、註解MVC等)的惟一前提就是讓POJO的類變成一個受Spring容器管理的Bean,除此以 外沒有其餘任何的要求。下面的實例用一個POJO完成全部的功能,既是Controller,又是Service,仍是DAO: java
package com.baobaotao.mixlayer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; //①將POJO類經過註解變成Spring MVC的Controller @Controller public class MixLayerUserService { //②自動注入JdbcTemplate @Autowired private JdbcTemplate jdbcTemplate; //③經過Spring MVC註解映射成爲處理HTTP請求的函數,同時做爲一個擁有事務性的方法 @RequestMapping("/logon.do") @Transactional public String logon(String userName,String password){ if(isRightUser(userName,password)){ String sql = "UPDATE t_user u SET u.score = u.score + ? WHERE user_name =?"; jdbcTemplate.update(sql,20,userName); return "success"; }else{ return "fail"; } } private boolean isRightUser(String userName,String password){ //do sth return true; } }
經過@Controller註解將MixLayerUserService變成Web層的Controller,同時也是Service層的服務類。此 外,因爲直接使用JdbcTemplate訪問數據,因此MixLayerUserService仍是一個DAO。來看一下對應的Spring配置文件: mysql
<?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> … <!--①事務管理配置-> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource"/> <tx:annotation-driven/> <!--②啓動Spring MVC的註解功能--> <bean class="org.springframework.web.servlet.mvc.annotation. AnnotationMethodHandlerAdapter"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/> </beans>
在①處,經過事務註解驅動使MixLayerUserService的logon()工做於事務環境下,②處配置了Spring MVC的一些基本設施。要使程序可以運行起來還必須進行web.xml的相關配置: web
<?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"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:com/baobaotao/mixlayer/applicationContext.xml</param-value> </context-param> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/classes/log4j.properties</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>user</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:com/baobaotao/mixlayer/applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>user</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
這個配置文件很簡單,惟一須要注意的是DispatcherServlet的配置。默認狀況下Spring MVC根據Servlet的名字查找WEB-INF下的<servletName>-servlet.xml做爲Spring MVC的配置文件,在此,咱們經過contextConfigLocation參數顯式指定Spring MVC配置文件的確切位置。
將org.springframework.jdbc及org.springframework.transaction的日誌級別設置爲DEBUG,啓 動項目,並訪問http://localhost:8088/chapter10/logon.do?userName=tom應 用,MixLayerUserService#logon方法將做出響應,查看後臺輸出日誌,以下所示:spring
Spring框架自己不該是代碼複雜化的理由,使用Spring的開發者應該是無拘無束的:從實際應用出發,去除那些所謂原則性的接口,去掉強制分層的束縛,簡單纔是硬道理。 sql
注:以上內容摘自《Spring 3.x企業應用開發實戰》 數據庫