복붙노트

[SPRING] 오래된 Struts 애플리케이션을 Spring 3.x와 통합하는 방법

SPRING

오래된 Struts 애플리케이션을 Spring 3.x와 통합하는 방법

Struts 1.x 응용 프로그램을 Spring 3.x와 통합하여 IOC의 이점을 누릴 수있는 방법을 궁금해했습니다.

해결법

  1. ==============================

    1.ContextLoaderPlugin을 사용하고 struts 컨트롤러를 다음과 같이 (struts-config.xml 내에서) processorClass "AutowiringRequestProcessor"로 설정하십시오 :

    ContextLoaderPlugin을 사용하고 struts 컨트롤러를 다음과 같이 (struts-config.xml 내에서) processorClass "AutowiringRequestProcessor"로 설정하십시오 :

    <controller>
        <set-property property="processorClass" value="org.springframework.web.struts.AutowiringRequestProcessor" />
    </controller>
    
    <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
        <set-property property="contextConfigLocation" value="/WEB-INF/config/action-servlet.xml"/>
    </plug-in>
    

    action-servlet.xml은 빈 빈 컨텍스트 파일이어야합니다.

    <beans></beans>
    

    web.xml의 ActionServlet에 다음 init 매개 변수를 추가하십시오.

    <init-param>
        <param-name>autowire</param-name>
        <param-value>byName</param-value>
    </init-param>
    

    일반 struts 액션을 작성하고 모든 액션에 주석 "@Component"를 추가하면 스프링이 액션을 발견하고 그 액션에서 빈을 생성합니다. "AutowiringRequestProcessor"는 struts-config.xml에 정의 된 동작 클래스와 일치하는 올바른 bean을 찾는다.

    setter에 @Autowired가 포함 된 다른 클래스를 Action 클래스에 삽입 할 수도 있습니다.

  2. ==============================

    2.ContextLoaderPlugIn을 사용하십시오. Spring 3.0에서는 더 이상 사용되지 않지만 여전히 그렇다.

    ContextLoaderPlugIn을 사용하십시오. Spring 3.0에서는 더 이상 사용되지 않지만 여전히 그렇다.

    저는 Struts 1.x와 Spring 2.5.x에서 사용했습니다 - 아름답게 작동했습니다. 이 통합 접근법은 Spring Bean을 Struts 액션에 직접 주입 할 수있게 해줍니다. 이것은 매우 깨끗하고 간단합니다.

  3. ==============================

    3.ApplicationContextAware 인터페이스를 사용하여 유틸리티 클래스가 ApplicationContext에 액세스하도록 할 수 있습니다.

    ApplicationContextAware 인터페이스를 사용하여 유틸리티 클래스가 ApplicationContext에 액세스하도록 할 수 있습니다.

    public class SpringUtil implements ApplicationContextAware {
        private static ApplicationContext applicationContext = null;
    
        public static Object getSpringBean(String beanName) {
            return applicationContext.getBean(beanName);
        }
    
        public void setApplicationContext(ApplicationContext appContext)
            throws BeansException {
            applicationContext = appContext;
       }
    }
    

    그런 다음 Action 클래스에서 정적 메서드에 액세스 할 수 있습니다.

    public class MyAction extends LookupDispatchAction {
        private MyService getMyService() {
            return (MyService) SpringUtil.getSpringBean("myService");
        }
    }
    

    가장 우아한 솔루션은 아니지만 작동합니다.

  4. ==============================

    4.이것은 작동하지 않습니다. ContextLoaderPlugin은 Spring 2.x 이후의 모든 Spring 버전에서 사용할 수 없습니다. Struts 1.x는 2.x 이후 Spring의 어떤 버전과도 호환되지 않습니다. Struts 1.x를 사용하기 위해 2.x 이후의 스프링을 설정하는 것은 불가능합니다. Spring Struts 업그레이드 버전을 다운 그레이드해야합니다. 3.x 이전의 최대 절전 모드 버전을 사용하고 있지 않으면 Spring을 다운 그레이드하는 것이 더 쉬울 것입니다.

    이것은 작동하지 않습니다. ContextLoaderPlugin은 Spring 2.x 이후의 모든 Spring 버전에서 사용할 수 없습니다. Struts 1.x는 2.x 이후 Spring의 어떤 버전과도 호환되지 않습니다. Struts 1.x를 사용하기 위해 2.x 이후의 스프링을 설정하는 것은 불가능합니다. Spring Struts 업그레이드 버전을 다운 그레이드해야합니다. 3.x 이전의 최대 절전 모드 버전을 사용하고 있지 않으면 Spring을 다운 그레이드하는 것이 더 쉬울 것입니다.

  5. from https://stackoverflow.com/questions/5835910/how-to-integrate-an-old-struts-application-with-spring-3-x by cc-by-sa and MIT license