Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

115
Vistas
Is it possible to reach bean method's annotation from within bean?

Suppose I am initializing bean with the following code:

@Configuration 
MyConfig {
    @Bean
    @MyAnnotation // how to know this from bean constructor or method?
    MyBean myBean() {
       MyBean ans = new MyBean();
       /// setup ans
       return ans;
    }
}

Can I know from withing bean constructor or from withing bean's methods something about @MyAnnotation annotation?

Not from within myBean() method, which is obvious.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Well it's probably not the best or even correct way, but my first guess is to use current stack trace and it seems to work for me:

package yourpackage;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class AspectJRawTest {

    public static void main(String[] args) {
        System.out.println("custom annotation playground");

        ISomething something = new SomethingImpl();

        something.annotatedMethod();
        something.notAnnotatedMethod();
    }

}

interface ISomething {
    void annotatedMethod();

    void notAnnotatedMethod();
}

class SomethingImpl implements ISomething {
    @MyCustomAnnotation
    public void annotatedMethod() {
        System.out.println("I am annotated and something must be printed by an advice above.");
        CalledFromAnnotatedMethod ca = new CalledFromAnnotatedMethod();
    }

    public void notAnnotatedMethod() {
        System.out.println("I am not annotated and I will not get any special treatment.");
        CalledFromAnnotatedMethod ca = new CalledFromAnnotatedMethod();
    }
}

/**
 * Imagine this is your bean which needs to know if any annotations affected it's construction
 */
class CalledFromAnnotatedMethod {
    CalledFromAnnotatedMethod() {
        List<Annotation> ants = new ArrayList<Annotation>();
        for (StackTraceElement elt : Thread.currentThread().getStackTrace()) {
            try {
                Method m = Class.forName(elt.getClassName()).getMethod(elt.getMethodName());
                ants.addAll(Arrays.asList(m.getAnnotations()));
            } catch (ClassNotFoundException ignored) {
            } catch (NoSuchMethodException ignored) {
            }
        }
        System.out.println(ants);
    }
}

Output clearly shows that I got an access to my annotation within object's constructor when it was called from annotated method:

custom annotation playground
I am annotated and something must be printed by an advice above.
[@yourpackage.MyCustomAnnotation(isRun=true)]
I am not annotated and I will not get any special treatment.
[]
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda