Spring component finder strategies
Included in the
structurizr-spring library are a number of component finder strategies that help you identify components in Spring applications, including those built using Spring Boot.
- SpringMvcControllerComponentFinderStrategy: A component finder strategy that finds Spring MVC controllers (classes annotated
- SpringRestControllerComponentFinderStrategy: A component finder strategy that finds Spring REST controllers (classes annotated
- SpringServiceComponentFinderStrategy: A component finder strategy that finds Spring services (classes annotated
- SpringComponentComponentFinderStrategy: A component finder strategy that finds Spring components (classes annotated
- SpringRepositoryComponentFinderStrategy: A component finder strategy for Spring repositories (classes annotated
@Repository, plus those that extend
- SpringComponentFinderStrategy: A combined component finder strategy that uses all of the individual strategies listed above.
Spring naming conventions and interfaces vs implementation classes
Some of the Spring annotations (e.g.
@Repository) are typically used to annotate implementation classes. For example, a
JdbcCustomerRepository class might be annotated
@Repository, rather than the
CustomerRepository interface. This component finder strategy tries to refer to interface types rather than implementation classes, based upon the following assumptions:
- Having a component named
CustomerRepositoryis preferable to
- Other types in the codebase will likely have a dependency on the interface type (e.g. via dependency injection) rather than being coupled to the implementation class.
Given that a class can implement any number of interfaces, for a given implementation class, the component finder strategies will try to find the interface where the name of that interface is included in the name of the implementation class (i.e.
*Interface*). For example, the following implementation classes will match an interface named
Public vs non-public types
By default, non-public types will be ignored so that, for example, you can hide repository implementations behind services, as described at Whoops! Where did my architecture go. Use the
setIncludePublicTypesOnly method to change this behaviour.
You can see an example of how to use the Spring component finder strategies in the Spring PetClinic example.