Grails @Autowire in Java class not working

廉价感情. 提交于 2019-12-02 07:42:38

-- Editing answer based on the sample code --

I suspect the issue is related to how component scanning works with Grails and Java code mixed together. It might also have to do with the order in which beans are created in this situation and proxying of classes behind the scenes.

I made the following changes to make this work:

  1. I went back to neo4j xml configuration and added a grails-app/conf/resources.xml with necessary neo4j configuration.

  2. Removed the @EnableNeo4jRepositories annotation from the class

  3. Took out statement from Config.groovy -- grails.spring.bean.packages = ['grailsSdn4j']

For others' reference, the neo4j configuration in resources.xml looks like this:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/data/neo4j http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd">

    <context:spring-configured/>
    <context:annotation-config/>

    <neo4j:config storeDirectory="target/data/db" base-package="grailsSdn4j"/>

    <neo4j:repositories base-package="grailsSdn4j"/>
</beans>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!