2016-06-21 12 views
0

Ich habe eine Spring-Boot-Anwendung mit Spring Tool-Suite erstellt. Ich erhalte folgende Fehlermeldung, wenn sie als 'Spring-Boot-App'org.springframework.beans.factory.BeanCreationException im Frühjahr Boot-Anwendung

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: public com.frameworkonly.webapp.service.TaskServiceInterface com.frameworkonly.webapp.controller.HomeController.taskServiceInterface; 
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type [com.frameworkonly.webapp.service.TaskServiceInterface] 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:334) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE] 
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE] 
    at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE] 
    at com.frameworkonly.webapp.SpringBootDemoApplication.main(SpringBootDemoApplication.java:10) [classes/:na] 
Caused by: org.springframework.beans.factory.BeanCreationException: 
Could not autowire field: public com.frameworkonly.webapp.service.TaskServiceInterface com.frameworkonly.webapp.controller.HomeController.taskServiceInterface; 
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type [com.frameworkonly.webapp.service.TaskServiceInterface] 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:573) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    ... 17 common frames omitted 

HomeController.java

package com.frameworkonly.webapp.controller; 

import java.util.ArrayList; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.RequestMapping; 

import com.frameworkonly.webapp.domain.Task; 
import com.frameworkonly.webapp.service.TaskServiceInterface; 

@Controller 
public class HomeController { 
    @Autowired 
    public TaskServiceInterface taskServiceInterface; 

@RequestMapping("/home") 
public String home(Model model) { 
    model.addAttribute("allTasks", (ArrayList<Task>)taskServiceInterface.getAllTasks()); 
    System.out.print("calling tasks"); 
    return "tasks"; 
} 
} 

TaskServiceInterface

package com.frameworkonly.webapp.service; 

import java.util.Collection; 

import com.frameworkonly.webapp.domain.Task; 

public interface TaskServiceInterface { 

    public Task saveTask(Task task); 

    public Boolean deleteTask(Long taskId); 

    public Task editTask(Task task); 

    public Task findTask(Long taskId); 

    public Collection<Task> getAllTasks(); 
} 

TaskServiceImplementation

läufteine Anmerkung auf Ihrer Implementierung 210
package com.frameworkonly.webapp.service; 

import java.util.Collection; 

import org.springframework.beans.factory.annotation.Autowired; 

import com.frameworkonly.webapp.domain.Task; 
import com.frameworkonly.webapp.repository.TaskRepository; 

public class TaskServiceImplementation implements TaskServiceInterface{ 

    @Autowired 
    protected TaskRepository taskRepository; 

    @Override 
    public Task saveTask(Task task) { 
     return taskRepository.save(task); 
    } 

    @Override 
    public Boolean deleteTask(Long taskId) { 
     Task temp = taskRepository.findOne(taskId); 
     if(temp!=null){ 
      taskRepository.delete(temp); 
      return true; 
     } 
     return false; 
    } 

    @Override 
    public Task editTask(Task task) { 
     return taskRepository.save(task); 
    } 

    @Override 
    public Task findTask(Long taskId) { 
     return taskRepository.findOne(taskId); 
    } 

    @Override 
    public Collection<Task> getAllTasks() { 
     Iterable<Task> itr = taskRepository.findAll(); 
     return (Collection<Task>)itr; 
    } 

} 

SpringBootDemoApplication

package com.frameworkonly.webapp; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 

@SpringBootApplication 
public class SpringBootDemoApplication { 

    public static void main(String[] args) { 
     SpringApplication.run(SpringBootDemoApplication.class, args); 
    } 
} 

es läuft gut, wenn ich @Autowired Anmerkung aus HomeController entfernen, aber NullPointerException erhalten, wenn die App als localhost:8080/home

java.lang.NullPointerException: null 
    at com.frameworkonly.webapp.controller.HomeController.home(HomeController.java:21) ~[classes/:na] 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_77] 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_77] 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_77] 
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_77] 
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) ~[spring-web-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) ~[spring-web-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) ~[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:832) ~[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:743) ~[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:961) ~[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:895) ~[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967) ~[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:858) ~[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622) ~[tomcat-embed-core-8.0.33.jar:8.0.33] 
+0

Fügen Sie Ihre 'SpringBootDemoApplication' hinzu und auch in welchem ​​Paket befindet sich die eigentliche Implementierung des 'TaskServiceInterface'? –

+0

Beitrag mit diesen Klassen aktualisiert. – Muhammad

+0

Setzen Sie '@ Service' auf' TaskServiceImplementation '. Ohne sie wird es nicht abgeholt. –

Antwort

0

Sie starten fehlt . Es sollte mit @Service oder einer anderen @Component Annotation für die Feder kommentiert werden, um es mit Komponenten-Scannen zu erfassen.