1,在工程中导入spring支持的jar包:spring.jar、commons-logging.jar;
官网下载地址:http://www.springsource.org/download
2,配置bean的xml文件,模板可以下载的例子里找到;
<?xml version="1.0" encoding="UTF-8"?>
<!--
- Middle tier application context definition for the image database.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="..." class="...">
...
</beans>
3,实例化spring容器环境(xml文件是基于路径的):
方法一:
public static ApplicationContext getApplicationContextByClassPath()
{
// eg1.
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
// eg2.
// String[] locations = {"bean1.xml", "bean2.xml", "bean3.xml"};
// ApplicationContext ctx = new ClassPathXmlApplication(locations);
return ctx;
}
方法二:
public static ApplicationContext getApplicationContextByFileSystem()
{
// eg1.
ApplicationContext ctx =new FileSystemXmlApplicationContext("D:/workspace2/springtimer/src/beans.xml");
// eg2.
// String[] locations = {"beans.xml", "bean2.xml", "bean3.xml"};
// ApplicationContext ctx = new FileSystemXmlApplicationContext(locations );
return ctx;
}
4,在web环境中配置spring环境:
(1)在web.xml里配置ApplicationContext(beans.xml文件的路径是相对于web应用根目录的)
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/beans.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
如果有多个配置文件,
A.可以采用加载多个xml的方式:
a.可以这样用逗号分开:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext-dao.xml,/WEB-INF/applicationContext-service.xml</param-value>
</context-param>
b.也可以这样使用通配符:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext-*.xml</param-value>
</context-param>
B.可以采用import标签倒入多个文件的方式:
<beans>
<import resource="applicationContext-service.xml"/>
<import resource="applicationContext-dao.xml"/>
</beans>
(2)在Servlet中获取spring容器环境,获取bean
ServletContext servletContext=request.getSession().getServletContext();
ApplicationContext atx=WebApplicationContextUtils.getWebApplicationContext(servletContext);
IUserService userService=(IUserService)atx.getBean("userService");
注:也可以使用3步中的方式,不过每次创建bean,都需要加载xml、和bean,效率不高。
(3)在普通java类中获取ServletContext或ApplicationContext
1)实现接口org.springframework.web.context.ServletContextAware。如:
public class SpringContextUtil implements ServletContextAware
{
private static ServletContext servletContext;
@Override
public void setServletContext(ServletContext servletContext)
{
SpringContextUtil.servletContext = servletContext;
}
public static Object getBean(String name)
{
ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
return applicationContext.getBean(name);
}
}
2)实现接口org.springframework.context.ApplicationContextAware。如:
public class SpringContextUtil implements ApplicationContextAware
{
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
{
SpringContextUtil.applicationContext=applicationContext;
}
public static Object getBean(String name)
{
return applicationContext.getBean(name);
}
}
注意xml配置文件,bean必须立即加载,才能注入ServletContext或ApplicationContext对象:
<bean id="SpringContextUtil" class="com.company.util.SpringContextUtil" lazy-init="false"></bean>