• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Spring AOP. How to get private variable?

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abhay Agarwal,
I try this, but in target I can use only public methods and variables...
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Live ordinary life in an extraordinary way. Details embedded in this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic