spring相关配置
1.web.xml中载入spring配置
<context-param><param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
或者
<servlet> <servlet-name>context</servlet-name> <servlet-class> org.springframework.web.context.ContextLoaderServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet>
配置完成之后,即可通过WebApplicationContextUtils.getWebApplicationContext
方法在Web应用中获取ApplicationContext引用。
为了在Struts中加载Spring Context,在struts-config.xml中增加如下部分:
<struts-config> <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" /> </plug-in> </struts-config>
2.关于hbm.xml文件的载入
通常在spring中会这么写代码:
<bean id="sessionFactory" name="code"><bean id="sessionFactory" name="code"><bean id="txProxyTemplate" abstract="true"parent="txProxyTemplate"><property name="target"><bean name="code"><alias name="dataSource" alias="componentA-dataSource"/><alias name="dataSource" alias="componentB-dataSource"/>
6.JNDI定位DataSource(通常由应用程序服务器管理)
<bean id="dataSource" name="code"><bean id="jdbcConfiguration"p:driverClassName="${jdbc_driver}" p:url="${jdbc_url}" p:username="${username}" p:password="${password}"/>8.在JSP里调用spring管理的bean取得数据
<%@ page import="org.springframework.context.ApplicationContext"%><%@ page import="org.springframework.web.context.support.WebApplicationContextUtils"%><%@ page import="com.yourcompany.service.CategoryService"%><%//applicationContext.xml中一定要有完整的依赖链,从dataSource到CategoryServiceApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());CategoryService cs = (CategoryService) ctx.getBean("CategoryService");List list =cs.getCategoryDAO().findAll();%>附参考资料:
http://www.family168.com/oa/tech/spring.html