Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Spring and the fly likes Spring AOP. How to get private variable? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Spring AOP. How to get private variable?" Watch "Spring AOP. How to get private variable?" New topic
Author

Spring AOP. How to get private variable?

Sergey Deplake
Greenhorn

Joined: Apr 13, 2010
Posts: 13
Hi all! Help me please. I use Spring AOP to control what shall my application do before some method. For Example, class, which method I want to control:

In this class I want to controll printName and printURL methods. I can do this, but I also want to get private variables name and url from this class. How I can do this? To control methods I use class, that implements MethodBeforeAdvice. May be there is some way get this variables in this class in before method? Or some other ways? Example of this class:

Example of spring-config.xml:
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1086
    
    1

hi

i am still learning Spring.
according to me, try to get the values from "target" object available in your "before" method but i am not sure.

~ abhay
Sergey Deplake
Greenhorn

Joined: Apr 13, 2010
Posts: 13
Abhay Agarwal,
I try this, but in target I can use only public methods and variables...
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16022
    
  20

Welcome to the JavaRanch, Sergey!

We have some small translation problems here, I think. Your use of the words "control" and "get private variable" don't make sense. Can you help us understand?

The java language itself forbids direct access of any member or class variable marked as "private" from any method outside of that class.


Customer surveys are for companies who didn't pay proper attention to begin with.
Sergey Deplake
Greenhorn

Joined: Apr 13, 2010
Posts: 13
Tim Holloway,

I mean that I want to control method, but while controlling I need to get private variable. I understand how to controll method, but have problems to get this variable and, for example, change it in this method. (In this example I can set value to it by setter method, but in my project there is no setter method for private variable) This is only simply example to say what I want easialy. Do you now understand what I want?

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16022
    
  20

Sergey Koval wrote:Tim Holloway,

I mean that I want to control method, but while controlling I need to get private variable. I understand how to controll method, but have problems to get this variable and, for example, change it in this method. (In this example I can set value to it by setter method, but in my project there is no setter method for private variable) This is only simply example to say what I want easialy. Do you now understand what I want?



I'm afraid I don't understand what you mean when you say you want to "control method". In Java terminology, execution of the method is done when you "invoke" method, although the more general Computer Science term would be to "call" the method. I'm not sure if that's what you mean, however, since Spring "controls" javabeans, although I wouldn't say that that's controlling their methods.

If a variable is a private variable, it cannot be seen (as an object), read, or changed from any code outside that class. That is not subject to override, since it's part of the definition of the Java language. You can access the private variable using a "get" property method. Or for that matter, any other method internal to the class that wishes to read/write that variable. However, the variable itself remains inaccessible to outsiders.
Sergey Deplake
Greenhorn

Joined: Apr 13, 2010
Posts: 13
I find a way to get private variable from the class using package java.lang.reflect.*
To see example see link
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Spring AOP. How to get private variable?