[size=medium]web启动
servlet启动 1--spring容器启动 2--加载自己的servlet
也就对应初始化操作:
1--在bean中进行初始化操作
2--等spring容器启动完毕,在servlet中进行初始化
1--在bean中进行初始化
加载bean的时候可以得到serletContext,但是[/size]
这个时候可以在这个bean中注入数据源,且该bean不需要继承其它类
添加DataSource属性即可.
2--在servlet中进行初始化
在web.xml中配置一下就可以了,init(ServletConfig servletConfig)可以得到
servletConfig,
WebApplicationContext wac=WebApplicationContextUtils.getWebApplicationContext(servletConfig); 这个也可以得到,因为spring容器已经初始化完毕了
如是getBean()就可以得到数据源了[/size]
servlet启动 1--spring容器启动 2--加载自己的servlet
也就对应初始化操作:
1--在bean中进行初始化操作
2--等spring容器启动完毕,在servlet中进行初始化
1--在bean中进行初始化
加载bean的时候可以得到serletContext,但是[/size]
WebApplicationContext wac=WebApplicationContextUtils.getWebApplicationContext(servletConfig);
[size=medium]无法获取
这个时候可以在这个bean中注入数据源,且该bean不需要继承其它类
添加DataSource属性即可.
2--在servlet中进行初始化
在web.xml中配置一下就可以了,init(ServletConfig servletConfig)可以得到
servletConfig,
WebApplicationContext wac=WebApplicationContextUtils.getWebApplicationContext(servletConfig); 这个也可以得到,因为spring容器已经初始化完毕了
如是getBean()就可以得到数据源了[/size]
<bean id="initSpringServletContext" class="com.isoftstone.telesales.util.InitSpringServletContext">
<property name="dataSource" ref="dataSource" />
</bean>
import org.springframework.web.context.ServletContextAware;
public class InitSpringServletContext implements ServletContextAware {
private static final Logger log = Logger
.getLogger(InitSpringServletContext.class);
private DataSource dataSource;
public void setServletContext(ServletContext servletContext) {
//这里就已经得到了servletContext,
//这个值是等配置文件的所有属性都set以后开始初始化的
}
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}