2013-08-12 7 views
5

Ich benutze Spring und ich habe eine Menge Probleme mit der @Autowired Annotation. Nach dem Kompilieren wirft diese Fehler EDIT Ich benutze regelmäßige Java-Klassen, um meine Klassen zu schreiben. Das verursacht das Problem nicht, oder?Spring @Autowired Nicht funktionierender Fehler Erstellen von Bean-Injektion von Autowire-Abhängigkeiten fehlgeschlagen

Aug 12, 2013 2:40:32 PM org.apache.catalina.core.ApplicationContext log 
INFO: No Spring WebApplicationInitializer types detected on classpath 
Aug 12, 2013 2:40:32 PM org.apache.catalina.core.ApplicationContext log 
INFO: Initializing Spring root WebApplicationContext 
Aug 12, 2013 2:40:33 PM org.apache.catalina.core.StandardContext listenerStart 
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener 
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.NoSuchBeanDefinitionException: No matching bean of type [com.mycompany.smsdatabase.service.PersonImport] 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)} 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464) 
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:384) 
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283) 
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111) 
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4887) 
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5381) 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901) 
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877) 
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633) 
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:657) 
    at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:536) 
    at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1462) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301) 
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) 
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:791) 
    at org.apache.catalina.manager.ManagerServlet.check(ManagerServlet.java:1445) 
    at org.apache.catalina.manager.ManagerServlet.deploy(ManagerServlet.java:860) 
    at org.apache.catalina.manager.ManagerServlet.doGet(ManagerServlet.java:357) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:581) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1008) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) 
    at java.lang.Thread.run(Thread.java:722) 
Caused by: 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.NoSuchBeanDefinitionException: No matching bean of type [com.mycompany.smsdatabase.service.PersonImport] 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)} 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:506) 
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284) 
    ... 53 more 
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.mycompany.smsdatabase.service.PersonImport] 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)} 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:924) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:793) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478) 
    ... 55 more 

Aug 12, 2013 2:40:33 PM org.apache.catalina.core.ApplicationContext log 
INFO: Closing Spring root WebApplicationContext 

HomeController.java

package com.mycompany.smsdatabase.controller; 

import com.mycompany.smsdatabase.domain.Person; 
import com.mycompany.smsdatabase.repositories.PersonRepository; 
import com.mycompany.smsdatabase.service.PersonImport; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.support.AbstractApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
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() 
    { 
     return "index"; 
    } 
} 

PersonImport.java

package com.mycompany.smsdatabase.service; 
import com.mycompany.smsdatabase.domain.Person; 
import com.mycompany.smsdatabase.repositories.PersonRepository; 
import javax.transaction.Transaction; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.stereotype.Service; 
import org.springframework.transaction.annotation.Transactional; 
import org.springframework.web.bind.annotation.PathVariable; 

@Service 
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("+++++++++++++++++++++++++++++++++++++hello+++++++++++++"); 
     return person; 
    } 
} 

PersonRepository.java

import com.mycompany.smsdatabase.domain.Person; 
import org.springframework.data.neo4j.core.GraphDatabase; 
import org.springframework.data.neo4j.repository.GraphRepository; 
import org.springframework.data.neo4j.repository.NamedIndexRepository; 

public interface PersonRepository extends GraphRepository<Person>, NamedIndexRepository<Person>{ 
    Person findById(String id); 
} 

Person.java

package com.mycompany.smsdatabase.domain; 
import java.util.Date; 
import org.springframework.data.neo4j.annotation.GraphId; 
import org.springframework.data.neo4j.annotation.Indexed; 
import org.springframework.data.neo4j.annotation.NodeEntity; 
import org.springframework.data.neo4j.support.index.IndexType; 

@NodeEntity 
public class Person { 
    @GraphId Long nodeId; 
    @Indexed(unique=true) 
    String id; 
    @Indexed(indexType=IndexType.FULLTEXT, indexName = "people") 
    String name; 
    private Date birthday; 

    public Person(String id, String name) 
    { 
     this.id = id; 
     this.name = name; 
    } 
    public String getId() { 
     return id; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 
    public void setBirthday(Date birthday) { 
     this.birthday = birthday; 
    } 
} 

web.xml

<?xml version="1.0" encoding="UTF-8"?> 

<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    version="3.0"> 

    <context-param> 
     <param-name>contextClass</param-name> 
     <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> 
    </context-param> 

    <!-- Location of Java @Configuration classes that configure the components that makeup this application --> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>com.mycompany.smsdatabase.controller 
        /WEB-INF/applicationContext.xml 
     </param-value> 
    </context-param> 

    <!-- Creates the Spring Container shared by all Servlets and Filters --> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <!-- Ensure UTF-8 character encoding is used --> 
    <filter> 
     <filter-name>encodingFilter</filter-name> 
     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 
     <init-param> 
      <param-name>encoding</param-name> 
      <param-value>UTF-8</param-value> 
     </init-param> 
     <init-param> 
      <param-name>forceEncoding</param-name> 
      <param-value>true</param-value> 
     </init-param> 
    </filter> 

    <filter-mapping> 
     <filter-name>encodingFilter</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 

    <!-- Enables support for DELETE and PUT request methods with web browser clients --> 
    <filter> 
     <filter-name>hiddenHttpMethodFilter</filter-name> 
     <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> 
    </filter> 

    <filter-mapping> 
     <filter-name>hiddenHttpMethodFilter</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 

    <servlet> 
     <servlet-name>appServlet</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/dispatcherServlet.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>appServlet</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

    <servlet> 
     <servlet-name>H2Console</servlet-name> 
     <servlet-class>org.h2.server.web.WebServlet</servlet-class> 
     <init-param> 
      <param-name>-webAllowOthers</param-name> 
      <param-value>true</param-value> 
     </init-param> 
     <load-on-startup>2</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>H2Console</servlet-name> 
     <url-pattern>/admin/h2/*</url-pattern> 
    </servlet-mapping> 

    <session-config> 
     <session-timeout> 
      30 
     </session-timeout> 
    </session-config> 
</web-app> 

DispatchServlet.xml

<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" 
    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"> 

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

    <bean 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix"> 
      <value>/WEB-INF/</value> 
     </property> 
     <property name="suffix"> 
      <value>.jsp</value> 
     </property> 
    </bean> 

</beans> 

Anwendung context.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"> 

    <context:annotation-config/> 
    <context:component-scan base-package="com.mycompany.smsdatabase"> 
     <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 
    </context:component-scan> 

    <context:spring-configured/> 

    <bean 
class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> 

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

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

gelöst Sie jemals herausfinden? Ich habe das gleiche genaue Problem, ähnliche Konfiguration. – PJH

Antwort

-1

Es ist sehr klar aus der Ausnahme ist, dass keine Bohne verfügbar vom Typ PersonImport in Ihre Anwendungskontexte. Deklarieren Sie die Person importieren Bean in Ihrer Konfigurationsdatei und versuchen Sie es dann.

<bean id="personImport" class="com.mycompany.smsdatabase.service.PersonImport"/> 
+0

Ich habe das versucht und bekomme immer noch den gleichen Fehler, den ich oben gepostet habe. Das macht mich wahnsinnig verrückt Ich habe versucht, dies zu arbeiten für Tage ohne vorherige – IceNine

0

Try PersonImport Schnittstelle und benennen Klasse PersonImport-PersonImportImpl implements PersonImport zu machen.

+0

Ich versuchte dies auch, es hat nicht funktioniert – IceNine

-1

Entfernen Sie den Ausschlussfilter aus Ihrer Spring-Konfiguration. Es teilt Spring mit, alle Klassen mit Anmerkungen zu ignorieren. Daher werden keine Autowired-Objekte in den HomeController eingespeist.

+0

Ich nahm das heraus und ich bekomme immer noch die gleichen Fehler – IceNine

4

Ich hatte das gleiche Problem und eine Lösung gefunden.

Das Problem ist, dass Ihre Klasse Proxy ist (entweder von @transactional oder etwas ähnliches). Spring kann nicht erkennen, dass dieser Proxy eine Instanz Ihrer Klasse ist, da diese an eine Schnittstelle weitergegeben wird.

  1. ein interface PersonImport und @Autowire es anstelle der Klasse erstellen:

    Ich habe zwei Lösungen gefunden.

  2. Verwendung: proxy-target-class="true" in <tx:annotation-driven>
0

einfach die folgenden Bean Verdrahtung XML-Datei (contextConfigLocation)

<bean id="name of the object" class="com.java.model.FirstModelImple" /> 

in meinem Fall hinzufügen, ist dies hilfreich und ma Problem/Exception

Verwandte Themen