Struts 1:如何在不使用DI容器的情况下将依赖项注入Action类?

时间:2019-09-19 19:08:20

标签: java struts struts-1

一个客户雇用我们来维护一个非常老的Struts 1 Web应用程序。对于功能请求,我们需要实现一个依赖于Service类的新Action。原始代码库主要由意大利面条代码组成,不使用任何依赖项注入容器。我们正在缓慢地重构所有内容以遵循现代标准,但是现在我们必须处理原始代码库。

如何在不引入依赖项容器的情况下将Service注入Action类?

package com.example.action;

import com.example.service.ExampleService;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public final class ExampleAction extends Action {
    private ExampleService exampleService;

    // This Action cannot be instantiated by Struts because of its dependency! :(
    public ExampleAction(ExampleService exampleService) {
        this.exampleService = exampleService;
    }

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        // ...

        exampleService.doSomething();

        // ...
    }
}

我发现了很多有关如何将Spring容器与Struts 1集成的信息,但是我找不到有关如何手动操纵Strut的Action实例化过程的任何信息。我真的不想重构,而不必让新功能正常运行,因此,我希望使用一种快速而又肮脏的解决方案,因为我只是干扰Strut的Action实例化过程来手动注入我的服务。

0 个答案:

没有答案
相关问题