java – Custom Spring Boot Starter with commons components

java – Custom Spring Boot Starter with commons components

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 dont 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. Its 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.

java – Custom Spring Boot Starter with commons components

Leave a Reply

Your email address will not be published. Required fields are marked *