Java 从@Autowired切换到@Inject后,存储库中的EntityManager为空

Java 从@Autowired切换到@Inject后,存储库中的EntityManager为空,java,spring,jpa,cdi,Java,Spring,Jpa,Cdi,我需要在服务模块中从Spring注释切换到CDI注释。一切正常,但实体管理器为空。如果我用@Autowire对存储库进行注释,则会插入实体管理器。当我切换到@Inject时,实体管理器为空,我不知道为什么 以下是spring上下文配置: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http:/

我需要在服务模块中从Spring注释切换到CDI注释。一切正常,但实体管理器为空。如果我用@Autowire对存储库进行注释,则会插入实体管理器。当我切换到@Inject时,实体管理器为空,我不知道为什么

以下是spring上下文配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- Scan service classes for beans -->
    <context:component-scan
            base-package="com.monitoring.facade, com.monitoring.indication, com.monitoring.app"
            annotation-config="false">
    </context:component-scan>
</beans>

您是否定义了META-INF/beans.xml?Spring3.0及更高版本提供了对JSR330注释的支持。您使用的是哪个版本的spring?您配置了annotation config=false,这将禁用注释处理。删除此项或将其设置为默认值true。我定义了一个beans.xml,它只包含一个空标记,我使用的是Spring 3.0.1我想现在CDI和Spring之间存在冲突。确保CDI没有在这里造成严重破坏,否则spring将取决于您如何看待它。确保您没有beans.xml,因为它将触发CDI注入,而spring是您唯一的控制手段。基本上,annotation config=false会完全禁用处理,所以我相信您没有使用spring,尽管您认为您在使用spring。
@Named
public class IndicatorTreeService implements Serializable{
    //@Autowire - everything ok
    @Inject
    private IndicatorRepository indicatorRepository;

    @Inject
    private IndicatorTreeBuilder indicatorTreeBuilder;

    public List<AbstractIndicatorTreeItem> getIndicatorTreeItems(){
        List<IndicatorVO> indicators = indicatorRepository.getIndicatorQuery().getResultList();
        return indicatorTreeBuilder.buildTree(indicators);
    }
}
@Named
public class IndicatorRepository{
    @PersistenceContext
    private EntityManager em;

    public IndicatorQuery getIndicatorQuery(){
        return new IndicatorQuery(em);
    }
}