2016-06-21 5 views
0

나는 스프링 툴 스위트로 스프링 부트 애플리케이션을 만들었습니다. '봄의 부팅 응용 프로그램'스프링 부트 애플리케이션의 org.springframework.beans.factory.BeanCreationException

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

,691,363 (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); 
    } 
} 

내가 HomeController에서 @Autowired 주석을 제거하지만 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

'SpringBootDemoApplication'을 추가하고'TaskServiceInterface'의 실제 구현은 어떤 패키지에 있습니까? –

+0

게시물이 업데이트되었습니다. – Muhammad

+0

'TaskServiceImplementation'에'@ Service'를 넣으십시오. 그것이 없으면 그것은 집어 들지 않을 것입니다. –

답변

0

당신은 당신의 구현에 주석을 누락로 응용 프로그램을 시작할 때 NullPointerException을받을 때 잘 실행 . 구성 요소 검색을 사용하여 픽업하려면 봄에 @Service 또는 다른 @Component 주석을 주석으로 첨부해야합니다.

관련 문제