Class SpringCodingRules


  • public final class SpringCodingRules
    extends Object
    SpringCodingRules provides a set of general ArchConditions and ArchRules for coding that might be useful in the projects using Spring.
    • 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 by Transactional, directly on the method or at the class level.

        If you use programmatic transaction management with &#64;Transactional, it is recommended to always open a transaction (readonly or not) before calling a repository's method.

        Supposing you have such a repository :
        
         &#64;Repository
         public class FooRepository {
        
         	public void saveFoo(Foo) {
         		// Impl
         	}
         }
        
         
        Valid examples :
        
         &#64;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);
         	}
         }
        
         
        or
        
         // No annotation here, but present on the method
         public class FooService {
        
         	private final FooRepository repository;
        
         	public FooService(FooRepository repository) {
         		this.repository = repository;
         	}
        
         	&#64;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 Spring Repository are only called by methods annotated by Transactional, directly on the method or at the class level.

        If you use programmatic transaction management with &#64;Transactional, it is recommended to always open a transaction (readonly or not) before calling a repository's method.

        Supposing you have such a repository :
        
         &#64;Repository
         public class FooRepository {
        
         	public void saveFoo(Foo) {
         		// Impl
         	}
         }
        
         
        Valid examples :
        
         &#64;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);
         	}
         }
        
         
        or
        
         // No annotation here, but present on the method
         public class FooService {
        
         	private final FooRepository repository;
        
         	public FooService(FooRepository repository) {
         		this.repository = repository;
         	}
        
         	&#64;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);
         	}
         }
        
         
        See Also:
        BE_ACCESSED_BY_TRANSACTIONAL_CLASSES_OR_METHODS
      • 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 by Repository are only accessed by classes annotated by Service.
        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.
        See Also:
        StandardCodingRules.beAccessedOnlyByClassesMetaAnnotatedBy(Class)
      • 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 by Repository are only accessed by classes annotated by Service or by Controller
        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.
        See Also:
        StandardCodingRules.beAccessedOnlyByClassesMetaAnnotatedBy(Class)