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
@Controller
). - SpringRestControllerComponentFinderStrategy: A component finder strategy that finds Spring REST controllers (classes annotated
@RestController
). - SpringServiceComponentFinderStrategy: A component finder strategy that finds Spring services (classes annotated
@Service
). - SpringComponentComponentFinderStrategy: A component finder strategy that finds Spring components (classes annotated
@Component
). - SpringRepositoryComponentFinderStrategy: A component finder strategy for Spring repositories (classes annotated
@Repository
, plus those that extendJpaRepository
orCrudRepository
). - SpringComponentFinderStrategy: A combined component finder strategy that uses all of the individual strategies listed above.
Some of the Spring annotations (e.g. @Component
, @Service
and @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
CustomerRepository
is preferable toJdbcCustomerRespository
. - 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
, Interface*
and *Interface*
).
For example, the following implementation classes will match an interface named CustomerRepository
:
JdbcCustomerRepository
CustomerRepositoryImpl
JdbcCustomerRepositoryImpl
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.