spring 容器外的实例得到spring中的bean,达到启动后初始化数据目的

可用过在web.xml文件中配置一个listener或servlet启动时加载spring的bean,然后用WebApplicationContextUtils.getWebApplicationContext(servletContext).getBean("beanName");
得到bean,然后做需要的操作,得到的bean需要转换成借口类而不是具体类,因为得到的是代理。

 

web.xml中的配置

<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方式:

 

1.创建servlet类

public class SystemLoadingServlet extends HttpServlet {
	/**
	 * Constructor of the object.
	 */
	public SystemLoadingServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); 
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		SystemParamObject.webNoticeDir=this.getServletContext().getRealPath("/") + "files/notice/";
		SystemParamObject.webDir=this.getServletContext().getRealPath("/") + "files/";
		
		IitemService itemService = (IitemService) WebApplicationContextUtils.getWebApplicationContext(this.getServletContext()).getBean("IItemService");
		
		try {
			itemService.addCreateItemXml();
			itemService.addCreateKjfsXml();
		} catch (Exception e) {
			throw new ServletException("生成栏目或快捷方式xml文件异常", e);
		}
	}
}

 

 

2.配置web.xml 注意load-on-startup中的值

 

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/conf/applicationContext-*.xml</param-value>
  </context-param>
  <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

<filter>
  	<filter-name>struts2</filter-name>
  	<filter-class>
  		org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  	</filter-class>
  	<init-param>
  		<param-name>config</param-name>
  		<param-value>struts-default.xml,struts-plugin.xml,../conf/struts.xml</param-value>
  	</init-param>
  </filter>
  <filter-mapping>
  	<filter-name>struts2</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <servlet>
  	<servlet-name>initWebDir</servlet-name>
  	<servlet-class>javacode.com.greatchn.tax.servlet.SystemLoadingServlet</servlet-class>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>initWebDir</servlet-name>
  	<url-pattern>/servlet/initWeb</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>

 

这样在项目启动后会就会在sevlet初始化时得到spring的bean并做相应操作

 

参考http://www.iteye.com/problems/14886

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值