Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

111
Visualizações
¿Es posible llegar a la anotación del método bean desde dentro de bean?

Supongamos que estoy inicializando bean con el siguiente código:

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

¿Puedo saber algo sobre la anotación @MyAnnotation a partir del constructor de withing bean o de los métodos de withing bean?

No desde dentro del método myBean() , lo cual es obvio.

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Bueno, probablemente no sea la mejor manera o incluso la correcta, pero mi primera suposición es usar el seguimiento de pila actual y parece funcionar para mí:

 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); } }

El resultado muestra claramente que obtuve acceso a mi anotación dentro del constructor del objeto cuando se llamó desde el método anotado:

 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. []
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda