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

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

它给出了以下错误 .


应用程序未能启动


描述:

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

行动:

考虑在配置中定义类型为“service.ApiCaller”的bean .

示例应用程序(一个抛出错误)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>

Spring 天工厂代码

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

MyAPI产品

@Service
@Configuration
public class ApiCaller {

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