Github user gavlyukovskiy commented on a diff in the pull request:

    https://github.com/apache/incubator-griffin/pull/444#discussion_r231981734
  
    --- Diff: 
service/src/main/java/org/apache/griffin/core/event/GriffinEventListeners.java 
---
    @@ -0,0 +1,50 @@
    +package org.apache.griffin.core.event;
    +
    +import java.util.ArrayList;
    +import java.util.List;
    +
    +import org.springframework.beans.BeansException;
    +import org.springframework.beans.factory.annotation.Autowired;
    +import org.springframework.beans.factory.annotation.Value;
    +import org.springframework.boot.context.properties.ConfigurationProperties;
    +import org.springframework.context.ApplicationContext;
    +import org.springframework.context.annotation.Bean;
    +import org.springframework.context.annotation.Configuration;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +@Configuration
    +public class GriffinEventListeners {
    +    private static final Logger LOGGER = LoggerFactory
    +            .getLogger(GriffinEventListeners.class);
    +
    +    @Autowired
    +    private ApplicationContext context;
    +    @Value("#{'${internal.event.listeners}'.split(',')}")
    +    private List<String> listeners;
    +
    +    @Bean
    +    public List<GriffinHook> getListenersRegistered() {
    --- End diff --
    
    I see that you getting beans by names from the context, am I right that 
you're trying to declare only beans that user has specified in the property? I 
think it will not work, because `@Autowired List<GriffinHook> eventListeners` 
will always inject all beans of given type from the context.
    For example if you do:
    ```
        @Bean
        public static List<GriffinHook> list() {
            return Arrays.asList(
                    new GriffinHook("1"),
                    new GriffinHook("2")
            );
        }
    
        @Bean
        public static GriffinHook hook3() {
            return new GriffinHook("3");
        }
    
        @Autowired
        private final List<GriffinHook> griffinHooks;
    ```
    in this case spring will try to inject list of beans of given type - list 
containing only `hook3`.
    
    Instead I would propose to remove this bean declaration and make changes in 
`GriffinEventManager` to get all beans from context and use only beans 
specified in the config:
    ```
    public class GriffinEventManager {
    
        @Autowired
        private ApplicationContext applicationContext;
        @Value("#{'${internal.event.listeners}'.split(',')}")
        private Set<String> enabledListeners;
    
        private List<GriffinHook> eventListeners;
    
        @PostConstruct
        void initializeListeners() {
            List<GriffinHook> eventListeners = new ArrayList<>();
            applicationContext.getBeansOfType(GriffinHook.class)
                    .forEach((beanName, listener) -> {
                        if (enabledListeners.contains(beanName)) {
                            eventListeners.add(listener);
                        }
                    });
            this.eventListeners = eventListeners;
        }
    
        ...
    }
    ```


---

Reply via email to