Spring启动定制启动器,应用程序找不到所需的bean

时间:2018-02-20 07:55:52

标签: java spring-boot spring-boot-starter

http://www.baeldung.com/spring-boot-custom-starter

我已经按照上面的链接提供的教程和github示例进行了类似的实现。我正在使用spring-boot-starter-parent :2.0.0.M3。即使在我的自定义启动器依赖项包含在应用程序的pom中之后,它也无法在部署时找到所需的bean @componentScan

出现以下错误。

申请失败

说明

com.core.controller.TestController中的字段fooApiCaller需要一个类型为' service.ApiCaller'的bean。无法找到。

动作:

考虑定义一个类型为&service的服务器.ApiCaller'在你的配置中。

示例应用程序(一次抛出错误) 的pom.xml

<dependency>
        <groupId>abc.def</groupId>
        <artifactId>custom-starter</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
@Controller
public class FooController {
@Autowired
ApiCaller fooApiCaller
}

custom-starter模块pom.xml

<dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>

        <dependency>
            <groupId>abc.def</groupId>
            <artifactId>custom-spring-boot-autoconfigure</artifactId>
            <version>${project.version}</version>
        </dependency>

        <dependency>
            <groupId>abc.def</groupId>
            <artifactId>myapi</artifactId>
            <version>${project.version}</version>
        </dependency>

    </dependencies>

自动配置模块依赖

 <dependencies>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot</artifactId>
                <version>${spring-boot.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-autoconfigure</artifactId>
                <version>${spring-boot.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <version>${spring-boot.version}</version>
                <optional>true</optional>
            </dependency>

            <dependency>
                <groupId>abc.def</groupId>
                <artifactId>myapi</artifactId>
                <version>${project.version}</version>
                <optional>true</optional>
            </dependency>

        </dependencies>
春天工厂代码

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
abc.def.myapi.autoconfigure.AutoConfiguration

MyAPI产品

@Service
@Configuration
public class ApiCaller {

public String getName(String Id){return "name";}
}

0 个答案:

没有答案
相关问题