Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring-@自动连接存储库不起作用@自动连接其他类不需要_Java_Spring_Spring Data_Autowired_Spring Data Neo4j - Fatal编程技术网

Java Spring-@自动连接存储库不起作用@自动连接其他类不需要

Java Spring-@自动连接存储库不起作用@自动连接其他类不需要,java,spring,spring-data,autowired,spring-data-neo4j,Java,Spring,Spring Data,Autowired,Spring Data Neo4j,我试图自动连接存储库,但它拒绝工作。我已经为此挣扎了一个星期,但我似乎无法理解。有趣的是,当我注释掉person repository的自动连接时,程序可以正常工作和编译,但当我尝试自动连接person repository时,我得到了这个错误(我忽略了大部分错误,因为我无法发布所有错误)。这里是例外,我的类在下面 HTTP Status 500 - Servlet.init() for servlet appServlet threw exception type Exception rep

我试图自动连接存储库,但它拒绝工作。我已经为此挣扎了一个星期,但我似乎无法理解。有趣的是,当我注释掉person repository的自动连接时,程序可以正常工作和编译,但当我尝试自动连接person repository时,我得到了这个错误(我忽略了大部分错误,因为我无法发布所有错误)。这里是例外,我的类在下面

HTTP Status 500 - Servlet.init() for servlet appServlet threw exception

type Exception report

message Servlet.init() for servlet appServlet threw exception

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Servlet.init() for servlet appServlet threw exception
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1852)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    java.lang.Thread.run(Thread.java:722)

root cause

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'homeController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.mycompany.smsdatabase.service.PersonImport com.mycompany.smsdatabase.controller.HomeController.personImport; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personImport': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.mycompany.smsdatabase.repositories.PersonRepository com.mycompany.smsdatabase.service.PersonImport.PersonRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.mycompany.smsdatabase.repositories.PersonRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
    org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645)
    org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508)
    org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449)
    org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133)
    javax.servlet.GenericServlet.init(GenericServlet.java:160)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1852)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    java.lang.Thread.run(Thread.java:722)
以下是我正在学习的课程

package com.mycompany.smsdatabase.controller;

import com.mycompany.smsdatabase.service.PersonImport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeController {
   @Autowired
   PersonImport personImport;

   @RequestMapping(value = "/", method = RequestMethod.GET)
    public String Home()
    {
      personImport.doImportPerson("anthony");
      return "index";
    }
}

package com.mycompany.smsdatabase.service;
import com.mycompany.smsdatabase.domain.Person;
import com.mycompany.smsdatabase.repositories.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class PersonImport  {

@Autowired
private PersonRepository PersonRepository;

 public Person doImportPerson( String id)
 {   
  Person person = PersonRepository.findById(id);
  if (person == null) {
    person = new Person(id,"anthony");
     }
   PersonRepository.save(person);
     System.out.println("inside doImportPerson");
     return person;
 }
}
package com.mycompany.smsdatabase.repositories;

import com.mycompany.smsdatabase.domain.Person;
import org.springframework.data.neo4j.repository.GraphRepository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public interface PersonRepository extends GraphRepository<Person>{
    Person findById(String id);
}
package com.mycompany.smsdatabase.controller;
导入com.mycompany.smsdatabase.service.PersonImport;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Controller;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
@控制器
公共类家庭控制器{
@自动连线
PersonImport PersonImport;
@RequestMapping(value=“/”,method=RequestMethod.GET)
公共字符串Home()
{
personImport.doImportPerson(“安东尼”);
返回“索引”;
}
}
包com.mycompany.smsdatabase.service;
导入com.mycompany.smsdatabase.domain.Person;
导入com.mycompany.smsdatabase.repositories.PersonRepository;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Service;
导入org.springframework.transaction.annotation.Transactional;
@服务
@交易的
公共类PersonImport{
@自动连线
个人知识库;
公共人物doImportPerson(字符串id)
{   
Person=PersonRepository.findById(id);
if(person==null){
人员=新人员(id为“anthony”);
}
PersonRepository.save(person);
System.out.println(“内部doImportPerson”);
返回人;
}
}
包com.mycompany.smsdatabase.repositories;
导入com.mycompany.smsdatabase.domain.Person;
导入org.springframework.data.neo4j.repository.graphRespository;
导入org.springframework.stereotype.Repository;
导入org.springframework.transaction.annotation.Transactional;
@存储库
公共接口PersonRepository扩展了GraphRespository{
Person findById(字符串id);
}
我省略了person类,因为它只是一个节点实体类

这里还有我的应用程序上下文xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/data/neo4j
    http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd">

    <mvc:annotation-driven />

    <context:component-scan base-package="com.mycompany.smsdatabase">
    </context:component-scan>

    <context:spring-configured/>
    <neo4j:config storeDirectory="target/data/graph.db"/>
    <neo4j:repositories base-package="com.mycompany.smsdatabase"/>

    <bean id="graphDatabaseService" class="org.neo4j.test.ImpermanentGraphDatabase" destroy-method="shutdown"/>
    <tx:annotation-driven mode="proxy"/>

</beans>

您应该为应用程序上下文文件中用@AutoWired注释的变量定义相应的bean,即

<bean id="personRepository" class="com.mycompany.smsdatabase.repositories.PersonRepository"/>
<bean id="personImport" class="com.mycompany.smsdatabase.service.PersonImport"/>

您应该为应用程序上下文文件中用@AutoWired注释的变量定义相应的bean,即

<bean id="personRepository" class="com.mycompany.smsdatabase.repositories.PersonRepository"/>
<bean id="personImport" class="com.mycompany.smsdatabase.service.PersonImport"/>

您的spring配置错误:

您的配置中有

但是java代码中的导入读取

import com.mycompany.smsdatabase.repositories.PersonRepository

您应该将spring配置更正为


您的spring配置错误:

您的配置中有

但是java代码中的导入读取

import com.mycompany.smsdatabase.repositories.PersonRepository

您应该将spring配置更正为



你能发布PersonImport的代码吗?没关系,我第一次错过了。你能把PersonRepository实现也放在那里吗?至少是带有注释的类声明您是否有PersonRepository接口的实现?看来它找不到了。@Repository注释应该在一个具体的类上。然后应该插入接口。我注意到,在上下文xml中,您声明com.mycompany.smsdatabase为repositories基本包,而您附加的代码中的实际包是com.mycompany.smsdatabase.repositories。您可以发布PersonImport的代码吗,我第一次错过了。你能把PersonRepository实现也放在那里吗?至少是带有注释的类声明您是否有PersonRepository接口的实现?看来它找不到了。@Repository注释应该在一个具体的类上。然后你应该注入接口。我注意到在上下文xml中,你声明com.mycompany.smsdatabase为repositories基本包,而你附加的代码中的实际包是com.mycompany.smsdatabase.repositories。我尝试过这个,它仍然抛出相同的错误,我不确定此时要做什么
PersonRepository
是一个接口。接口不能自动连接?在spring提供的cineast教程中,它们自动关联了许多interfaces@IceNine您不会自动连接接口,因为接口无法实例化。您可以将实现自动关联到接口。通过这个答案中的bean声明,Spring将尝试实例化它不能实例化的
PersonRepository
。@SotiriosDelimanolis你是对的,我错过了PersonRepository是一个接口。我认为您可以删除PersonRepository bean声明,我有一个类似的项目,并且存储库自动连接一切都很好。我已经尝试过了,但它仍然抛出相同的错误,我不确定此时该做什么
PersonRepository
是一个接口。接口不能自动连接吗?在spring提供的cineast教程中,它们自动关联了许多interfaces@IceNine您不会自动连接接口,因为接口无法实例化。您可以将实现自动关联到接口。通过这个答案中的bean声明,Spring将尝试实例化它不能实例化的
PersonRepository
。@SotiriosDelimanolis你是对的,我错过了PersonRepository是一个接口。我认为您可以删除PersonRepository bean声明,我有一个类似的项目,使用repositories自动连接一切都很好。