Advanced XML Config

MVC 命名空间没有高级模式。如果你需要自定义一个 Bean 的属性,但又无法通过其他方式更改,你可以使用 Spring ApplicationContextBeanPostProcessor 生命周期挂钩,如下例所示:

The MVC namespace does not have an advanced mode. If you need to customize a property on a bean that you cannot change otherwise, you can use the BeanPostProcessor lifecycle hook of the Spring ApplicationContext, as the following example shows:

  • Java

  • Kotlin

@Component
public class MyPostProcessor implements BeanPostProcessor {

	public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException {
		// ...
		return bean;
	}
}
@Component
class MyPostProcessor : BeanPostProcessor {

	override fun postProcessBeforeInitialization(bean: Any, name: String): Any {
		// ...
		return bean
	}
}

请注意,你需要将 MyPostProcessor 声明为一个 Bean,或者在 XML 中显式声明,或者通过 <component-scan/> 声明让其被检测到。

Note that you need to declare MyPostProcessor as a bean, either explicitly in XML or by letting it be detected through a <component-scan/> declaration.