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

猜你喜欢

转载自mib168.iteye.com/blog/1445708