Java Struts 2.3:运行时添加拦截器

Java Struts 2.3:运行时添加拦截器,java,struts2,interceptor,struts2-interceptors,interceptorstack,Java,Struts2,Interceptor,Struts2 Interceptors,Interceptorstack,我有一项任务要在运行时添加/更改拦截器(使用插件,无法访问父配置) 在以前版本的Struts(2.0)中,这非常简单:类InterceptorStackConfig和ActionConfig具有方法addInterceptor和addInterceptors 在较新的版本(2.3)中,这些方法移到了Builder静态子类中,我不能像以前那样使用它们 所以这是一个问题。已经花了好几天的时间试图避免它。有人能帮忙吗 我前面的代码示例: public class IpLoggingIntercepto

我有一项任务要在运行时添加/更改拦截器(使用插件,无法访问父配置)

在以前版本的Struts(2.0)中,这非常简单:类InterceptorStackConfigActionConfig具有方法addInterceptoraddInterceptors

在较新的版本(2.3)中,这些方法移到了Builder静态子类中,我不能像以前那样使用它们

所以这是一个问题。已经花了好几天的时间试图避免它。有人能帮忙吗

我前面的代码示例:

public class IpLoggingInterceptorConfiguration implements ConfigurationProvider {

private Interceptor interceptor;
private Configuration configuration;

@Override
public void init(Configuration configuration) throws ConfigurationException {
    this.configuration = configuration;
}

@Override
public void loadPackages() throws ConfigurationException {

    for (Object packageConfigName : configuration.getPackageConfigNames()) {
        try {
            String name = (String) packageConfigName;
            PackageConfig packageConfig = configuration.getPackageConfig(name);
            updatePackage(packageConfig);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

public void updatePackage(PackageConfig packageConfig) {
    Map interceptorConfigs = packageConfig.getInterceptorConfigs();

    for (Object stack : interceptorConfigs.keySet()) {

        if (!(interceptorConfigs.get(stack) instanceof InterceptorStackConfig)) continue;

        InterceptorStackConfig interceptorStackConfig = (InterceptorStackConfig) interceptorConfigs.get(stack);

        InterceptorMapping interceptorMapping = new InterceptorMapping("iplogging", getInterceptor());

        List<InterceptorMapping> list = new ArrayList<InterceptorMapping>();
        list.addAll(interceptorStackConfig.getInterceptors());
        interceptorStackConfig.getInterceptors().clear();
        interceptorStackConfig.addInterceptor(interceptorMapping);
        interceptorStackConfig.addInterceptors(list);
    }

    for (String key : packageConfig.getActionConfigs().keySet()) {
        ActionConfig actionConfig = packageConfig.getActionConfigs().get(key);

        InterceptorMapping interceptorMapping = new InterceptorMapping("iplogging", getInterceptor());

        List<InterceptorMapping> list = new ArrayList<InterceptorMapping>();
        list.addAll(actionConfig.getInterceptors());
        actionConfig.getInterceptors().clear();
        actionConfig.addInterceptor(interceptorMapping);
        actionConfig.addInterceptors(list);
    }
}


@Override
public void destroy() {
}

@Override
public boolean needsReload() {
    return false;
}

@Override
public void register(ContainerBuilder arg0, LocatableProperties arg1)
        throws ConfigurationException {
}

public Interceptor getInterceptor() {
    return interceptor;
}

public void setInterceptor(Interceptor interceptor) {
    this.interceptor = interceptor;
}
}
公共类IpLoggingInterceptorConfiguration实现ConfigurationProvider{
专用拦截器;
私有配置;
@凌驾
public void init(配置)引发ConfigurationException{
this.configuration=配置;
}
@凌驾
public void loadPackages()引发ConfigurationException{
对于(对象packageConfigName:configuration.getPackageConfigNames()){
试一试{
字符串名称=(字符串)packageConfigName;
PackageConfig PackageConfig=configuration.getPackageConfig(名称);
更新包(packageConfig);
}捕获(例外e){
e、 printStackTrace();
}
}
}
公共无效更新包(PackageConfig PackageConfig){
Map interceptorconfig=packageConfig.getinterceptorconfig();
for(对象堆栈:interceptorConfigs.keySet()){
如果(!(interceptorconfig.get(stack)instanceof InterceptorStackConfig))继续;
InterceptorStackConfig InterceptorStackConfig=(InterceptorStackConfig)interceptorConfigs.get(堆栈);
InterceptorMapping InterceptorMapping=新的InterceptorMapping(“iplogging”,getInterceptor());
列表=新的ArrayList();
addAll(interceptorStackConfig.getInterceptors());
interceptorStackConfig.getInterceptors().clear();
interceptorStackConfig.addInterceptor(interceptorMapping);
interceptorStackConfig.addInterceptors(列表);
}
对于(字符串键:packageConfig.getActionConfigs().keySet()){
ActionConfig ActionConfig=packageConfig.getActionConfigs().get(键);
InterceptorMapping InterceptorMapping=新的InterceptorMapping(“iplogging”,getInterceptor());
列表=新的ArrayList();
addAll(actionConfig.getInterceptors());
actionConfig.getInterceptors().clear();
actionConfig.addInterceptor(截取映射);
actionConfig.addInterceptors(列表);
}
}
@凌驾
公共空间销毁(){
}
@凌驾
公共布尔需求加载(){
返回false;
}
@凌驾
公共无效寄存器(ContainerBuilder arg0,LocatableProperties arg1)
抛出配置异常{
}
公共拦截器getInterceptor(){
返回拦截器;
}
公共截取器(截取器截取器){
this.interceptor=拦截器;
}
}

我发现并知道,这个解决方案很难看,但很简单而且有效。。。也许有更好的

try {
            Field interceptorsListField=InterceptorStackConfig.class.getDeclaredField("interceptors");
            interceptorsListField.setAccessible(true);
            List<InterceptorMapping> interceptorsList= (List<InterceptorMapping>) interceptorsListField.get(interceptorStackConfig);

            List<InterceptorMapping> list = new ArrayList<>();
            list.add(interceptorMapping);
            list.addAll(interceptorStackConfig.getInterceptors());
            interceptorsListField.set(interceptorStackConfig,list);
        } catch (Exception e) {
            e.printStackTrace();
        }
试试看{
Field interceptorsListField=InterceptorStackConfig.class.getDeclaredField(“拦截器”);
interceptorsListField.setAccessible(true);
List interceptorsList=(List)interceptorsListField.get(interceptorStackConfig);
列表=新的ArrayList();
添加(截取映射);
addAll(interceptorStackConfig.getInterceptors());
interceptorsListField.set(interceptorStackConfig,list);
}捕获(例外e){
e、 printStackTrace();
}

我发现并知道,这个解决方案很难看,但很简单而且有效。。。也许有更好的

try {
            Field interceptorsListField=InterceptorStackConfig.class.getDeclaredField("interceptors");
            interceptorsListField.setAccessible(true);
            List<InterceptorMapping> interceptorsList= (List<InterceptorMapping>) interceptorsListField.get(interceptorStackConfig);

            List<InterceptorMapping> list = new ArrayList<>();
            list.add(interceptorMapping);
            list.addAll(interceptorStackConfig.getInterceptors());
            interceptorsListField.set(interceptorStackConfig,list);
        } catch (Exception e) {
            e.printStackTrace();
        }
试试看{
Field interceptorsListField=InterceptorStackConfig.class.getDeclaredField(“拦截器”);
interceptorsListField.setAccessible(true);
List interceptorsList=(List)interceptorsListField.get(interceptorStackConfig);
列表=新的ArrayList();
添加(截取映射);
addAll(interceptorStackConfig.getInterceptors());
interceptorsListField.set(interceptorStackConfig,list);
}捕获(例外e){
e、 printStackTrace();
}

因为现在interceptorStackConfig.getInterceptors()是不可修改的集合:(什么是次要版本?我认为修改是因为xwork的新版本行:如果您没有访问父配置的权限,如何运行您的项目?没有考虑太多您实际要做的事情,您知道您可以查看Spring AOP。如果您想要应用程序级日志记录,这几乎就是因为现在interceptorStackConfig.getInterceptors()是不可修改的集合:(什么是次要版本?我认为修改是因为xwork的新版本行:如果您没有访问父配置的权限,如何运行您的项目?没有考虑太多您实际要做的事情,您知道您可以查看Spring AOP。如果您想要应用程序级日志记录,这几乎就是f是一个横切关注点,因此它是用于AOP的典型示例。