-1

I am stuck in a situation where there are many Action classes all of them extend from a parent class TPDispatchAction. Now I have to basically set a session spring bean everytime any action method is called wherein i set the tenant id of the user.

The crude way will be to add the line of code setting the value in all the action methods of all action classes.

Is there any better way to do this?

Edit

Solved the problem using MethodBeforeAdvice AOP

Sumeet Sharma
  • 2,573
  • 1
  • 12
  • 24

4 Answers4

1

You can put an interceptor in front to set the bean in Session scope. Try implementing

org.springframework.web.servlet.HandlerInterceptor

to prehandle and posthandle of requests processing scenarios. You can set the tenantID in the prehandle method

Keerthivasan
  • 12,760
  • 2
  • 32
  • 53
0

You may want to create a method in the super class like

  //super class A
  A {

  public foo(){
    //create here whatever you want
    bar(); 
  }

  protected bar();
  }


  //extending class/action
  B:A{

    bar(){
      //your implementation
    }

  }

and whose call another method that is implemented by all other subclasses.

External object will only have to call and see the foo method, and there you will be able to to your initializations or postprocessing.

jalone
  • 1,953
  • 4
  • 27
  • 46
  • But in this case i have to implement bar() in all the child classes which will be repetitive as the line of code is exact everywhere. Also I have to call the bar() function from all the methods in the child class which wont reduce the effort. – Sumeet Sharma Jun 02 '14 at 10:31
0

Suggestion Use PostConstruct to inject.

@PostConstruct
void initializeCommon() {
    //set values here
 }
Abs
  • 3,902
  • 1
  • 31
  • 30
0

I resolved my problem using Spring AOP MethodBeforeAdvice

public void before(Method methodName, Object[] arguments, Object arg2)
            throws Throwable {
}

In this the methodName is the name of method which is called. arguments is the list of arguments of that method and arg2 is the returned object. So as HTTPRequest object is one of the objects in my action methods I am able to use the arguments and capture the session. (Basically I have integrated Spring into my project which is Struts 1.3 based)

In applicationContext.xml

<bean name="globalInterceptor"
    class="com.xyz.common.filters.GlobalRequestInterceptor"></bean>
<bean name="autoProxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
    <property name="beanNames">
        <list>
            <value>*</value>
        </list>
    </property>
    <property name="interceptorNames">
        <list>
            <value>globalInterceptor</value>                
        </list>
    </property>
</bean>

So this basically intercepts all the method calls in all the service beans.

Sumeet Sharma
  • 2,573
  • 1
  • 12
  • 24