Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Can';无法从外部项目获取bean_Java_Spring - Fatal编程技术网

Java Can';无法从外部项目获取bean

Java Can';无法从外部项目获取bean,java,spring,Java,Spring,我有两个项目:用户服务和数据模型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/

我有两个项目:用户服务和数据模型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
应用程序:

@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

如果我理解正确,问题在于接线接口。我如何解决这个问题(如果可能的话,在不更改数据模型dao的情况下)?

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

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


@Service
public class UserService {

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

    //methods
}

确保您已经在
用户服务
@ComponentScan(basePackages={“pl.moza.user.*,”pl.moza.dao“})中添加了
数据模型dao的依赖项
覆盖了实际需要的路径

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

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


@Service
public class UserService {

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

    //methods
}

确保您已经在
用户服务
@ComponentScan(basePackages={“pl.moza.user.*,”pl.moza.dao“})中添加了
数据模型dao的依赖项
覆盖了实际需要的路径

用户接口的具体类实现在哪里?它在datamodel dao/pl.moza/resources/pl.moza.dao/UserInterface.xml中,它映射在datamodel dao/pl.moza/resources/mybatis.configuration.xml中。用户接口的具体类实现在哪里?它在datamodel dao/pl.moza/resources/pl.moza.dao/UserInterface.xml中数据模型dao/pl.moza/resources/mybatis.configuration.xmlGot对此进行了映射,但感谢您的回答:)实际问题在于用户接口,这是mybatis与数据库通信的东西,它导致了问题。我可以加载UserDAO,但不能加载UserInterface。根据Spring官方文档,Spring不支持接口注入。@Elas,根据Spring官方文档,Spring不支持接口注入<代码>1。构造函数注入(例如Pico容器,Spring支持它)。2.设定器注入(如弹簧支撑)。3.接口注入(例如Avalon,Spring不支持它)。
有关我的batis配置的更多信息,请参阅和。必须做一些魔术,但它毕竟起了作用:在用户服务中创建了接口,这些接口从用户dao扩展了相同的接口(没有添加任何内容)将xml与MyBatis SQL连接到用户服务,然后我必须对DAO使用的每个接口(用户服务版本)使用@Autowired,并对每个接口使用setFooInterface。我明白了这一点,但感谢您的回答:)实际问题是用户接口,这是MyBatis与DB通信的问题,它导致了问题。我可以加载UserDAO,但不能加载UserInterface。根据Spring官方文档,Spring不支持接口注入。@Elas,根据Spring官方文档,Spring不支持接口注入<代码>1。构造函数注入(例如Pico容器,Spring支持它)。2.设定器注入(如弹簧支撑)。3.接口注入(例如Avalon,Spring不支持它)。
有关我的batis配置的更多信息,请参阅和。必须做一些魔术,但它毕竟起了作用:在用户服务中创建了接口,这些接口从用户dao扩展了相同的接口(没有添加任何内容)将带有MyBatis SQL的xml复制到用户服务,然后我必须对DAO使用的每个接口(用户服务版本)使用@Autowired,并对每个接口使用setFooInterface