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" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="mappingResources">
<list>
<value>Student.hbm.xml</value>
<value>Course.hbm.xml</value>
…
</list>
</property>
</bean>
如果X.hbm.xml文件很多时,则写起来会很不方便,可以像下面这种写法就简单多了:(其中假设所有的.hbm.xml文件都存于com/model目录)
<bean id="sessionFactory" class="org.springframework.
orm.hibernate.LocalSessionFactoryBean">
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/com/model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">100</prop>
</props>
</property>
</bean>
3.Spring中实现事务管理
首先,如果使用mysql,确定mysql为InnoDB类型。
事务管理的控制应该放到商业逻辑层。你可以写个处理商业逻辑的JavaBean,在该JavaBean中调用DAO,然后把该Bean的方法纳入spring的事务管理。
<bean id="txProxyTemplate" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="userManage" parent="txProxyTemplate">
<property name="target">
<bean class="com.yz.spring.service.implement.UserManageImpl">
<property name="userDAO">
<ref bean="userDAO"/>
</property>
</bean>
</property>
</bean>
com.yz.spring.service.implement.UserManageImpl就是我们的实现商业逻辑的JavaBean。我们通过parent元素声明其事务支持。
4.加载多个xml配置文件,生成ApplicationContext实例
(1)ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"applicationContext1.xml","applicationContext2.xml"});
BeanFactory factory=(BeanFactory)context;
(2)<beans> <import resource="conf/jms-applicationContext.xml"/></beans>
5.给bean指定别名
如:通过不同的名字来引用同一个数据源
<alias name="dataSource" alias="componentA-dataSource"/>
<alias name="dataSource" alias="componentB-dataSource"/>
6.JNDI定位DataSource(通常由应用程序服务器管理)
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/myds</value>
</property>
</bean>
7.加载属性文件
<bean id="jdbcConfiguration"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc-config.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" 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到CategoryService
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
CategoryService cs = (CategoryService) ctx.getBean("CategoryService");
List list =cs.getCategoryDAO().findAll();
%>
9.通过BeanFactoryAware从Spring容器中取Bean
public final class BeanFactoryHelper implements BeanFactoryAware {
private static BeanFactory beanFactory;
@Override
public void setBeanFactory(BeanFactory factory) throws BeansException {
beanFactory = factory;
}
public BeanFactory getBeanFactory() {
return beanFactory;
}
public static Object getBean(String name) {
return beanFactory.getBean(name);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public static Object getBean(String name, Class clazz) {
return beanFactory.getBean(name, clazz);
}
}
<bean id="beanFactoryHelper" class="net.demo.spring3.util.BeanFactoryHelper"></bean>
10.使用ApplicationContextAware让Spring容器传递ApplicationContext
public final class ApplicationContextHelper implements ApplicationContextAware{
private static ApplicationContext appCtx;
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
appCtx = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return appCtx;
}
public static Object getBean(String beanName ) {
return appCtx.getBean(beanName);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public static Object getBean(String name, Class requiredType) throws BeansException {
return appCtx.getBean(name, requiredType);
}
public static boolean containsBean(String name) {
return appCtx.containsBean(name);
}
public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
return appCtx.isSingleton(name);
}
}
<bean id="springApplicationHelper" class="net.demo.spring3.util.ApplicationContextHelper"></bean>
11.在Servlet(或者Filter,或者Listener)中使用spring的IOC容器
web.xml中的加载顺序为:listener >> filter >> servlet >> spring。其中filter的执行顺序是filter- mapping在web.xml中出现的先后顺序。
加载顺序会影响对spring bean的调用。比如filter 需要用到bean ,但是加载顺序是先加载filter 后加载spring,则filter中初始化操作中的bean为null。所以,如果过滤器中要使用到 bean,可以将spring 的加载改成Listener的方式。
在servlet或者filter或者Listener中使用spring的IOC容器的方法是:
WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
由于spring是注入的对象放在ServletContext中的,所以可以直接在ServletContext取出WebApplicationContext 对象:
WebApplicationContext webApplicationContext = (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
附参考资料:
http://www.family168.com/oa/tech/spring.html