title: Spring AOP - enable @Aspect programmatically
body: <p>I have an aspect </p>
  
  <pre><code>@Aspect
  @Log4j2
  public class AnnotationBroker {
  
      @AfterReturning(pointcut = " @annotation(p) ", returning = "msg")
      public void onProducerReturn(Produce p, Object msg) throws IOException {
          for (String queue : p.queue()) {
              processCallback(msg, "", queue, p.onFailure(), p.onSuccess());
          }
      }
  
  }
  </code></pre>
  
  <p>I enabled it by adding it to the config class</p>
  
  <pre><code>@Configuration
  @EnableAspectJAutoProxy
  public class AppConfig {
  
      @Value("${amqp.default ?:}")
      String defaultNamespace;
  
      @Bean
      ConnectionFactory defaultMqConnFactory() {
          return ConnectionFactoryBuilder.build(defaultNamespace);
      }
  
      @Bean
      AnnotationBroker broker(){
          AnnotationBroker broker = new AnnotationBroker();
          broker.setTemplate(new RabbitTemplate(defaultMqConnFactory()));
          return broker;
      }
  }
  </code></pre>
  
  <p>It works fine, butif I register this bean by adding it to beanFactory programmatically, the @Aspect annotation is not working.</p>
  
  <pre><code>@Configuration
  @EnableAspectJAutoProxy
  public class AppConfig {
  
      @Autowired
      ConfigurableApplicationContext ctx;
  
      @PostConstruct
      void broker2() throws IOException, TimeoutException {
          Map&lt;String, RabbitTemplate&gt; template = ctx.getBeansOfType(RabbitTemplate.class);
          ConfigurableListableBeanFactory beanFactory = ctx.getBeanFactory();
          for (Entry&lt;String, RabbitTemplate&gt; entry : template.entrySet()) {
              AnnotationBroker broker = new AnnotationBroker();
              broker.setTemplate(entry.getValue());
              beanFactory.registerSingleton(entry.getKey() + ".broker", broker);
              beanFactory.initializeBean(broker, entry.getKey() + ".broker");
          }
      }
  }
  </code></pre>
  
  <p>any suggestions? 
  Thanks in advance.</p>