Spring+Servlet整合(如何向Servlet注入属性(转),servlet获取spring容器中的bean)
来源:互联网 发布:linux audit 编辑:程序博客网 时间:2024/06/09 20:24
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.
- Spring+Servlet整合(如何向Servlet注入属性(转),servlet获取spring容器中的bean)
- Spring+Servlet整合(如何向Servlet注入属性)
- Struts2、Hibernate、Spring整合时如何向Servlet注入属性
- Struts2、Hibernate、Spring整合时如何向Servlet注入属性
- servlet spring bean注入
- 在servlet中注入spring容器中的bean
- 在servlet中注入spring容器中的bean
- Servlet获取Spring bean
- 如何让servelt和Filter注入到spring容器或者在servlet中调用spring中的bean
- 如何在servlet 获取spring bean
- Spring强制向servlet中注入bean的方法
- 【Spring强制向servlet中注入bean的方法】
- 使用自定义注解向servlet注入spring bean
- 如何使用spring的autowire为servlet注入Bean
- 如何使用spring的autowire为servlet注入bean
- 如何使用spring的autowire为servlet注入Bean
- 如何使用spring的autowire为servlet注入Bean
- Spring Bean 注入 Servlet 的方法
- 如何学好c++
- 软件管理
- 小议函数调用—关于调用后的值保存问题
- 附件程序
- TaobaoWeb
- Spring+Servlet整合(如何向Servlet注入属性(转),servlet获取spring容器中的bean)
- 项目错误集锦
- tiangongkaiwu
- TurboMail免费打造企业级别邮件系统
- hdu1242 rescue
- 排序算法之插入排序
- js表单验证控制代码大全
- java中的强引用(Strong Reference)
- 对动态添加控件的视图状态分析