gpt4 book ai didi

java - Bean 后处理器实现

转载 作者:行者123 更新时间:2023-12-04 10:08:16 25 4
gpt4 key购买 nike

根据 Spring Boot 文档

An ApplicationContext can autodetect BeanPostProcessor beans in its bean definitions and apply those post-processors to any beans subsequently created



我在主类包中创建了一个自定义 bean 后处理器类,但它不起作用。这是代码。
public class DataBeanPostProcessor implements BeanPostProcessor, Ordered {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
LogFactory.getLog(this.getClass()).info("Before Processing- " + beanName);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
LogFactory.getLog(this.getClass()).info("After Processing- " + beanName);
return bean;
}


@Override
public int getOrder() {
return Integer.MAX_VALUE;
}
}

最佳答案

正如大牛在评论中所说,你必须添加@Component

@Component
public class DataBeanPostProcessor implements BeanPostProcessor, Ordered {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
LogFactory.getLog(this.getClass()).info("Before Processing- " + beanName);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
LogFactory.getLog(this.getClass()).info("After Processing- " + beanName);
return bean;
}


@Override
public int getOrder() {
return Integer.MAX_VALUE;
}
}

关于java - Bean 后处理器实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61462049/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com