CDI @Decorator,有没有办法在运行时停用装饰器?

时间:2014-08-09 14:43:06

标签: cdi decorator java-ee-7

我使用以下解决方法来控制@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;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

Deltaspike有一个exclude功能......可能会有所帮助,我没有尝试过装饰器。

相关问题