Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not autowire. No beans of Neo4jTemplate type found

I use IDEA IntelliJ 12.0.2.

My application-context.xml is:

<?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>

My test class is:

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
}

Have I missed some configuration?

It seems to be an old problem with Intellij: http://www.markvandenbergh.com/archives/260/autowiring-spring-bean-in-intellij/

like image 871
Mik378 Avatar asked Jan 18 '13 14:01

Mik378


1 Answers

This happens a lot in IntelliJ with Spring Data beans. IntelliJ does not parse out instances from Spring Data's namespace configurations too well. As an example (in addition to yours,) IntelliJ will not properly validate an @Autowired or @Injected class that extends a Spring Data MongoRepository. As you've noticed, it does not hurt your app, but it is pretty annoying during development. Here's how you can suppress the "error":

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

You can accomplish the same by clicking the red light bulb (error indicator when hovering over the red-underlined element,) selecting "Inspection 'Autowiring for Bean Class' options" and then finally "Suppress for field." Or, if you want to suppress it for your entire class, select "Suppress for class."

like image 180
MattSenter Avatar answered Sep 20 '22 15:09

MattSenter