【问题标题】:CDI @Decorator, Is there a way to deactivate a decorator at runtime?CDI @Decorator,有没有办法在运行时停用装饰器?
【发布时间】:2014-08-09 14:43:05
【问题描述】:

我使用以下解决方法来控制 @Decorator 的行为,因为我找不到停用它的方法。

    if (!FacesContext.getCurrentInstance().getViewRoot().getViewId()
            .endsWith("decoratorDemo.xhtml")) {
        return transInterBean.getTransactionalInsertRecords();
    } else {
        ... 
    }

有没有办法在运行时决定是否应该应用装饰器?


package com.cdi.decorators;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;

import javax.decorator.Decorator;
import javax.decorator.Delegate;
import javax.enterprise.inject.Any;
import javax.faces.context.FacesContext;
import javax.inject.Inject;

import com.cdi.cdibeans.TransactionalInterceptor;
import com.cdi.cdibeans.TransactionalInterceptorBean;

@Decorator
public abstract class TransactionalInterceptorDecorator implements
        TransactionalInterceptor {

    /**
     * 
     */
    private static final long serialVersionUID = -1191671082441891759L;

    @Inject
    @Delegate
    @Any
    TransactionalInterceptorBean transInterBean;

    @Override
    public ArrayList<String> getTransactionalInsertRecords()
            throws SQLException {

        ArrayList<String> records = new ArrayList<String>();

        if (!FacesContext.getCurrentInstance().getViewRoot().getViewId()
                .endsWith("decoratorDemo.xhtml")) {
            return transInterBean.getTransactionalInsertRecords();
        } else {

            Iterator<String> iter = transInterBean
                    .getTransactionalInsertRecords().iterator();

            while (iter.hasNext()) {
                String record = iter.next();
                records.add(record);
                records.add(">>>Decorator<<< Added record ... ");
            }

            if (records.isEmpty()) {
                records.add(">>>Decorator<<< Currently there are no records yet!");
            }

            return records;
        }
    }
}

【问题讨论】:

    标签: cdi decorator java-ee-7


    【解决方案1】:

    Deltaspike 有一个 exclude 功能......也许这会有所帮助,我没有尝试使用装饰器。

    【讨论】:

      猜你喜欢
      • 2018-05-31
      • 1970-01-01
      • 2016-01-20
      • 2019-06-16
      • 2012-05-11
      • 1970-01-01
      • 1970-01-01
      • 2022-01-09
      • 2020-04-14
      相关资源
      最近更新 更多