簡體   English   中英

@PostConstruct 在 GlassFish 中部署錯誤

[英]@PostConstruct Wrong deploy in GlassFish

  • 我的項目基於 Spring 4.2.3.RELEASE
  • 之前用Tomcat,但是上次遷移項目ClassLoader有問題,於是把Tomcat 8換成GlassFish 4.1!
  • 在項目中有一個@Component("i18N")
  • 在我的對象調用中i18N有一個方法
  • 但是我要在 GlassFish 中部署文件site-1.3.0.0.war
  • 已經放錯了錯誤
  • 在其他項目中也有同樣的問題,但我沒有時間解決這個問題,現在我需要解決這個問題。

Error occurred during deployment: Exception while deploying the app 
[site-1.3.0.0] : The lifecycle method [init] must not throw a checked exception. Related annotation information: annotation 
[@javax.annotation.PostConstruct()] on annotated element [public void com.sys.resolver.SysResourceBundleRead.init() throws java.lang.IllegalAccessException,java.lang.InstantiationException,java.io.IOException,org.apache.taglibs.standard.lang.jstl.ELException] of type 
[METHOD]. Please see server.log for more details.

添加方法類

** @PostConstruct public void init() 拋出異常{

} **

正如錯誤消息所說,帶有@PostConstruct的方法不得拋出已檢查的異常。

因此,從您的方法中刪除throws Exception並在方法主體中捕獲它:

@PostConstruct
public void init() {

   try {
         // bla
   } catch (Exception x) {
        // do something
   }

}

在這里,我已經解決了在我的情況下有效的工作。

您可以首先通過添加帶有 metadata-complete="true" 的 web.xml 來解決此問題。 接下來,您需要確保您的上下文位於 Web 根目錄 /WEB-INF/ 中。

使用此 glassfish 可以加載所有 @PostConstructSpring 依賴項。

我通過在 web.xml 中添加metadata-complete="true"來解決,例如:

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" metadata-complete="true">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM