Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 为什么在实现BeanFactoryPostProcessor时@Inject不起作用_Spring - Fatal编程技术网

Spring 为什么在实现BeanFactoryPostProcessor时@Inject不起作用

Spring 为什么在实现BeanFactoryPostProcessor时@Inject不起作用,spring,Spring,我有一些带有简单任务的web应用程序: public class CustomTask { private final Logger logger = LoggerFactory.getLogger(getClass()); @Inject private CustomDao customDao; @Override @Transactional(propagation = Propagation.REQUIRED, timeout = 600)

我有一些带有简单任务的web应用程序:

public class CustomTask {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Inject
    private CustomDao customDao;



    @Override
    @Transactional(propagation = Propagation.REQUIRED, timeout = 600)
    public int run() {      
        customDao.doSomething();
    }
}
现在它可以正常工作了。但现在我想将BeanFactoryPostProcessor的实现(
implements BeanFactoryPostProcessor
)添加到此任务并重写此方法:

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

    logger.info(beanFactory.getClass() + "xxxxxxxxxx");

    logger.info("The factory contains the followig beans:");
    String[] beanNames = beanFactory.getBeanDefinitionNames();
    for (int i = 0; i < beanNames.length; ++i)
        logger.info(beanNames[i]);
}
@覆盖
public void后处理beanFactory(ConfigurableListableBeanFactory beanFactory)引发BeanException{
logger.info(beanFactory.getClass()+“xxxxxxxxx”);
info(“工厂包含以下bean:”);
字符串[]beanNames=beanFactory.getBeanDefinitionNames();
对于(int i=0;i

但是现在我想对
customDao
做一些事情,它抛出
NullPointException
,我不知道为什么,因为在logger中我看到这个bean被注册了。您能解释一下发生此异常的原因以及我应该如何修复它吗?

这将不起作用,因为
@Inject
(就像
@Autowired
)是由
AutowiredNotationBeanPostProcessor
检测到的,它是
BeanPostProcessor
<代码>BeanFactoryPostProcessor在应用程序上下文中的任何其他bean之前实例化。因此,当您的后处理器被创建时,检测
@Inject
注释的基础结构甚至没有被创建,并且不能作用于您的bean工厂后处理器

请参阅其中关于BeanFactoryPostProcessors限制的注释

您可以让您的任务实现BeanFactoryAware并自己注入bean:

public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.customDao = beanFactory.getBean(CustomDao.class);
    }

这将不起作用,因为
@Inject
(就像
@Autowired
)是由
自动连线标记BeanPostProcessor
检测到的,它是一个
BeanPostProcessor
<代码>BeanFactoryPostProcessor在应用程序上下文中的任何其他bean之前实例化。因此,当您的后处理器被创建时,检测
@Inject
注释的基础结构甚至没有被创建,并且不能作用于您的bean工厂后处理器

请参阅其中关于BeanFactoryPostProcessors限制的注释

您可以让您的任务实现BeanFactoryAware并自己注入bean:

public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.customDao = beanFactory.getBean(CustomDao.class);
    }

您也可以使用java配置
@Configuration
@Bean
创建您自己的任务实例,但要注意@Bean java文档中指定的,与
BeanFactoryPostProcessor
s相关。非常感谢您的提示!)您也可以使用java配置
@Configuration
@Bean
创建您自己的任务实例,但要注意@Bean java文档中指定的,与
BeanFactoryPostProcessor
s相关。非常感谢您的提示!)