簡體   English   中英

CXF 攔截器階段因缺少階段聲明而被跳過

[英]CXF Interceptor phases being skipped for missing phase declaration

已經在這個方面工作了一段時間,但沒有任何運氣。

我正在從 Spring 3.2.9 升級到 4.2.6。 我的第一步是升級到 cxf-core 3.1.6,我沒有對應用程序產生任何問題。

在升級所有 spring 依賴項時,我遇到了攔截器設置的問題。 以下是基礎知識:

攔截器

public class MyInterceptor extends AbstractPhaseInterceptor<Message>{

private static final org.slf4j.Logger logger = LoggerFactory.getLogger(MyInterceptor.class);

public MyInterceptor() {
    super(Phase.PRE_INVOKE);
    addAfter(HolderInInterceptor.class.getName());
}

@PostConstruct
public void display() {
    logger.warn(this.getPhase());
}


@Override
public void handleMessage(Message message) throws Fault {
    ....

cxfContext.xml

<bean id="contentInInterceptor" class="com.MyInterceptor">
</bean>

<cxf:bus>
    <cxf:inInterceptors>
        <ref bean="contentInInterceptor"/>
    </cxf:inInterceptors>


</cxf:bus>

以及它被添加到 web.xml 中。 運行時,我看到記錄的信息顯示攔截器是在預調用階段創建的(我添加了這個來驗證我沒有發瘋)。 但是在調用服務時,PhaseInterceptor 鏈跳過了未聲明階段的攔截器:

日志:

2016-05-31 16:08:28,208 [localhost-startStop-1] [] 警告 c.MyInterceptor - 預調用

2016-05-31 16:10:14,552 [http-bio-8080-exec-1] [] 警告 oacpPhaseInterceptorChain - 跳過攔截器 com.MyInterceptor$$EnhancerBySpringCGLIB$$1afa70e1:缺少階段聲明。

這是攔截 jaxws 服務器調用。 同樣,我所做的所有導致問題的更改都是更新到最新版本的 Spring。 看起來好像 cxf 正在使用一個從未從 bean 生成的單獨的攔截器。

編輯

我相信我已經找到了問題,但我再次不確定修復的位置。 Spring 4 代理在創建代理時不會調用構造函數兩次(即它使用構造函數創建原始 bean,但是 CGLIB 創建的 bean 代理不調用構造函數)。 這會導致攔截器的相位為空。

我在課堂上更改了我的 @PostContsruct 以確認這一點:

    @PostConstruct
public void display() {
    logger.warn(this.getPhase());
    MyInterceptor myInterceptor = (MyInterceptor) appContext.getBean("contentInInterceptor");
    logger.warn("Bean phase is: " + myInterceptor.getPhase());
}

日志顯示 bean 創建階段與代理階段:

2016-06-01 10:36:52,829 [localhost-startStop-1] [] 警告 c.MyInterceptor - 預調用

2016-06-01 10:36:52,839 [localhost-startStop-1] [] WARN c.MyInterceptor - Bean 階段是:空

好吧,我現在想出了一個方法來解決這個問題,但這不是最好的實現。

基本上我最終要做的是創建一個抽象類來實現 PhaseInterceptor 並且所有這些實現的方法都被設置為抽象類。

然后我的攔截器從那個類擴展並有自己的階段/id/before/after 變量。 當變量在 bean 的直接類中不可用時,似乎 Spring 的 bean 代理設置可能存在問題。 至少這就是我在這種情況下發現的。

同樣,不確定這是否是最佳解決方案,但這會使攔截器再次工作。

你說攔截器正在攔截jaxws服務器調用。 所以我的假設是,這是撥出電話。

Phase.PRE_INVOKE用於傳入消息。 使用其他階段,如Phase.PRE_STREAM用於傳出消息

有關階段的文檔,請參閱

http://cxf.apache.org/docs/interceptors.html

暫無
暫無

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

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