<i id='0JG6R'><tr id='0JG6R'><dt id='0JG6R'><q id='0JG6R'><span id='0JG6R'><b id='0JG6R'><form id='0JG6R'><ins id='0JG6R'></ins><ul id='0JG6R'></ul><sub id='0JG6R'></sub></form><legend id='0JG6R'></legend><bdo id='0JG6R'><pre id='0JG6R'><center id='0JG6R'></center></pre></bdo></b><th id='0JG6R'></th></span></q></dt></tr></i><div id='0JG6R'><tfoot id='0JG6R'></tfoot><dl id='0JG6R'><fieldset id='0JG6R'></fieldset></dl></div>
    <tfoot id='0JG6R'></tfoot>

    <small id='0JG6R'></small><noframes id='0JG6R'>

      <bdo id='0JG6R'></bdo><ul id='0JG6R'></ul>
    1. <legend id='0JG6R'><style id='0JG6R'><dir id='0JG6R'><q id='0JG6R'></q></dir></style></legend>

    2. Java类中的Grails @Autowire不起作用

      时间:2024-08-24
          • <tfoot id='A0NCn'></tfoot>
              <tbody id='A0NCn'></tbody>
            <legend id='A0NCn'><style id='A0NCn'><dir id='A0NCn'><q id='A0NCn'></q></dir></style></legend>
              <bdo id='A0NCn'></bdo><ul id='A0NCn'></ul>
            • <i id='A0NCn'><tr id='A0NCn'><dt id='A0NCn'><q id='A0NCn'><span id='A0NCn'><b id='A0NCn'><form id='A0NCn'><ins id='A0NCn'></ins><ul id='A0NCn'></ul><sub id='A0NCn'></sub></form><legend id='A0NCn'></legend><bdo id='A0NCn'><pre id='A0NCn'><center id='A0NCn'></center></pre></bdo></b><th id='A0NCn'></th></span></q></dt></tr></i><div id='A0NCn'><tfoot id='A0NCn'></tfoot><dl id='A0NCn'><fieldset id='A0NCn'></fieldset></dl></div>

            • <small id='A0NCn'></small><noframes id='A0NCn'>

                本文介绍了Java类中的Grails @Autowire不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一些 Java 代码,我想将其转换为可在 Grails 控制器中使用的 Bean.通过依赖注入提供服务.该代码基于 here (作为独立运行时工作正常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 运行应用程序时,Grails 崩溃并显示一条很长的错误消息,指出它无法使用空数据库.我不相信PersonRepository 或graphDatabase 都会使用@Autowire.

                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.

                所以问题是,我还需要做些什么才能将使用 Java 代码(在 src/java 中)编写为 Grails 控制器或服务中的 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:

                1. 我回到 neo4j xml 配置并添加了一个带有必要 neo4j 配置的 grails-app/conf/resources.xml.

                1. 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 = ['grailsSdn4j']

                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="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类中的Grails @Autowire不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:休眠查询以过滤嵌套对象列表中的结果 下一篇:Spring框架中子文档数组字段中的过滤数组

                相关文章

                <legend id='oaR6T'><style id='oaR6T'><dir id='oaR6T'><q id='oaR6T'></q></dir></style></legend>

                <i id='oaR6T'><tr id='oaR6T'><dt id='oaR6T'><q id='oaR6T'><span id='oaR6T'><b id='oaR6T'><form id='oaR6T'><ins id='oaR6T'></ins><ul id='oaR6T'></ul><sub id='oaR6T'></sub></form><legend id='oaR6T'></legend><bdo id='oaR6T'><pre id='oaR6T'><center id='oaR6T'></center></pre></bdo></b><th id='oaR6T'></th></span></q></dt></tr></i><div id='oaR6T'><tfoot id='oaR6T'></tfoot><dl id='oaR6T'><fieldset id='oaR6T'></fieldset></dl></div>

                <small id='oaR6T'></small><noframes id='oaR6T'>

                  <tfoot id='oaR6T'></tfoot>

                    <bdo id='oaR6T'></bdo><ul id='oaR6T'></ul>