2015-11-06 118 views
-1

我正在嘗試實施Spring Boot並設置我的用戶服務。我遇到無法找到UserService bean的錯誤。任何建議或方向將不勝感激。Autowired issue/No bean found

UserController中

@RestController 
public class UserController { 

    private final IUserService userService; 

    @Inject 
    public UserController(final IUserService userService) { 
     this.userService = userService; 
    } 

    @RequestMapping(value = "/user", method = RequestMethod.POST) 
    public FBUser createUser(@RequestBody @Valid final FBUser fBUser) { 
     return userService.save(fBUser); 
    } 
} 

IUserService

public interface IUserService { 

    FBUser save(FBUser fBUser); 
} 

UserService

public class UserService implements IUserService { 

    private final IUserRepository repository; 

    @Inject 
    public UserService(final IUserRepository repository) { 
     this.repository = repository; 
    } 

    @Override 
    @Transactional 
    public FBUser save(final FBUser fBUser) { 
     return repository.save(fBUser); 
    } 

} 

IUserRepository

public interface IUserRepository extends JpaRepository<FBUser, Long> { 
} 

錯誤

Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController' defined in file [/Users/christopher/workspace/FeedAndBedding/target/classes/com/creativefuhsion/controllers/UserController.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.creativefuhsion.services.impls.UserService]: : No qualifying bean of type [com.creativefuhsion.services.impls.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.creativefuhsion.services.impls.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} 
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) 
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1137) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1040) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:759) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) 
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:117) 
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:689) 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:321) 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:969) 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:958) 
    at com.creativefuhsion.FeedAndBeddingApplication.main(FeedAndBeddingApplication.java:16) 
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.creativefuhsion.services.impls.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942) 
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813) 
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) 
... 18 more 
+0

您很可能使用JDK代理。在@ Transactional配置中將'proxy-class'設置爲true。 –

回答

6

註解你與@Service用戶服務:

@Service 
public class UserService implements IUserService { 

否則,當它掃描的東西注入春天不會發現它。

+0

完美!謝謝。這總是那些小錯誤。只需要第二組眼睛。 – clenard

+1

@clenard哦是的。在我身邊好幾次;) –