Grails @Autowire在Java类中无法正常工作

编程入门 行业动态 更新时间:2024-10-28 12:20:14
本文介绍了Grails @Autowire在Java类中无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I have some Java code that I want to turn into a Bean that can be used within Grails controllers & services via dependency injection. The code is based on that here (which works fine when run as a standalone Java application).

具体来说,我有:

// 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); }

现在我想从控制器中使用:

So now I want to use from a controller:

// TestController.groovy package hello import hello.WannabeABeanDB class TestController { WannabeABeanDB graph def index() { render graph.test() } }

我已设置(在Config.groovy内):

I've set (within Config.groovy):

grails.spring.bean.packages = ['hello']

但是,当我执行grails run-app时,Grails会崩溃并显示一条很长的错误消息,指出它无法使用null数据库。我不相信@Autowire被选为PersonRepository或graphDatabase。

However, when I do a grails run-app, Grails crashes with a very long error message that says it can't work with a null database. I don't believe the @Autowire is being picked up for either PersonRepository or for graphDatabase.

所以问题是,我还需要做些什么才能做到在Grails控制器或服务中使用Java代码(在src / java中)作为Bean吗?

So the question is, what more do I need to do to be able to write use Java code (within src/java) as a Bean within a Grails controller or service?

推荐答案

- 编辑基于答案的答案示例代码 -

-- Editing answer based on the sample code --

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

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:

  • 我回到neo4j xml配置并添加了grails-app / conf / resources.xml neo4j配置。

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

    从类中删除了@ EnableNeo4jRepositories注释

    Removed the @EnableNeo4jRepositories annotation from the class

    取出声明来自Config.groovy - grails.spring.bean.packages = ['grailsS​​dn4j']

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

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

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

    <beans xmlns="www.springframework/schema/beans" xmlns:context="www.springframework/schema/context" xmlns:xsi="www.w3/2001/XMLSchema-instance" xmlns:neo4j="www.springframework/schema/data/neo4j" xsi:schemaLocation="www.springframework/schema/beans www.springframework/schema/beans/spring-beans-3.0.xsd www.springframework/schema/context www.springframework/schema/context/spring-context-3.0.xsd www.springframework/schema/data/neo4j www.springframework/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>
  • 更多推荐

    Grails @Autowire在Java类中无法正常工作

    本文发布于:2023-07-25 09:01:51,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1208951.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:无法正常   类中   工作   Grails   Autowire

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!