Skip to content

Annotation processing library for replacement most usable spring structures by annotations

Notifications You must be signed in to change notification settings

ArtemZip/spring-helpers

Repository files navigation

Spring Helpers (tmp name)

Description

Spring helpers is additional library for Spring framework which provides additional annotations for generation common spring structures (such as repositories, controllers, etc).

Architecture

This project contains three modules:

  • annotations-lib - contains only annotations which will be used in processors
  • processors-lib - contains processors which creates codes according to your annotation usage
  • experiment - temporary module, which will be used for testing during developing

Example

By the help of @JpaRepositoty will be created same named structure for your entities, which you will easily inject in your spring project. Moreover, you will be able to specify custom methods by additional annotation @Method

Example of entity
@Entity
@JpaRepository({
    @Method(name = "findAllByName", returnType = List.class, args = {String.class}),
    @Method(name = "findAllByNameAndId", returnType = List.class, args = {String.class, Long.class}),
    @Method(name = "getNameById", returnType = String.class, args = {Long.class}, query = "Select u.name from ExampleEntity u where u.id = ?1")
})
public class ExampleEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;
    
    //geters & setters
}
Example of generated code
public interface ExampleEntityRepository extends JpaRepository<ExampleEntity, Long> {
  
  List<ExampleEntity> findAllByName(String arg0);

  List<ExampleEntity> findAllByNameAndId(String arg0, Long arg1);

  @Query("Select u.name from ExampleEntity u where u.id = ?1")
  String getNameById(Long arg0);
}

Problems

  • doesn't exist direct access to annotated class, therefore there is no simple way to get class properties .
  • doesn't exist easy way to update annotated class, in this way should be used AST (Abstract syntax tree).
  • creating some classes could be too complicated to code (for instance, generic classes full of methods which directs to parameter or annotated class), as a possible solution will be added additional module with custom templating engine, which perform transformations from prepared code-templates to java classes.

Provided annotations

  • @JpaRepository - creates interface of JpaRepository<Entity,Id>
  • @RestCrudController - creates spring RestController with CRUD methods (validation will be provided)
  • @CrudController - creates spring Controller with CRUD methods (validation will be provided)
  • @CrudOperations - inserts to existing (Rest)Controller CRUD methods (validation will be provided)
  • TODO: think about other structures

Dependencies

  • Javapoet - library for easier way to write java classes in time of processing
  • Spring framework - source of data structures which will be generated by processing

About

Annotation processing library for replacement most usable spring structures by annotations

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published