spring mvc怎么能在项目ear一启动的时候,就自动加载一个类的一个方法,高手们来看看啊
晓旭大军 2020-03-03 07:50:55 原来struts one里面struts-config.xml
<plug-in className="jp.sales.oio.oiobama.core.InitPlugin">
<set-property property="sideNaviConfig" value="/WEB-INF/Navigation.xml" />
<set-property property="roleConfig" value="/WEB-INF/Role.xml" />
<set-property property="log4jConfig" value="log4j.xml" />
</plug-in>
-------------------------------------------------------------------------------------------------------
InitPlugin.java
package jp.sales.oio.oiobama.core;
import java.io.InputStream;
import javax.servlet.ServletException;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;
import com.ibm.jp.ams.tools.core.global.CoreConstants;
import com.ibm.jp.ams.tools.core.logging.LogManager;
import com.ibm.jp.ams.tools.core.logging.Logger;
import com.ibm.jp.ams.web.navigationsupport.digester.NavigationDigester;
import com.ibm.jp.ams.web.navigationsupport.digester.RolesDigester;
import com.ibm.jp.ams.web.navigationsupport.entity.Navigation;
import com.ibm.jp.ams.web.navigationsupport.entity.Roles;
public class InitPlugin implements PlugIn {
private ActionServlet servlet = null;
/* ログ定義xmlファイル名 */
private String log4jConfig = null;
/* properties 表示非表示 ACL */
// private String propertiesAcl = null;
/* ナビゲーション定義xmlファイル名 */
private String sideNaviConfig = null;
private String roleConfig = null;
/**
* <p>Log Manager</p>
*/
private Logger logger = Logger.getLogger(InitPlugin.class);
/**
* Initial処理を実行します。<br/>
*
* @see PlugIn#init(ActionServlet, ModuleConfig)
*/
public void init(ActionServlet actionServlet, ModuleConfig moduleConfig) throws ServletException {
this.servlet = actionServlet;
try {
// ログマネージャーinitialize
initLogManager();
// サイドナビファイル読み込み
initNavigater();
// Role定義読み込み
initRole();
// PropertiesAccessControl init
//initPropertiesACL();
} catch (Exception e) {
throw new ServletException(e);
}
}
/**
* ログマネージャーの初期化処理を行います。
* @throws Exception
*/
private void initLogManager() throws Exception {
LogManager.init(log4jConfig);
}
通过struts-config.xml里面定义的plug in,那样随着项目启动,自动加载一个类比如"jp.sales.oio.oiobama.core.InitPlugin,这个InitPlugin类又继承了import org.apache.struts.action.PlugIn; 所以我认为这个InitPlugin类的public void init(ActionServlet actionServlet, ModuleConfig moduleConfig) throws ServletException 是自动加载的,
----------------------------------------------------------------------------------------------------------------------
问一个问题,现在这个struts 1.1项目改为spring mvc了
spring mvc有没有类似的方法,
Spring MVC 能不能在项目一启动的时候就自动调用一个类(Controller) 的一个方法,
为什么有这个需求呢,因为我的客户端.net程序要求调用java服务器端的webservice,而webservice里面的被调用的方法会用到这个InitPlugin类的一个实例,
如果这个InitPlugin类在拦截器被初始化是很正常的,但是客户端调用webservice里面方法的时候就麻烦了,客户端不能等服务器端的用户登录web,启用拦截器,然后在启用这个类啊,
谁来救救我啊,
看我写的spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--scan package-->
<!--<context:component-scan base-package="cn.java.controller"/>-->
<context:component-scan base-package="jp.sales.oio.................."/>
<!--driven annoatation-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--view analyze-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/"></property>
<property name="suffix" value=".jsp"/>
</bean>
<!--file upload-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- encording -->
<property name="defaultEncoding" value="UTF-8"></property>
<!-- file sieze -->
<property name="maxUploadSize" value="10485760"></property>
</bean>
<bean class="jp.sales.oio.oiobama.core.InitPlugin"/>
-------------------------------------
<bean class="jp.sales.oio.oiobama.core.InitPlugin"/>
这个bean我可以自动实例化么,然后我在实例化里写上我要自动随项目启动的方法
public class InitPlugin {//implements PlugIn
//private ActionServlet servlet = null;
private Controller controller = null;
/* 0グ定義xmlファイル名 */
private String log4jConfig = null;
/* properties 表示非表示 ACL */
// private String propertiesAcl = null;
/* 0ナビゲーション定義xmlファイル名 */
private String sideNaviConfig = null;
private String roleConfig = null;
/**
* <p>Log Manager</p>
*/
private Logger logger = Logger.getLogger(InitPlugin.class);
public void InitPlugin() throws ServletException {
try {
// 0ログマネージャーinitialize
initLogManager();
// 0サイドナビファイル読み込み
initNavigater();
// Role定義読み込み
initRole();
// PropertiesAccessControl init
//initPropertiesACL();
} catch (Exception e) {
throw new ServletException(e);
}
}
可以不
或者大佬们提提宝贵意见,