2015-11-06 76 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哦是的。在我身边好几次;) –