Spring AOP pointcut expression matching caller's context [duplicate] - spring

This question already has an answer here:
Intercept a method only if that method is called from a certain class?
(1 answer)
Closed 1 year ago.
I am using Spring AOP. I want my target method be matched only when invoked from a certain package. For example, let's assume my target method is com.domain.target.MyService.run(), and that it can be accessed from anywhere in my project. However, I want the pointcut to trigger only when the method is invoked from within a certain package, say com.domain.caller.*.
Is this something doable?

Yes, it's able to advice with package.
We could also match any type within the package or a sub-package.
#Pointcut("within(com.domain.caller)")
#Pointcut("within(com.domain.caller.*)")
You can find more detail:
https://www.baeldung.com/spring-aop-pointcut-tutorial

Related

Best practice to call a Repository class method in another Controller-Service-Repository in Spring Boot?

Image Description:
The image represents the backend coding of my application designed in MDD architecture, say chan 1 corresponds to model Complaint and chain 2 corresponds to model Location.
Working:
The client makes request to backend at Location controller passing an argument 'complaintId', and expects a Location corresponding to it. This work is to be done by method in Complaint repository using an inner join query.
Question:
My question is how should I call this method:
C2-S2-R1?
C2-S2-S1-R1?
C2-S1-R1?
(NOTE: Though there are many patterns described already in SO, solution to this question wasn't found. I would also appreciate if anyone could post an already answered link here. ty)

Create custom RxJs Subject

Question
Is there an official recommended way to create a custom RxJs Subject?
Use Case
I have a need for a QueueSubject, i.e. a Subject that queues all values passed to its next method until there is a subscriber. This is different from the built-in ReplaySubject because the ReplaySubject does not clear its buffer upon a subscription.
What I have learned so far
An exact implementation of what I need is available in this GitHub project by James Pike. The reason for my question despite this perfectly available solution is that the _subscribe method is an internal method. It is even marked as #deprecated, therefore if a linter is used, a linter rule exception needs to be added to the class to suppress the deprecation warning.
I did not find anything in the documentation about how to create a custom Subject.
You can use any Subject implementation as a reference for your own custom one, for example this one on Github.
Concerning _subscribe: You can override it with your custom class, but never call it directly from an outside consumer class (this is why it is annotated with #deprecated). The function is called by the Subject class internally following the Template Method Pattern.
In summary: Your linked implementation looks valid to me.

When inheriting from a class, its inner classes still inherit from the parent [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
When making a subclass, its inner classes will always subclass from the parent, not its child.
class Create
class Draft < self
# code
class Update < Create
# Update::Draft's is implicitly created, but its parent is Create, not `Update`. Why?
Create::Draft.superclass will return Create. That's fine. But Update::Draft.superclass will return Create too. I want Update::Draft to inherit from Update class.
Ruby doesn't have nested classes, which also means it doesn't have virtual classes (obviously, since virtual classes are a special case of nested classes). If you want to use virtual classes, you need to use a language which supports them, such as Beta, gBeta, or newspeak (I don't know of any others).
Since virtual classes are nested classes that are virtual, you can sort-of fake them in Ruby with methods (which are nested inside classes and virtual) returning classes (which are first-class objects in Ruby, and thus can be returned by methods).
That still doesn't solve the problem that the superclass expression is evaluated only once, during class declaration, and not everytime for every message send, though. Again, newspeak does this, so you could just use that.
There are other problems with your code. For example, you somehow want an Update::Draft class to magically appear. If you want a fourth class, you need to define a fourth class. There is no fourth class. There isn't even a fourth constant. It's just constant lookup rules. Update doesn't have a constant named Draft, so Ruby continues looking in the lexically enclosing class (which is Object), which also doesn't have a constant named Draft, so it continues looking in the superclass and finds it there. Nothing appeared. It was always there.
Since there is only one class, it cannot have to different superclasses at different times, you really need to classes.
What you basically want is class hierarchy inheritance, which is a feature enabled by virtual classes. newspeak's parser combinator library is a great example of the use of class hierarchy inheritance.

How to invoke a method in Ruby when the method name is dynamic? [duplicate]

This question already has answers here:
How to call methods dynamically based on their name? [duplicate]
(5 answers)
Closed 8 years ago.
#video = Video.new
thumb = "video_thumbnail"
#video_thumbnail = #video.video_thumbnail ## works fine
#video_thumbnail = #video.thumb ## undefined method `thumb'
How can I call the video_thumbnail method dynamically?
In Ruby, you can use send similar to this:
thumb = :video_thumbnail
#video_thumbnail = #video.send(thumb)
However, you should be aware of the potential security implications of this approach. If the contents of the thumb variable is in any way controllable by the user (e.g. by setting it from params), any user might have the possibility to call arbitrary methods on your video object. And given the large amount of meta-programming in Rails, you could easily have the ability to call and run arbitrary code that way. Because of that, you should make sure, that you tightly control the contents of the thumb variable.
If possible, you should user other means to call the methods, e.g. by using separate controller actions and proper routes to call them.
You have send() to dynamically invoke methods (and __send__ if the object may have overriden send)
http://ruby-doc.org/core-2.0/Object.html#method-i-send

Aspect to trap Controller creation in Roo project - how to?

I would like my first Aspect in a Roo project to run the advice when a web controller starts up. But I cant get the pointcut to match.
The controllers have a class name starting Cfx. I have tried with the following form:
pointcut setBrand() : initialization(Cfx*.new (..));
before() : setBrand()
{
log.info("xxxxxxxxxxxx setting brand");
}
As well as "initialization" I have tried (from the book AspectJ Cookbook) call(Signature) with new keyword, preinitialization, staticinitialization. What is the formula?
Maybe this is related - the Roo aspects do not have this form - no pointcut for example. How are they working? Where is this documented?
Thanks
PS apologies, this is a re-post. I posted this to the Spring Roo forum but got no response. http://forum.springsource.org/showthread.php?129374-Aspect-to-trap-Controller-creation-how-to
I know next to nothing about Roo or Spring, but some AspectJ, so I am going to answer your question from an AspectJ perspective only, assuming that you are an AOP newbie (sorry if my assumption is incorrect):
If you want to do something when a class is loaded, use a staticinitialization(TypePat) pointcut.
If you want to do something when an object (instance) is created, use something like execution(ConstructorPat). The initialization is for special purposes and preinitialization is needed even more rarely. I am assuming that the first one will do for you, not knowing your exact purpose.
Further assuming that something like execution(Cfx*.new (..)) is basically the thing you want, I suggest you look at possible errors or warnings like "advice defined in ... has not been applied [Xlint:adviceDidNotMatch]", because it might just be a pointcut matching issue. Please note that the type pattern you use assumes the matched constructors are in the same package as the aspect and that they have standard visibility (not public or anything else). So unless there is a class-loading issue, maybe you just want to specify more exactly (or more generally) what you want to match. Examples:
com.bigboxco.my_app.Cfx*.new(..)
com.bigboxco..Cfx*.new(..)
public com.bigboxco..Cfx*.new(..)
!private com.bigboxco..Cfx*.new(..)
* com.bigboxco..Cfx*.new(..)
A good strategy could be trying to match one of your constructors by replicating its exact signature and using its fully qualified class name, then working on from that point to make it more general.
Update: I know you can do a web search by yourself, but anyway here are some useful links:
AspectJ quick reference
AspectJ language semantics with topics about signatures, matching etc.

Resources