1

We've built a Spring service discovery solution that autowires several local classes annotated with @Component or @Service through a component scan at the root package level via @SpringBootApplication. The project follows a standard maven/spring layout:

project
  src/main/java
    service
      - ServiceA.java
      - ServiceB.java
      ...
    model
      - modelA.java
      ...
    component
      - ComponentA.java
      - ComponentB.java

    BootApp.java
    pom.xml

We'd like to now distribute this internally as a library, and doing so requires a spring.factories file (see how marathon does it / see spring autoconfig docs). Looking at other service-discovery implementations like marathon and eureka, it appears that I should remove all autowiring annotations (ie. @Component and @Service) and instead manually configure all of the components/beans into one or more @Configuration classes. The app is already autowiring and bootstrapping over ten classes- I'd prefer to not have to refactor and manually wire these up.

My question: is there any way to simply retain the existing autowiring/project structure and have spring.factories pick it up in something similar to a component scan? Something like:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=project.BootApp

or

org.springframework.boot.autoconfigure.EnableAutoConfiguration=project.*

Are there any other tips or best practices on how refactor to support spring.factories without having a massive @Configuration class? Is it possible to split the difference and have most of our internal objects autowired but then manually configure public objects like DiscoveryClient and ServiceRegistery (see spring cloud SPI) and reference them in spring.factories?

1 Answer 1

1

Turns out that it is possible to use @ComponentScan in a very similar way to @SpringBootApplication

@Configuration
@ComponentScan("com.bnymellon.tsg.discovery.springcloud.autoconfig")
public class BootApp {

}

The key difference is that @SpringBotApplication contains @EnableAutoConfiguration, which doesn't play nice here.

PS - I was not inheriting from spring-starter-parent so spring.factories was not included in the built artifact. Had to add the following to pom.xml

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.factories</include>
            </includes>
        </resource>
    </resources>
</build>

I guess at the end of the day, autowiring through annotations (eg. @Service) is a valid approach and I was incorrect in the question to presume it was incompatible with spring.factories.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.