第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用特殊的自動啟動servlet在啟動時初始化并共享應(yīng)用程序數(shù)據(jù)

使用特殊的自動啟動servlet在啟動時初始化并共享應(yīng)用程序數(shù)據(jù)

繁星淼淼 2019-06-04 16:23:34
使用特殊的自動啟動servlet在啟動時初始化并共享應(yīng)用程序數(shù)據(jù)我需要獲得一些配置,并連接到某個地方的外部資源/對象/系統(tǒng),并將其存儲在應(yīng)用程序范圍內(nèi)。我可以看到設(shè)置應(yīng)用程序的兩種方法:覆蓋init()在現(xiàn)有的servlet和必需的代碼中,并將所有構(gòu)造的對象保存在同一個servlet中。具有某種初始化servlet,并使用其init()去做這項工作。然后將創(chuàng)建的對象存儲在ServletContext與我的其他servlet共享它。以上哪種方法更好?是否有更好的方法在servlet之間共享對象?直接給他們打電話嗎.?
查看完整描述

1 回答

?
一只斗牛犬

TA貢獻(xiàn)1784條經(jīng)驗 獲得超2個贊

兩者都不是更好的方法。servlet的目的是偵聽HTTP事件(HTTP請求),而不是偵聽部署事件(啟動/關(guān)閉)。


CDI/JSF/EJB不可用?使用ServletContextListener

@WebListenerpublic class Config implements ServletContextListener {

    public void contextInitialized(ServletContextEvent event) {
        // Do stuff during webapp's startup.
    }

    public void contextDestroyed(ServletContextEvent event) {
        // Do stuff during webapp's shutdown.
    }}

如果您還沒有使用Servlet3.0并且無法升級,那么就不能使用@WebListener注釋,則需要手動在/WEB-INF/web.xml如下所示:

<listener>
    <listener-class>com.example.Config</listener-class></listener>

要在應(yīng)用程序作用域中存儲和獲取對象(以便所有servlet都可以訪問它們),請使用ServletContext#setAttribute()#getAttribute().

下面是一個讓偵聽器將自己存儲在應(yīng)用程序范圍中的示例:

    public void contextInitialized(ServletContextEvent event) {
        event.getServletContext().setAttribute("config", this);
        // ...
    }

然后在servlet中獲得它:

    protected void doGet(HttpServletRequest request, HttpServletResponse response) {
        Config config = (Config) getServletContext().getAttribute("config");
        // ...
    }

它也可以在JSPEL中使用${config}..所以你也可以做一個簡單的豆子。


有CDI嗎?使用@Observes在……上面ApplicationScoped.class

import javax.enterprise.context.ApplicationScoped;@ApplicationScopedpublic class Config {

    public void init(@Observes @Initialized(ApplicationScoped.class) ServletContext context) {
        // Do stuff during webapp's startup.
    }

    public void destroy(@Observes @Destroyed(ApplicationScoped.class) ServletContext context) {
        // Do stuff during webapp's shutdown.
    }}

這在servlet中是可用的@Inject..必要的話也要做@Named所以它可以通過#{config}在艾爾也是。

應(yīng)該指出,這是自CDI 1.1以來的新情況。如果您仍然使用CDI1.0并且無法升級,那么請選擇另一種方法。


JSF可用嗎?使用@ManagedBean(eager=true)

import javax.faces.bean.ManagedBeanimport javax.faces.bean.ApplicationScoped;@ManagedBean(eager=true)@ApplicationScopedpublic class Config {

    @PostConstruct
    public void init() {
        // Do stuff during webapp's startup.
    }

    @PreDestroy
    public void destroy() {
        // Do stuff during webapp's shutdown.
    }}

這是通過#{config}在艾爾也是。


EJB可用嗎?考慮@Startup@Singleton

@Startup@Singletonpublic class Config {

    @PostConstruct
    public void init() {
        // Do stuff during webapp's startup.
    }

    @PreDestroy
    public void destroy() {
        // Do stuff during webapp's shutdown.
    }}

這在servlet中是可用的@EJB..我說的是“考慮”,因為您不應(yīng)該為了啟動鉤子而濫用EJB。此外,@Singleton默認(rèn)情況下,讀/寫是鎖定的,主要用于事務(wù)處理,例如調(diào)度后臺作業(yè)。

另見:


查看完整回答
反對 回復(fù) 2019-06-04
  • 1 回答
  • 0 關(guān)注
  • 462 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號