Package io.github.cpetot.archunit
Class SpringCodingRules
- java.lang.Object
-
- io.github.cpetot.archunit.SpringCodingRules
-
public final class SpringCodingRules extends Object
SpringCodingRules provides a set of generalArchConditions
andArchRules
for coding that might be useful in the projects using Spring.
-
-
Field Summary
Fields Modifier and Type Field Description static com.tngtech.archunit.lang.ArchCondition<com.tngtech.archunit.core.domain.JavaClass>
BE_ACCESSED_BY_TRANSACTIONAL_CLASSES_OR_METHODS
A condition that checks if the methods of Java class are only called by methods annotated byTransactional
, directly on the method or at the class level.static com.tngtech.archunit.lang.ArchRule
REPOSITORIES_ARE_ACCESSED_ONLY_BY_SERVICE_CLASSES
A rule that checks that all classes annotated byRepository
are only accessed by classes annotated byService
.static com.tngtech.archunit.lang.ArchRule
REPOSITORIES_ARE_ACCESSED_ONLY_BY_SERVICE_OR_CONTROLLER_CLASSES
A rule that checks that all classes annotated byRepository
are only accessed by classes annotated byService
or byController
Depending on your architecture, you may want to enforce this rule.static com.tngtech.archunit.lang.ArchRule
REPOSITORIES_ARE_ACCESSED_ONLY_BY_TRANSACTIONAL_METHODS_OR_CLASSES
A rule that checks that all methods in a SpringRepository
are only called by methods annotated byTransactional
, directly on the method or at the class level.
-
-
-
Field Detail
-
BE_ACCESSED_BY_TRANSACTIONAL_CLASSES_OR_METHODS
@PublicAPI(usage=ACCESS) public static final com.tngtech.archunit.lang.ArchCondition<com.tngtech.archunit.core.domain.JavaClass> BE_ACCESSED_BY_TRANSACTIONAL_CLASSES_OR_METHODS
A condition that checks if the methods of Java class are only called by methods annotated byTransactional
, directly on the method or at the class level.If you use programmatic transaction management with
@Transactional
, it is recommended to always open a transaction (readonly or not) before calling a repository's method.Supposing you have such a repository :@Repository public class FooRepository { public void saveFoo(Foo) { // Impl } }
Valid examples :
or@Transactional public class FooService { private final FooRepository repository; public FooService(FooRepository repository) { this.repository = repository; } // No annotation here, but present at class level public void createFoo() { Foo foo = new Foo(); // Some code repository.saveFoo(foo); } }
// No annotation here, but present on the method public class FooService { private final FooRepository repository; public FooService(FooRepository repository) { this.repository = repository; } @Transactional public void createFoo() { Foo foo = new Foo(); // Some code repository.saveFoo(foo); } }
Invalid examples :// No annotation here public class FooService { private final FooRepository repository; public FooService(FooRepository repository) { this.repository = repository; } // No annotation here neither public void createFoo() { Foo foo = new Foo(); // Some code repository.saveFoo(foo); } }
-
REPOSITORIES_ARE_ACCESSED_ONLY_BY_TRANSACTIONAL_METHODS_OR_CLASSES
@PublicAPI(usage=ACCESS) public static final com.tngtech.archunit.lang.ArchRule REPOSITORIES_ARE_ACCESSED_ONLY_BY_TRANSACTIONAL_METHODS_OR_CLASSES
A rule that checks that all methods in a SpringRepository
are only called by methods annotated byTransactional
, directly on the method or at the class level.If you use programmatic transaction management with
@Transactional
, it is recommended to always open a transaction (readonly or not) before calling a repository's method.Supposing you have such a repository :@Repository public class FooRepository { public void saveFoo(Foo) { // Impl } }
Valid examples :
or@Transactional public class FooService { private final FooRepository repository; public FooService(FooRepository repository) { this.repository = repository; } // No annotation here, but present at class level public void createFoo() { Foo foo = new Foo(); // Some code repository.saveFoo(foo); } }
// No annotation here, but present on the method public class FooService { private final FooRepository repository; public FooService(FooRepository repository) { this.repository = repository; } @Transactional public void createFoo() { Foo foo = new Foo(); // Some code repository.saveFoo(foo); } }
Invalid examples :// No annotation here public class FooService { private final FooRepository repository; public FooService(FooRepository repository) { this.repository = repository; } // No annotation here neither public void createFoo() { Foo foo = new Foo(); // Some code repository.saveFoo(foo); } }
-
REPOSITORIES_ARE_ACCESSED_ONLY_BY_SERVICE_CLASSES
@PublicAPI(usage=ACCESS) public static final com.tngtech.archunit.lang.ArchRule REPOSITORIES_ARE_ACCESSED_ONLY_BY_SERVICE_CLASSES
A rule that checks that all classes annotated byRepository
are only accessed by classes annotated byService
.Depending on your architecture, you may want to enforce this rule. If you do so, you will have errors if a repository is accessed by :- another class annotated by
Repository
, - a class annotated by
Controller
, - a class without any annotation.
- another class annotated by
-
REPOSITORIES_ARE_ACCESSED_ONLY_BY_SERVICE_OR_CONTROLLER_CLASSES
@PublicAPI(usage=ACCESS) public static final com.tngtech.archunit.lang.ArchRule REPOSITORIES_ARE_ACCESSED_ONLY_BY_SERVICE_OR_CONTROLLER_CLASSES
A rule that checks that all classes annotated byRepository
are only accessed by classes annotated byService
or byController
Depending on your architecture, you may want to enforce this rule. If you do so, you will have errors if a repository is accessed by :- another class annotated by
Repository
, - a class without any annotation.
- another class annotated by
-
-