Spring+Servlet整合(如何向Servlet注入属性(转),servlet获取spring容器中的bean)
<br />1.Spring数据库的配置view plaincopy to clipboardprint?<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-in
·
1.Spring数据库的配置
- <? 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:tx ="http://www.springframework.org/schema/tx"
- xmlns:aop ="http://www.springframework.org/schema/aop"
- 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 " >
- < bean id ="sessionFactory"
- class ="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
- scope ="singleton" >
- < property name ="dataSource" ref ="dataSource" />
- < property name ="hibernateProperties" >
- < props >
- < prop key ="hibernate.dialect" >
- org.hibernate.dialect.SQLServerDialect
- </ prop >
- < prop key ="hibernate.show_sql" > true</ prop >
- < prop key ="jdbc.use_scrollable_resultset" > true</ prop >
- < prop key ="hbm2ddl" > true</ prop >
- </ props >
- </ property >
- < property name ="mappingResources" >
- < list >
- < value > abu/csdn/bean/User.hbm.xml</ value >
- </ list >
- </ property >
- </ bean >
- < bean id ="hibernateTemplate" class ="org.springframework.orm.hibernate3.HibernateTemplate" scope ="singleton" >
- < property name ="sessionFactory" ref ="sessionFactory" />
- </ bean >
- < bean id ="dataSource"
- class ="org.apache.commons.dbcp.BasicDataSource" scope ="singleton" >
- < property name ="driverClassName"
- value ="com.microsoft.sqlserver.jdbc.SQLServerDriver" >
- </ property >
- < property name ="url"
- value ="jdbc:sqlserver://localhost:1433;databaseName=csu" >
- </ property >
- < property name ="username" value ="liky" />
- < property name ="password" value ="redhat" />
- </ bean >
- < bean id ="transactionManager"
- class ="org.springframework.orm.hibernate3.HibernateTransactionManager" >
- < property name ="sessionFactory" ref ="sessionFactory" />
- </ bean >
- < tx:advice id ="txAdvice" transaction-manager ="transactionManager" >
- < tx:attributes >
- < tx:method name ="save*" propagation ="REQUIRED" />
- < tx:method name ="delete*" propagation ="REQUIRED" />
- < tx:method name ="update*" propagation ="REQUIRED" />
- < tx:method name ="find*" read-only ="true" />
- </ tx:attributes >
- </ tx:advice >
- < aop:config >
- < aop:advisor pointcut-ref ="allServiceMethods"
- advice-ref ="txAdvice" />
- </ aop:config >
- </ beans >
2.Servlet
- package abu.csdn.servlet;
- import java.io.IOException;
- import javax.servlet.ServletContext;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.springframework.orm.hibernate3.HibernateTemplate;
- import org.springframework.web.context.WebApplicationContext;
- import org.springframework.web.context.support.WebApplicationContextUtils;
- /**
- *
- * 演示使用Spring向Servlet注入对象
- *
- * User: Abu Date: 2009-7-2 Time: 14:30:55
- */
- public class CopyOfShowImageServlet extends HttpServlet {
- HibernateTemplate hibernateTemplate;
- /**
- *
- * 在Servlet中注入对象的步骤:
- * 1.取得ServletContext
- * 2.利用Spring的工具类WebApplicationContextUtils得到WebApplicationContext
- * 3.WebApplicationContext就是一个BeanFactory,其中就有一个getBean方法
- * 4.有了这个方法就可像平常一样为所欲为了,哈哈!
- *
- */
- @Override
- public void init() throws ServletException {
- super .init();
- ServletContext servletContext = this .getServletContext();
- WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
- hibernateTemplate = (HibernateTemplate)ctx.getBean("hibernateTemplate" );
- }
- @Override
- protected void doPost(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- doGet(request, response);
- }
- @Override
- protected void doGet(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- }
- }
3.总结
你应该看到了,我在Spring中使用了声明式事务,如果直接使用Spring的工厂类在这里是不行的,因为所有的对象都已经有Spring的IoC管理了,所以只能借助WebApplicationContextUtils这个工具类来获得Bean.
更多推荐
已为社区贡献4条内容
所有评论(0)