【问题标题】:Injection of dependecy failed in multiple maven modules在多个 Maven 模块中注入依赖项失败
【发布时间】:2015-09-09 07:12:11
【问题描述】:

嗨,朋友们,我正在开发多个基于 Maven 模块的项目,并且我已经开发了一个运行良好的演示项目。项目结构是这样的

parent
    child1 
       src
       pom.xml
    child2
       src
       pom.xml
pom.xml

我的父母 pom 是

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <packaging>pom</packaging>
    <modules>
        <module>ChildModule1</module>
        <module>ChildModule2</module>
    </modules>


    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>1.2.5.RELEASE</version>
        <relativePath />
    </parent>

    <groupId>com.aquevix.parent</groupId>
    <artifactId>parentProject</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>

    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

而我的 child1 pom.xml 是

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>parentProject</artifactId>
        <groupId>com.aquevix.parent</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <packaging>jar</packaging>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>ChildProject1</artifactId>

</project>

child2 pom.xml 是

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>parentProject</artifactId>
        <groupId>com.aquevix.parent</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <packaging>jar</packaging>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>ChildProject2</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.aquevix.parent</groupId>
            <artifactId>ChildProject1</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>com.aquevix.rest.App</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

在 Child1 maven 模块中,我添加了一个用户控制器,并在其中添加了 UserService 对象,但是当我运行我的项目时它失败了

我使用这个命令运行我的项目

mvn -X exec:java -pl ChildModule2 -Dexec.mainClass="com.aquevix.rest.App"

它给了我这个错误

[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java (default-cli) on project ChildProject2: An exception occured while executing the Java class. null: InvocationTargetException: Error creating bean with name 'userController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.aquevix.service.UserService com.aquevix.rest.UserController.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.aquevix.service.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.inject.Inject()} -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java (default-cli) on project ChildProject2: An exception occured while executing the Java class. null
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:216)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)

【问题讨论】:

  • 发布您的App 课程。为什么不使用springBoot:run 来运行您的应用程序?

标签: java maven dependency-injection spring-boot


【解决方案1】:

我得到了解决方案。我默认覆盖组件扫描 @ComponentScan("com.aquevix") 它仅将所有类搜索到“com.aquevix.rest”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-01
    • 2016-04-25
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 2016-08-21
    相关资源
    最近更新 更多