0

I have a web application with a context hirarchy

a) application context with scanning which exclude controller:

<context:component-scan base-package="com.mypackage">
        <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation" />
    </context:component-scan>

b) a mvc context wich include controller :

<context:component-scan base-package="com.mypackage"
        use-default-filters="false">
        <context:include-filter expression="org.springframework.stereotype.Controller"
            type="annotation" />
    </context:component-scan>

I 'wrote' this beanPostProcessor (that automatiocaly set logger)

@Component
public class LoggableInjector implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException {
        ReflectionUtils.doWithFields(bean.getClass(), new FieldCallback() {
            public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
                ReflectionUtils.makeAccessible(field);
                if (field.getAnnotation(Loggable.class) != null) {
                    Logger log = LoggerFactory.getLogger(bean.getClass());
                    field.set(bean, log);
                }
            }
        });
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(final Object bean, String beanName) throws BeansException {
        ReflectionUtils.doWithFields(bean.getClass(), new FieldCallback() {
            public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
                ReflectionUtils.makeAccessible(field);
                if (field.getAnnotation(Loggable.class) != null) {
                    Logger log = LoggerFactory.getLogger(bean.getClass());
                    field.set(bean, log);
                }
            }
        });
        return bean;
    }

}

The problem is that this processor is not called for @Controller class I guess because it only process beans in its own context.

How could I make it process beans (ie @Controller) which are in the mvc context ?

thanks !

jpprade
  • 3,497
  • 3
  • 45
  • 58
  • 1
    By adding it to that context. As you already mentioned yourself a `Bean(Factory)PostProcessor` handles only beans it can reach i.e. which are in the same context. It will not handle beans in the child or parent context. – M. Deinum Sep 02 '14 at 08:00
  • Yeah... just declaring it int the mvc xml was enough – jpprade Sep 02 '14 at 08:31

1 Answers1

1

https://jira.spring.io/browse/SPR-8331

Here is what Spring explicitly say.

The PostProcessor can only process the beans in context which the PostProcessor itself belongs to and not from Hierarchical contexts.

The only way that comes to my mind is adding PostProcessor in all context you are going to load.