gpt4 book ai didi

java - Java 类中的 Grails @Autowire 不起作用

转载 作者:行者123 更新时间:2023-12-01 12:17:37 25 4
gpt4 key购买 nike

我有一些 Java 代码,我想将它们转换为 Bean,可以通过依赖注入(inject)在 Grails Controller 和服务中使用。该代码基于 here (作为独立的 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);
}

所以现在我想从 Controller 使用:

// TestController.groovy
package hello

import hello.WannabeABeanDB

class TestController {

WannabeABeanDB graph

def index() {
render graph.test()
}
}

我已经设置(在 Config.groovy 中):

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

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

所以问题是,我还需要做什么才能编写使用 Java 代码(在 src/java 中)作为 Grails Controller 或服务中的 Bean?

最佳答案

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

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

我进行了以下更改以使其正常工作:

  1. 我回到 neo4j xml 配置并添加了 grails-app/conf/resources.xml 以及必要的 neo4j 配置。

  2. 从类中删除了 @EnableNeo4jRepositories 注释

  3. 从 Config.groovy 中取出语句 -- grails.spring.bean.packages = ['grailsS​​dn4j']

供其他人引用,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>

关于java - Java 类中的 Grails @Autowire 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26894640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com