无法自动装配.找不到Neo4jTemplate类型的bean

Mik*_*378 8 java spring intellij-idea spring-data-neo4j

我使用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>
Run Code Online (Sandbox Code Playgroud)

我的测试类是:

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
}
Run Code Online (Sandbox Code Playgroud)

我错过了一些配置吗?

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

Mat*_*ter 9

这在IntelliJ中使用Spring Data bean发生了很多.IntelliJ没有很好地解析Spring Data的命名空间配置中的实例.作为一个例子(除了你的),IntelliJ将无法正确验证扩展Spring数据的ed @Autowired@Injected类MongoRepository.正如您所注意到的,它不会伤害您的应用程序,但在开发过程中它非常烦人.以下是如何抑制"错误":

@SuppressWarnings("SpringJavaAutowiringInspection")
@Autowired
Neo4jTemplate template;
Run Code Online (Sandbox Code Playgroud)

您可以通过单击红色灯泡(悬停在带红色下划线的元素上时出现错误指示符)选择"检查'Bean类的自动装配'选项"然后最后"抑制字段"来完成相同操作.或者,如果要为整个班级禁止它,请选择"抑制课程".