我有一个带有上下文层次结构的 Web 应用程序
a)扫描排除控制器的应用程序上下文:
<context:component-scan base-package="com.mypackage">
<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation" />
</context:component-scan>
b) 包含控制器的 mvc 上下文:
<context:component-scan base-package="com.mypackage"
use-default-filters="false">
<context:include-filter expression="org.springframework.stereotype.Controller"
type="annotation" />
</context:component-scan>
我“写”了这个 beanPostProcessor(自动设置记录器)
@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;
}
}
问题是我猜这个处理器没有被 @Controller 类调用,因为它只在自己的上下文中处理 bean。
我怎样才能让它处理 mvc 上下文中的 bean(即@Controller)?
谢谢 !