Spring+Servlet整合(如何向Servlet注入屬性(轉),servlet獲取spring容器中的bean)

1.Spring數據庫的配置

  1. <? xml version ="1.0" encoding ="UTF-8" ?>   
  2. < beans xmlns ="http://www.springframework.org/schema/beans"   
  3.     xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:tx ="http://www.springframework.org/schema/tx"   
  5.     xmlns:aop ="http://www.springframework.org/schema/aop"   
  6.     xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd " >   
  7.       
  8.     < bean id ="sessionFactory"   
  9.         class ="org.springframework.orm.hibernate3.LocalSessionFactoryBean"   
  10.         scope ="singleton" >   
  11.         < property name ="dataSource" ref ="dataSource" />   
  12.         < property name ="hibernateProperties" >   
  13.             < props >   
  14.                 < prop key ="hibernate.dialect" >   
  15.                      org.hibernate.dialect.SQLServerDialect   
  16.                 </ prop >   
  17.                 < prop key ="hibernate.show_sql" > true</ prop >   
  18.                   
  19.                 < prop key ="jdbc.use_scrollable_resultset" > true</ prop >   
  20.                 < prop key ="hbm2ddl" > true</ prop >   
  21.             </ props >   
  22.         </ property >   
  23.         < property name ="mappingResources" >   
  24.             < list >   
  25.                 < value > abu/csdn/bean/User.hbm.xml</ value >   
  26.             </ list >   
  27.         </ property >   
  28.     </ bean >   
  29.        
  30.       
  31.     < bean id ="hibernateTemplate" class ="org.springframework.orm.hibernate3.HibernateTemplate" scope ="singleton" >   
  32.         < property name ="sessionFactory" ref ="sessionFactory" />   
  33.     </ bean >   
  34.       
  35.     < bean id ="dataSource"   
  36.         class ="org.apache.commons.dbcp.BasicDataSource" scope ="singleton" >   
  37.         < property name ="driverClassName"   
  38.             value ="com.microsoft.sqlserver.jdbc.SQLServerDriver" >   
  39.         </ property >   
  40.         < property name ="url"   
  41.             value ="jdbc:sqlserver://localhost:1433;databaseName=csu" >   
  42.         </ property >   
  43.         < property name ="username" value ="liky" />   
  44.         < property name ="password" value ="redhat" />   
  45.     </ bean >   
  46.       
  47.     < bean id ="transactionManager"   
  48.         class ="org.springframework.orm.hibernate3.HibernateTransactionManager" >   
  49.         < property name ="sessionFactory" ref ="sessionFactory" />   
  50.     </ bean >   
  51.       
  52.     < tx:advice id ="txAdvice" transaction-manager ="transactionManager" >   
  53.         < tx:attributes >   
  54.               
  55.             < tx:method name ="save*" propagation ="REQUIRED" />   
  56.             < tx:method name ="delete*" propagation ="REQUIRED" />   
  57.             < tx:method name ="update*" propagation ="REQUIRED" />   
  58.             < tx:method name ="find*" read-only ="true" />   
  59.         </ tx:attributes >   
  60.     </ tx:advice >   
  61.       
  62.     < aop:config >   
  63.            
  64.         < aop:advisor pointcut-ref ="allServiceMethods"   
  65.             advice-ref ="txAdvice" />   
  66.     </ aop:config >   
  67.        
  68. </ beans >   

2.Servlet

  1. package abu.csdn.servlet;   
  2. import java.io.IOException;   
  3. import javax.servlet.ServletContext;   
  4. import javax.servlet.ServletException;   
  5. import javax.servlet.http.HttpServlet;   
  6. import javax.servlet.http.HttpServletRequest;   
  7. import javax.servlet.http.HttpServletResponse;   
  8. import org.springframework.orm.hibernate3.HibernateTemplate;   
  9. import org.springframework.web.context.WebApplicationContext;   
  10. import org.springframework.web.context.support.WebApplicationContextUtils;   
  11. /**
  12. *
  13. * 演示使用Spring向Servlet注入對象
  14. *
  15. * User: Abu Date: 2009-7-2 Time: 14:30:55
  16. */   
  17. public class CopyOfShowImageServlet extends HttpServlet {   
  18.      HibernateTemplate hibernateTemplate;   
  19.        
  20.        
  21.     /**
  22.       *
  23.       * 在Servlet中注入對象的步驟:
  24.       * 1.取得ServletContext
  25.       * 2.利用Spring的工具類WebApplicationContextUtils得到WebApplicationContext
  26.       * 3.WebApplicationContext就是一個BeanFactory,其中就有一個getBean方法
  27.       * 4.有了這個方法就可像平常一樣爲所欲爲了,哈哈!
  28.       *
  29.       */   
  30.     @Override   
  31.     public void init() throws ServletException {           
  32.         super .init();   
  33.                    
  34.          ServletContext servletContext = this .getServletContext();   
  35.                    
  36.          WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);   
  37.                    
  38.          hibernateTemplate = (HibernateTemplate)ctx.getBean("hibernateTemplate" );   
  39.      }   
  40.     @Override   
  41.     protected void doPost(HttpServletRequest request,   
  42.              HttpServletResponse response) throws ServletException, IOException {   
  43.          doGet(request, response);   
  44.      }   
  45.        
  46.        
  47.     @Override       
  48.     protected void doGet(HttpServletRequest request,   
  49.              HttpServletResponse response) throws ServletException, IOException {   
  50.            
  51.      }   
  52. }  

3.總結

你應該看到了,我在Spring中使用了聲明式事務,如果直接使用Spring的工廠類在這裏是不行的,因爲所有的對象都已經有Spring的IoC管理了,所以只能藉助WebApplicationContextUtils這個工具類來獲得Bean.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章