aspose file tools*
The moose likes Spring and the fly likes How to pass information from target object to Advice Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "How to pass information from target object to Advice Object" Watch "How to pass information from target object to Advice Object" New topic
Author

How to pass information from target object to Advice Object

Dhanaprakash Muddineti
Greenhorn

Joined: Sep 28, 2005
Posts: 6
Hi,

Can you please help me in finding a good solution in passing information from target object to Advice object in Spring AOP. Please note that both the objects are singletons and hence I can't use class members variables on target object for passing the info.

Example:
----------
public class TargetObject {
public boolean advisedMethod(String para1) {
String decisionMaker = "Hello";
//Some Logic is done to set decisionMaker variable and based on its value true or false will be returned.
return "true";
}
}

public class AdviceObject implements MethodBeforeAdvice {
public void before(Method method, Object[] args, Object target) throws Throwable {
//I need decisionMaker here. How to obtain???
}
}

Remarks:
-----------
1. Both TargetObject and AdviceObject are singletons.
2. Due to #1, member variables on targetObject can't be used to hold "decisionMaker". This is for thread safety.
3. The signature of targetObject's advisedMethod can't be altered.

Requesting your help,
Dhana Prakash
Shak Smith
Greenhorn

Joined: Feb 19, 2010
Posts: 12
Not sure if I understood what you want. But have you tried doing this?


in the application context be sure to have this:

Remember that the singleton definition in Spring is not like the one in the gang of four. Check this link
http://static.springsource.org/spring/docs/2.0.x/reference/beans.html#beans-factory-scopes

Remember that you can be wiring beans in your application context file (it is better to do it there than calling "getBean").
Let me know if this works for you.
Dhanaprakash Muddineti
Greenhorn

Joined: Sep 28, 2005
Posts: 6
Hi Shak,

Thanks a lot for your reply. The given suggestion with "DecisionMaker" singleton class doesn't solve my problem. It will have concurrence issue in multithreading environment. For example, user1 sets some value on DecisionMaker. But by the time AdviceObject accesses this value, some other user (say user2) might change the value on DecisionMaker object. This is possible as DecisionMaker object is singleton.

Do you have any other suggestions?

Regards,
Dhanaprakash
Shak Smith
Greenhorn

Joined: Feb 19, 2010
Posts: 12
Sorry Dhanaprakash, I didn't see that you were using AOP. I believe that now I understand what you are trying to do.
Here's a complete example. Not sure if this is what you want so please let me know whether this is what you were looking for or not:

Here is the file called SecurityAspect.aj (the Aspect)



Here is the DecisionMaker.java class


Here is the main File


Here is the Application Context file:


Dhanaprakash Muddineti
Greenhorn

Joined: Sep 28, 2005
Posts: 6
Hi Shak,

Thank you very much for your help. In the given example, you are sending data from Advice Object to Target Object. But actually I need the reverse way. Data has to be shared from Target Object to Advice Object after executing Target Object's method. (It is AfterReturning pointout).

And another important point is the decisionMaker object (from Target Object) is not part of method signature. Hence it can't be accessed direclty from Advice Object. Please have a look at my originial question once.

Can you please help?

Thanks,
Dhana Prakash
Shak Smith
Greenhorn

Joined: Feb 19, 2010
Posts: 12
I believe that is not possible man. Found this link:
http://dev.eclipse.org/mhonarc/lists/aspectj-dev/msg02566.html
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

It is definitely possible. You can get complete context of everything you need in Spring AOP.

So for

@AfterReturning(value="MyPOintcutExpression"returning="whatever")
public void myAdviceMethod(MyObject returning){
Now I have access to the object that got returned by the target.
}

I could also get the actual target and the values that are in it. Either through the JoinPoint api, or via putting target(somethinghere) or args() or @Annotation.

Please read the pointcut expression language for more on getting context. Also, you create named pointcut expressions for those occasions when you need to reuse a pointcut expression for mutliple advice methods.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Shak Smith
Greenhorn

Joined: Feb 19, 2010
Posts: 12
Hello Mark,
The thing with Dhana's problem is that he wants to access a local variable, So lets keep in mind these three things:
-Is not a property
-Is not a returning value (Although I'm not sure, as you said this one is possible )
-It is not even a parameter of the method.

I've spent some time looking on how to do that, but I didn't find anything. If you have a complete example of what to do that, I'll really
appreciate that.

Regards
Dhanaprakash Muddineti
Greenhorn

Joined: Sep 28, 2005
Posts: 6
Hi Shak and Mark,

Thanks a lot for your help. As Shak clarified, the value/object that I have to reference at Advice is not part of Target Object's Method signature. For thread safety, I can't use instance variables on Target Object.

I am looking for something like AOPContext that can hold the value for single invocation (similar to Request Context in Web Environment). Currently I am in the process of writing my own Context Object that can hold the values in Thread Environment (using ThreadLocal Object). I am wondering whether I am reiventing the wheel.

Regards,
Dhana Prakash
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to pass information from target object to Advice Object