This is the Spring Framework Plugin of jQAssistant. It provides pre-defined rules for projects using the Spring Framework, e.g.:
-
Spring Boot
-
Package layout, i.e. all classes of a Spring Boot application must be located in the package of the application class or a child package of it.
-
Considers classes annotated with
@SpringBootApplication
and@TestConfiguration
configuration classes and injectables.
-
-
Spring Components
-
Considers classes annotated with
@Component
,@Service
,@Repository
injectables. -
Considers classes that are returned from
@Bean
methods injectables. -
Considers Spring Data repositories injectables.
-
Verifies dependencies between Spring components, i.e. controllers (must depend on services, repositories or components), services (must depend on other services, repositories or components) and repositories (must depend on other repositories and components).
-
Requires
@Bean
methods to be used from with configuration classes only. -
Requires that JDK classes are not injectables.
-
-
Dependency Injection
-
Prevents field injection (except in tests, in Strict mode)
-
Requires injectables to be assigned to final fields (in Strict mode)
-
Rejects direct instantiation of injectable types, except in tests and
@Bean
methods. -
Ensures that fields of injectable types are not manipulated.
-
Ensures that injectables are never assigned to static fields or accessed via static methods.
-
Recommends to use
@PostConstruct
and@PreDestroy
over implementingInitializingBean
andDisposableBean
. -
Recommends to directly inject
BeanFactory
,ApplicationContext
, andApplicationEventPublisher
instead of implementing callback interfaces.
-
-
Transactions
-
Disallow direct invocation of methods annotated with
@Transaction
from methods not annotated with that annotation within the same class
-
For more information on jQAssistant see https://jqassistant.org.
- NOTE
-
Starting from jqassistant-spring-plugin v2.0.0, the Spring-Plugin resides under jqassistant-plugin. Therefore, not only the repository location changed but also group id and artifact id were adapted from
com.buschmais.jqassistant.plugin:spring
toorg.jqassistant.plugin:jqassistant-spring-plugin
. A manual configuration as shown below is required.
To execute rules, you just need to activate the desired group of rules, either "spring-boot:Default" or "spring-boot:Strict"
jqassistant:
plugins:
- group-id: org.jqassistant.plugin (1)
artifact-id: jqassistant-spring-plugin
version: ${jqassistant.spring-plugin.version}
analyze:
groups:
- spring-boot:Strict (2)
-
Dependency to the Spring plugin
-
Activates the group "spring-boot:Strict".
-
Moved Plugin from com.buschmais.jqassistant.plugin (core-Distribution) to org.jqassistant.plugin
-
Added concept
spring-security:ConfigurationComponent
to label classes annotated by@EnableGlobalAuthentication
,@EnableGlobalMethodSecurity
,@EnableReactiveMethodSecurity
,@EnableWebSecurity
,@EnableWebFluxSecurity
and@EnableWebMvcSecurity
as:Spring:Configuration:Component
-
Extended concepts
spring-transaction:TransactionalClass
andspring-transaction:TransactionalMethod
to accept JEE/JTA annotationjavax.transaction.@Transactional
-
Removed redundant result columns from constraints
spring-injection:BeanProducerMustNotBeInvokedDirectly
andspring-injection:InjectablesShouldBeHeldInFinalFields
-
Added concepts
spring-mvc:ControllerAdvice
andspring-mvc:RestControllerAdvice
-
Improved the constraints
spring-component:ControllerMustOnlyDependOnServicesRepositories
,spring-component:ServiceMustOnlyDependOnServicesRepositories
andspring-component:RepositoryMustOnlyDependOnRepositories
to report one row per invalid dependency (issue) -
Fixed false positives for constraint
spring-injection:InjectablesMustOnlyBeHeldInInjectables
for inner classes (issue) -
Improved constraint
spring-injection:InjectablesShouldBeHeldInFinalFields
to return theType
declaring theField
andInjectedType
as the injected field type.
-
Added constraint
spring-injection:BeanProducerMustNotBeInvokedDirectly
-
Allow dependencies from any Spring component to
@Component
annotated beans -
Defined primary columns for constraint results for better matching in SonarQube
-
Excluded generated Java types from Spring constraints, therefore the projects needs to extend the concept
java:GeneratedType
-
Excluded classes in test artifacts from constraint
spring-injection:InjectablesMustOnlyBeHeldInInjectables
-
fixed false-positive constraint violations for constraint
InjectablesMustNotBeInstantiated
for constructors invoking constructors from their super class -
the constraint
spring-injection:FieldsOfInjectablesMustNotBeManipulated
now allows manipulating fields from lifecycle callbacks (@PostConstruct
and@PreDestroy
)
-
Added meta-annotated Spring annotations to concepts, e.g.
@SpringBootApplication
will be also labeled with `Configuration -
Relaxed the constraint
spring-injection:InjectablesMustNotBeInstantiated
such that instantiation is allowed from either bean producer methods declared by injectables or non-injectables (issue) -
Relaxed the constraint
spring-injection:FieldsOfInjectablesMustNotBeManipulated
such that only fields of injectable types are checked for being written by non-constructor methods (issue) -
The constraints
spring-injection:FieldsOfInjectablesMustNotBeManipulated
,spring-injection:InjectablesMustNotBeHeldInStaticVariables
andspring-injection:InjectablesMustNotBeAccessedStatically
are improved such that also fields are considered where injectable types are injected by interfaces or super classes.
-
Renamed relation representing virtual dependencies between components from DEPENDS_ON to VIRTUAL_DEPENDS_ON (issue)