2017-08-02 84 views
0

我有两个项目:user-services和datamodel-dao。 在用户服务我试图从数据模型的DAO得到userDAO的,但我得到这个错误:从数据服务 服务,在这里,我试图让道无法从外部项目获取bean

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService' defined in file [/home/khliwa/projects/moza-core-services/moza-user-services/target/classes/pl/moza/user/service/UserService.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [pl.moza.user.service.UserService]: Constructor threw exception; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [pl.moza.dao.UserInterface] is defined 
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:275) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:838) ~[spring-context-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537) ~[spring-context-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE] 
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE] 
    at org.springframework.boot.SpringApplication.doRun(SpringApplication.java:347) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:295) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1112) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1101) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE] 
    at pl.moza.user.UserApplication.main(UserApplication.java:18) [classes/:na] 
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [pl.moza.user.service.UserService]: Constructor threw exception; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [pl.moza.dao.UserInterface] is defined 
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:163) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:122) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:267) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
    ... 18 common frames omitted 
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [pl.moza.dao.UserInterface] is defined 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:372) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:332) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1063) ~[spring-context-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
    at pl.moza.user.service.UserService.<init>(UserService.java:28) ~[classes/:na] 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_131] 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_131] 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_131] 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_131] 
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
    ... 20 common frames omitted 

类。

@Service 
public class UserService { 
    UserDao userDao; 

    @Autowired 
    public UserService(ApplicationContext ctx) { 
     userDao = (UserDao) ctx.getBean(UserDao.class); 
    } 
    //methods 

应用:

@Controller 
@SpringBootApplication 
@EnableAsync 
@ComponentScan(basePackages = {"pl.moza.user.*", "pl.moza.dao"}) 
public class UserApplication extends WebMvcConfigurerAdapter { 

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

而从数据模型-DAO:

public class UserDao implements Serializable{ 
    @Inject 
    UserInterface userInterface; 
//methods 
} 

的UserInterface是MyBatis的接口,看起来像这样:

public interface UserInterface { 

    public UserApp getUserByLogin(String login) throws Exception; 
    //more methods 

问题是与布线接口如果我小心并正确。我该如何解决这个问题(如果可能的话 - 不改变datamodel-dao)?

+0

UserInterface的具体类实现在哪里? – Plog

+0

它在datamodel-dao/pl.moza/resources/pl.moza.dao/UserInterface.xml中,它映射在datamodel-dao/pl.moza/resources/mybatis.configuration.xml中 – Elas

回答

1

在这里,您没有使用@ComponentUserDao的任何等效xml配置,那么spring容器(应用程序上下文)将不会创建任何bean,然后spring容器不负责创建或管理它的实例。 另一件事是我不明白你为什么使用@InjectuserInterface ???相反,您可以在中的UserDao@Autowired中实现它。

@Repository("userDao") 
public class UserDao implements UserInterface { 
    //methods 
} 


@Service 
public class UserService { 

    @Autowired 
    @Qualifier("userDao") 
    UserDao userDao; 

    //methods 
} 

确保您已经添加的datamodel-dao依赖于user-services@ComponentScan(basePackages = {"pl.moza.user.*", "pl.moza.dao"})涵盖实际所需的路径。

+0

得到了这个,但是谢谢你的回答:)实际的问题是UserInterface,它是与数据库通信的MyBatis事​​物,并且它引起了问题。我可以加载UserDAO,但不能UserInterface。 – Elas

+1

For your information按照Spring官方文档,spring不支持接口注入。 –

+1

@Elas,您的信息按照Spring官方文档,Spring不支持接口注入。 '1。构造器注入(例如Pico容器,Spring支持它)。 2. Setter Injection(例如Spring支持它)。 3.接口注入(例如Avalon,Spring不支持它)。'有关我的batis配置的更多信息,请参阅[here](http://www.mybatis.org/spring/mappers.html)和[here] (http://www.mybatis.org/spring/ko/sample.html)。 –