原來struts one里面struts-config.xml
<plug-in className="jp.sales.oio.oiobama.core.InitPlugin">
<set-property property="sideNaviConfig" value="https://bbs.csdn.net/WEB-INF/Navigation.xml" />
<set-property property="roleConfig" value="https://bbs.csdn.net/WEB-INF/Role.xml" />
<set-property property="log4jConfig" value="https://bbs.csdn.net/topics/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="https://bbs.csdn.net/WEB-INF/page/"></property>
<property name="suffix" value="https://bbs.csdn.net/topics/.jsp"/>
</bean>
<!--file upload-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- encording -->
<property name="defaultEncoding" value="https://bbs.csdn.net/topics/UTF-8"></property>
<!-- file sieze -->
<property name="maxUploadSize" value="https://bbs.csdn.net/topics/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);
}
}
可以不
或者大佬們提提寶貴意見,
uj5u.com熱心網友回復:
uj5u.com熱心網友回復:
uj5u.com熱心網友回復:
我在自己定義的springmvc.xml 里面追加定義<bean class="jp.sales.oio.oiobama.core.InitPlugin"/>然后在InitPlugin.java的構造方法里面追加我要呼叫的方法initRole();
public void InitPlugin() throws ServletException {
System.out.println("20200303 springmvc.xml InitPlugin run");
try {
// 0ログマネージャーinitialize
initLogManager();
// 0サイドナビファイル読み込み
initNavigater();
// Role定義読み込み
initRole();
// PropertiesAccessControl init
//initPropertiesACL();
} catch (Exception e) {
throw new ServletException(e);
}
}
為什么沒有起作用呢,就是專案啟動的時候,這個構造方法沒有被呼叫起來,為什么?????????????????????????
uj5u.com熱心網友回復:
你可以試試用spring事件,當spring容器初始化完成,會發布一個事件ContextStartedEvent,你可以寫一個監聽器監聽這個事件,然后做自己要做的事情。uj5u.com熱心網友回復:
你可以做我的粉絲。老弟
uj5u.com熱心網友回復:
跨越5個月的對話 ,其實不用監聽器也行 比如寫個過濾器 或 servlet 統配 * 在靜態塊寫東西就行了
當訪問 任意地址 必然加載這個類
uj5u.com熱心網友回復:
恕我直言public void InitPlugin() throws ServletException
這是一個構造方法?
uj5u.com熱心網友回復:
import javax.servlet.Filter;
public class StartFilter implements Filter{
private final static Logger logger = Logger.getLogger(StartFilter.class);
public void init(FilterConfig fc) throws ServletException {
new Thread(new Runnable() {
@Override
public void run() {
//TODO you want
}
}).start();
}
public void destroy() {
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
}
}
然后在web.xml 中
<filter>
<filter-name>startFilter</filter-name>
<filter-class>xxxx..StartFilter</filter-class>
</filter>
uj5u.com熱心網友回復:
百度一下spring中的@PostConstruct的注解。可以在專案啟動時執行注解的方法。uj5u.com熱心網友回復:
比樓上的來晚一步,樓上的就很方便了.或者實作一些介面(啟動后執行)重寫下就行uj5u.com熱心網友回復:
可以用aspectJ 注解式切面,用@Before 在方法執行前加載類。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/37910.html
標籤:Web 開發
