java - 无法从外部项目获取bean

标签 java spring

我有两个项目:user-services 和 datamodel-dao。 在用户服务中,我尝试从 datamodel-dao 获取 UserDao,但出现以下错误:

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

来自数据服务的类 服务,我正在尝试获取 dao。

@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);
    }
}

来自 datamodel-dao:

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

UserInterface是MyBatis的接口(interface),如下所示:

public interface UserInterface {

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

如果我理解正确的话,问题出在接线接口(interface)上。我该如何解决这个问题(如果可能的话 - 不更改 datamodel-dao)?

最佳答案

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

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


@Service
public class UserService {

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

    //methods
}

确保您已在 user-services@ComponentScan(basePackages = {"pl.moza.user.* ", "pl.moza.dao"}) 覆盖实际所需的路径。

关于java - 无法从外部项目获取bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45462177/

相关文章:

java - 在 Java 中检查文本的字体属性

java - 根据正在序列化的类修改对象序列化

Java Webapp 在 Eclipse 中运行良好,但在 Tomcat 中运行不佳

Spring 3.0 错误 : java. lang.NoClassDefFoundError: org/aspectj/lang/NoAspectBoundException

java - 有没有办法获取xml文件中的Bean ID

java - Spring 和 Hibernate 混搭,是 @Entity 的代理对象,并添加了额外的 @Service

java - 查询可能不存在的行的正确方法 - Java8

Java图像数组显示

java - Skinny WAR,EAR : "struts-tags not found" error 中的库

java - Autowired 在自定义约束 validator 中给出 Null 值