Barium Scoorge
Barium Scoorge

Reputation: 2018

Custom Spring Boot Starter with commons components

We run a few Spring boot apps based on the sames principles, such as :

Today we are basing our apps using a Spring Boot parent app. It works quite nicely, but it seems better to use Spring Boot Starter mecanism.

Some questions about Spring Boot Starter best practices :

Service interface

package net.mytest.boot.starter;

public interface Tooling {
    void log(String message);
}

Service Implementation

package net.mytest.boot.starter;

// Is service is "allowed" inside a starter ?
@Service
public class ToolingImpl implements Tooling {
    Logger log = LoggerFactory.getLogger(Tooling.class);

    @Autowired
    RepositoryLog repositoryLog;

    public void log(String message) {
        log.debug("Logging in DB " + message);
        EntityLog entityLog  = new EntityLog(UUID.randomUUID().toString(), message);

        repositoryLog.save(eLog);
    }
}

Service Configuration

package net.mytest.boot.starter;

@Configuration
@ComponentScan("net.mytest.boot.starter")
@EntityScan("net.mytest.boot.starter.db")
@EnableJpaRepositories
public class ToolingAutoConfiguration {

// If @Service is not used, this allows autowiring Tooling
//    @Bean
//    public Tooling toolingConfig() {
//        return new ToolingImpl();
//    }

}

In this example, ToolingAutoConfiguration is used in both ways : - Defining Spring Boot Starter configuration (@Configuration, @ComponentScan..) - Provide a service (Tooling)

If we're using it only to define our Starter configuration, does it make sense to provide some AutoConfiguration class without any @Bean or @Service ?

Starter Configuration only

package net.mytest.boot.starter;

@Configuration
@ComponentScan("net.mytest.boot.starter")
@EntityScan("net.mytest.boot.starter.db")
@EnableJpaRepositories
public class ToolingAutoConfiguration {
}

Upvotes: 1

Views: 1796

Answers (1)

dev4Fun
dev4Fun

Reputation: 1050

The best way to learn how to create startes with autoconfiguration is to check official spring boot starters. For example, autoconfiguration for spring boot web starter.

Most of your questions are opinion based and there is really no right answer to it. I will provide my opinion based my experience implementing autoconfigurations.

Is it ok to define some @Controller or @Service in a starter dependency?

I personally would keep implmentations as clean as possible from Spring (avoid if not really needed). Just declare implementations as @Bean in your configuration. This way if you need to add conditions (which might need to take multiple components into consideration) later you can do it from centralized place instead of digging into each separate class.

Is it ok to define common exception/error processing using @ControllerAdvice in a starter?

I would say no. The reason is that @ControllerAdvice has options to specify when controller advice should be applied. What if users of a starter don't want to apply advice to all controllers or they want different advise being applied? I would consider more flexible approach, maybe abstract class that users can extend?

If not using the @Conditional... annotations, is it ok to do the following?

Conditionals usually used when autoconfigured components might have undesired side effects on existing application, need other components to function or should be turned on/off based on property values. It's totally up to you to configure whatever is needed and be responsible for any side effects.

Overall from what I see you might not even need autoconfiguration. What you might need is a set of Configuration classes that can be put in common library that can be imported with something like @Import individually and be fine tuned by users if necessary. The real power of autoconfiguration is hiding complexity of configuring multiple components that work together, dealing with all the side effects and being able to switch from one autoconfiguration to another. The flexibility aspect is more controlled by autocofiguration author than its user.

Upvotes: 2

Related Questions