Reputation: 13209
i want to intercept method from a non-proxied object.
I have a class instance MyClass myClassInstance=new MyClass()
and i want to intercept call of myClassInstance
methods.
I know all works good with proxies, but i need aspect on non proxied.
Is this possible?
Thanks.
Upvotes: 3
Views: 2726
Reputation: 898
You can enable load time weaving in Spring. Then the class loader will weave whatever aspects you define into your classes when they are loaded - regardless of whether they are being created by Spring.
You'll need to enable LTW by adding this line to your context file:
Upvotes: 0
Reputation: 597314
It is possible, but it is not desirable.
You can use @Configurable
and AspectJ will intercept calls. Read the 7.8.1 Using AspectJ to dependency inject domain objects with Spring section of the docs about it.
In short - AspectJ plugs some "magic" either compile-time or load-time (with a respective weaver), which modifies the classes so that they become part of the spring context.
As of why I think it is undesirable - this is dark magic that happens by modifying the classes' structure. It's not an object-oriented approach, and I think it will be hard to support. Imagine 6 months later a colleague of yours is debugging a nasty issue. It could take days before he realizes this magic is happening. This opinion is subjection though. The functionality is there and if you need it - use it.
Upvotes: 5
Reputation: 14661
Using Spring, I do not believe so (I appear to be wrong), using AspectJ I believe you can as long as you compile all the code with the AspectJ compiler. However I have never tried it.
Upvotes: 1