我有一些Java代码,我想把它变成一个可以在Grails控制器中使用的Bean。通过依赖注入的服务。代码基于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);
}
所以现在我想从控制器中使用:
// 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会崩溃并显示一条非常长的错误消息,指出它无法使用null数据库。我不相信@Autowire正在为PersonRepository或graphDatabase选择。
所以问题是,我还需要做些什么才能在Grails控制器或服务中将Java代码(在src / java中)用作Bean?
答案 0 :(得分:1)
- 根据示例代码编辑答案 -
我怀疑这个问题与组件扫描如何与Grails和Java代码混合在一起有关。它也可能 与在这种情况下创建bean的顺序和幕后类的代理有关。
我进行了以下更改以使其工作:
我回到neo4j xml配置并添加了一个带有必要的neo4j配置的grails-app / conf / resources.xml。
从班级中删除了@ EnableNeo4jRepositories注释
从Config.groovy中取出声明 - grails.spring.bean.packages = [&#39; grailsSdn4j&#39;]
对于其他人&#39;参考,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>