【问题标题】:Could not autowire. No beans of Neo4jTemplate type found无法自动接线。找不到 Neo4jTemplate 类型的 bean
【发布时间】:2013-01-02 06:20:26
【问题描述】:

我使用 IDEA IntelliJ 12.0.2。

我的 application-context.xml 是:

<?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"
       xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/neo4j http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd">

    <neo4j:config storeDirectory="../embeddedNeo4j"/>

    <context:spring-configured/>

    <context:annotation-config/>
    <context:component-scan base-package="models"/>

</beans>

我的测试课是:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.neo4j.support.Neo4jTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"/application-context.xml"})
@Transactional
public class MyTest {

    @Autowired
    Neo4jTemplate template; //=> Could not autowire.No beans of Neo4jTemplate type found

    //my tests here
}

我错过了一些配置吗?

这似乎是 Intellij 的一个老问题:http://www.markvandenbergh.com/archives/260/autowiring-spring-bean-in-intellij/

【问题讨论】:

  • 从问题中不清楚您的应用程序是否无法正常工作,或者它只是一个 IDE 错误,但应用程序按预期工作。
  • @Andrey Polunin 实际上,应用程序可以编译,但 IntelliJ 会警告此“错误”。

标签: java spring intellij-idea spring-data-neo4j


【解决方案1】:

在带有 Spring Data bean 的 IntelliJ 中经常发生这种情况。 IntelliJ 不能很好地从 Spring Data 的命名空间配置中解析出实例。作为一个例子(除了你的),IntelliJ 不会正确验证扩展 Spring Data MongoRepository@Autowired@Injected 类。正如您所注意到的,它不会伤害您的应用程序,但在开发过程中却很烦人。以下是抑制“错误”的方法:

@SuppressWarnings("SpringJavaAutowiringInspection")
@Autowired
Neo4jTemplate template;

您可以通过单击红色灯泡(将鼠标悬停在带红色下划线的元素上时出现错误指示)选择“Inspection 'Autowiring for Bean Class' options”然后最后选择“Suppress for field”来完成同样的操作。或者,如果您想为整个班级禁止显示,请选择“为班级禁止显示”。

【讨论】:

    猜你喜欢
    • 2021-01-17
    • 2019-08-17
    • 2021-07-02
    • 2020-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多