How to implement AOP to set kafka offset to latest? - java

I want to implement an Aspect so that Kafka read message from the latest offset.
I want to create and annotation like below:
#Target(ElementType.METHOD)
#Retention(RetentionPolicy.RUNTIME)
public #interface MyAnnotation {
}
So if I use this annotation with any of the #KafkaListener method then this listener should read message from the latest one only.
I don't know how to write the AOP for this scenario.

Related

Function reference in a Java annotation parameter

I have a jersey resource method annotated with custom annotation #InBound(responseLog = true, requestLog = true). The annotation basically logs request and response for each incoming request into a db table, so that these can be looked later for debugging.
Definition of the annotation is as below:
#NameBinding
#Retention(RetentionPolicy.RUNTIME)
#Target({ElementType.METHOD, ElementType.TYPE})
public #interface InBound {
boolean requestLog() default false;
boolean responseLog() default false;
}
I want to accept one more argument in annotation which will be basically an object of Type java.util.function.Function to do an awesome task(find a unique key from the request).
Can annotations accept Function in parameters? If yes, Can you please help with an example?

Why is Spring's #Component Annotation RUNTIME?

I am wondering, why Spring's implementation of the #Component Annotation has RetentionPolicy.RUNTIME.
Here's the official implementation from github
#Target(ElementType.TYPE)
#Retention(RetentionPolicy.RUNTIME)
#Documented
#Indexed
public #interface Component {
String value() default "";
}
In my thought, the scanning for all Spring-Components annotated with #Component would be done at compile-time and all the results (annotated classes) would somehow be stored inside the ApplicationContext. I guess I am wrong, but why does this annotation need to be RetentionPolicy.Runtime?
The component scanning is done at the startup, not at the compilation.
Also, the aspects (AOP) are created at the runtime

Executing Spring AOP aspects for annotations aliased with #AliasFor

The idea is to create annotations hierarchy (similar to #Service, #Component etc) using #AliasFor annotation. This should give me the possibility to define aspect, that would execute on parent annotation, and every alias of it. But somehow it doesn't work for me.
#ComponentScan is fine, #EnableAspectJAutoProxy is set.
Example:
#Retention(RetentionPolicy.RUNTIME)
#Target({ElementType.TYPE, ElementType.METHOD})
public #interface ParentAnnotation {
}
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.METHOD)
#ParentAnnotation
public #interface ChildAnnotation {
#AliasFor(annotation = ParentAnnotation.class)
String value() default "";
}
#Aspect
#Component
public class EventRecorderAspect {
#Around("#annotation(com.example.ParentAnnotation)")
public void exampleMethod(ProceedingJoinPoint joinPoint) throws Throwable {
// This should be executed for both #ParentAnnotation and #ChildAnnotation
}
}
#RestController
public class ExampleController {
#ChildAnnotation // This should result in executing aspect for every implementation
String controllerMethod();
}
UPDATE:
I've updated code, as #M.Deinum suggested in a comment below. But it still doesnt work.
AspectJ pointcut matching syntax, a subset of which is used by Spring AOP, is ignorant of meta annotations, even though withing the Spring framework as such there is support for it in other places. I think the closest you can get to specifying meta annotations in your pointcut is to do it explicitly up to the nesting level you require. See this answer for examples showing the syntax variants for both
class-level, e.g. within(#(#com.example.ParentAnnotation *) *),
method-level, e.g. execution(#(#com.example.ParentAnnotation *) * *(..))
annotations.
Update: #Ariel Grabijas asked in a follow-up comment:
So is there a way to somehow inherit annotations from interface method to class method?
Not in Java and also not by means of Spring AOP. But there is a workaround using native AspectJ inter-type definitions (ITD).

Spring Security add multiple meta annotation

I created a Spring Boot 2.3 application. I securited my REST endpoint and my services.
I created some meta annotations because I have many role. For example:
#Target({ElementType.METHOD, ElementType.TYPE})
#Retention(RetentionPolicy.RUNTIME)
#PreAuthorize("hasAnyRole('ROLE_CUSTOMER')")
public #interface IsCustomerUser {
}
and
#Target({ElementType.METHOD, ElementType.TYPE})
#Retention(RetentionPolicy.RUNTIME)
#PreAuthorize("hasAnyRole('ROLE_API')")
public #interface IsApiUser {
}
I've several of this meta annotations with many other roles. I thought I could add multiple meta annotations on my endpoints like this:
#IsCustomerUser
#IsApiUser
#GetMapping(path = "/tenants/current/avatar/{version}")
public ResponseEntity<?> getAvatar(#PathVariable("version") long version) {
//TODO some stuff
}
but it seems only one annotation is evalued. I see there is a feature request here. I'd like to know if you have some workaround in order to add multiple meta annotations.
It is very annoying be forced to create all permutations of my basic security annotation.
In fact I should create something like #IsAdminOrCustomerOrApiOrManagerOr.... and it's not really convenient.

Spring - how to rename #Bean

How do you define an annotation in Spring that is semantically equivalent to #Bean, but has another name?
(Why? I'm building a DSL in which the functionality would fit, but it would greatly benefit from naming the annotation more closely to the role it plays in the library).
Well... It was as easy as:
#Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
#Retention(RetentionPolicy.RUNTIME)
#Bean
public #interface CustomAnnotation {
}

Categories

Resources