how to inject springbean to a struts intercepter..is it possible

后端 未结 1 1335
既然无缘
既然无缘 2020-12-07 05:36

I am having a used defined intercepter,from the intercepter i want to make a db call through DAO layer, so how can i inject spring bean to struts intercepter.is it possible

相关标签:
1条回答
  • 2020-12-07 06:01

    EDIT

    Since there is no need to declare the Interceptor as Spring bean, I striked the unnecessary parts. Thanks to @AleksandrM for testing it.


    Exactly the way you do with Actions , with (if I remember well) the exception of declaring it in beans.xml because Interceptors don't extend ActionSupport (that is autowired by default) .

    web.xml

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    

    ApplicationContext.xml

    <bean id="daoServiceBean" 
       class="org.foo.bar.business.dao.DaoService"/>
    

    <bean id="myInterceptorBean" 
       class="org.foo.bar.presentation.interceptors.MyInterceptor"/>
    

    Struts.xml

    <constant name="struts.objectFactory" value="spring" />
    
    <package ...>
        <interceptors>
    

            <interceptor name="myInterceptor" class="myInterceptorBean" />
    

            <interceptor name="myInterceptor" 
                        class="org.foo.bar.presentation.interceptors.MyInterceptor"/>
    

    MyInterceptor.java

    private DaoService daoServiceBean; // Autowired by Spring
    

    Also read:

    • Spring Plugin
    • Spring and Struts2
    0 讨论(0)
提交回复
热议问题