• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Spring AOP. How to get private variable?

 
Sergey Deplake
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhay Agarwal,
I try this, but in target I can use only public methods and variables...
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18100
51
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sergey Deplake
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18100
51
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I find a way to get private variable from the class using package java.lang.reflect.*
To see example see link
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic