Spring+Servlet整合(如何向Servlet注入属性(转),servlet获取spring容器中的bean)

来源:互联网 发布:linux audit 编辑:程序博客网 时间:2024/06/09 20:24

1.Spring数据库的配置

view plaincopy to clipboardprint?
  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

view plaincopy to clipboardprint?
  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.