Java类中的Grails@Autowire不工作

Java类中的Grails@Autowire不工作,java,spring,grails,spring-data,spring-data-neo4j,Java,Spring,Grails,Spring Data,Spring Data Neo4j,我有一些Java代码,我想把它们转换成一个Bean,通过依赖注入在Grails控制器和服务中使用。代码基于此(当作为独立Java应用程序运行时,它可以正常工作) 具体而言,我有: // WannabeABeanDB.java package hello; import org.neo4j.graphdb.GraphDatabaseService; import org.neo4j.graphdb.Transaction; import org.neo4j.graphdb.factory.Gra

我有一些Java代码,我想把它们转换成一个Bean,通过依赖注入在Grails控制器和服务中使用。代码基于此(当作为独立Java应用程序运行时,它可以正常工作)

具体而言,我有:

// WannabeABeanDB.java
package hello;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.kernel.impl.util.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.neo4j.config.EnableNeo4jRepositories;
import org.springframework.data.neo4j.config.Neo4jConfiguration;
import org.springframework.data.neo4j.core.GraphDatabase;

import java.io.File;

@Component
@Configuration
@EnableNeo4jRepositories(basePackages = "hello")
public class WannabeABeanDB extends Neo4jConfiguration {

    public WannabeABeanDB() {
        setBasePackage("hello");
    }

    @Bean
    GraphDatabaseService graphDatabaseService() {
        return new GraphDatabaseFactory().newEmbeddedDatabase("accessingdataneo4j.db");
    }

    @Autowired
    PersonRepository personRepository;

    @Autowired
    GraphDatabase graphDatabase;

    public String testThatWeCanAccessDatabase() throws Exception {
        Person greg = new Person("Greg");

        Transaction tx = graphDatabase.beginTx();
        try {
            personRepository.save(greg);
            greg = personRepository.findByName("Greg").toString();
            tx.success();
        } finally {
            tx.close();
        }
        return greg.name;

    }    
}



// Person.java
package hello;

import java.util.HashSet;
import java.util.Set;

import org.neo4j.graphdb.Direction;
import org.springframework.data.neo4j.annotation.Fetch;
import org.springframework.data.neo4j.annotation.GraphId;
import org.springframework.data.neo4j.annotation.NodeEntity;
import org.springframework.data.neo4j.annotation.RelatedTo;

@NodeEntity
public class Person {

    @GraphId Long id;
    public String name;

    public Person() {}
    public Person(String name) { this.name = name; }

    public String toString() {
        return this.name;
    }
}



// PersonRepository.java
package hello;

import org.springframework.data.repository.CrudRepository;

public interface PersonRepository extends CrudRepository<Person, String> {    
    Person findByName(String name);    
}
我已经设置(在Config.groovy中):

然而,当我运行grails run应用程序时,grails会崩溃,并显示一条很长的错误消息,说明它无法处理空数据库。我认为PersonRepository或graphDatabase都没有使用@Autowire

所以问题是,我还需要做些什么才能将Java代码(在src/Java中)作为Grails控制器或服务中的Bean来编写?

--根据示例代码编辑答案--

我怀疑这个问题与Grails和Java代码混合在一起时如何进行组件扫描有关。也可能 这与在这种情况下创建bean的顺序以及在幕后代理类有关

我做了以下更改以实现此功能:

  • 我回到了Neo4jXML配置,添加了一个grails app/conf/resources.xml和必要的neo4j配置

  • 已从类中删除@EnableNeo4jRepositories注释

  • 从Config.groovy中取出语句--grails.spring.bean.packages=['grailsSdn4j']

  • 供其他人参考,resources.xml中的neo4j配置如下所示:

    <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>
    
    
    
    谢谢,GraphDatabase bean不是我提供/编写的;它是spring-data-neo4j的一部分。我尝试将各种内容放入resources.groovy,例如bean={graphDatabase(graphDatabase){}},但没有成功。你有什么建议可以试试吗?我根据你的评论和上面的代码更新了我的答案。基本上,graphDatabase通常在Java代码(SpringDI)中注入,然后在grails应用程序上下文中注册POJO(WannabeABeanDB)。失败的是“@Autowired GraphDatabase GraphDatabase;”语句:GraphDatabase保持空。您的回答帮助我在其他地方解决了另一个问题(谢谢!),但遗憾的是它没有解决这个问题。那么graphDatabase bean将由Spring创建并注入?看起来@EnableNeo4jRepositories注释将扫描基本包“hello”中的存储库并自动生成实现。我猜想SpringJava应用程序中注释和扫描的工作方式与grails应用程序的工作方式并不完全相同。如果我们能够弄清楚这个bean应该如何创建,那么我们就可以找到这个问题的解决方案。如果有可能把你的代码放到github上,我可以在今天晚些时候看一看。我把代码放到了github上:我一辈子都不能想出我需要做什么才能让@Autowired语句工作。您所能提供的任何建议都将受到感激——我相信其他人也希望能够使用spring-data-neo4j和Grails。谢谢你的关注。
    grails.spring.bean.packages = ['hello']
    
    <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>