Public enumeration component Enum

Posted by sabbagh on Thu, 17 Feb 2022 11:35:40 +0100

rdf-component-enum

Do you worry about adding several methods to get values or enumeration every time you add an enumeration class? Although they are just copy and paste, they are still unbearable. The existence of this component is to solve this problem.

1, Effect

This component provides the basic functions of enumeration and some common methods. At the same time, it also supports the automatic acquisition of enumeration description and disclosure function.

1. Basic usage

  • The bold font content in the above figure is the defined enumeration field. The above method is the method provided by the component. The component definition fields include val value, code code code and desc description.

  • Method description

methodexplain
getByValGet enumeration object by enumeration value
getByDescGet enumeration object through enumeration description
getByCodeObtain enumeration object through enumeration code
getValByDescGet enumeration value through enumeration description
getCodeByDescObtain enumeration code through enumeration description
getDescByValGet enumeration description through enumeration value
getDescByCodeObtain enumeration description through enumeration code

2. Automatic disclosure of enumeration description

As a result of the response, the contents of the statusDesc field are revealed without manual setter

{
    "name": "rdf",
    "status": 1,
    "statusDesc": "give the thumbs-up"
}

2, Introduce dependency

  • Introducing maven dependency

The following dependencies can be directly added to the maven project pom. You can directly pull and change the dependencies, but only if your Gitee private server warehouse is configured! collocation method

<dependencies>
    <dependency>
        <groupId>com.lsl.rdf</groupId>
        <artifactId>rdf-component-enum</artifactId>
        <!-- Latest version -->
        <version>1.0.0-RELEASE</version>
    </dependency>
</dependencies>
  • Direct jar package import

The project jar package has been packaged and deployed to the personal private server warehouse. You can download the required jar and version by yourself! Download address

3, Mode of use

1. The basic enumeration class is defined in

  1. Define VCD type enumeration class. We define ordinary enumeration class, and then inherit the enumeration base class provided by us, so that we can use the methods provided by our base class. The specific definition method is as follows:
import com.lsl.rdf.enums.BasicsVCDEnum;

public class TypeEnum extends BasicsVCDEnum {
    public static final TypeEnum LIKE = new TypeEnum(1, "LIKE", "give the thumbs-up");
    public static final TypeEnum COLLECT = new TypeEnum(2, "COLLECT", "Collection");
    public static final TypeEnum TRANSMIT = new TypeEnum(3, "TRANSMIT", "forward");
    public static final TypeEnum ATTENTION = new TypeEnum(4, "ATTENTION", "follow");

    public TypeEnum(Integer val, String code, String desc) {
        super(val, code, desc);
    }
}
  1. Basic use
public class Test {

    public void test() {
        Integer val = TypeEnum.ATTENTION.getVal();
        String code = TypeEnum.ATTENTION.getCode();
        String desc = TypeEnum.ATTENTION.getDesc();

        TypeEnum attention = TypeEnum.getByCode(TypeEnum.class, "ATTENTION");
        TypeEnum attention1 = TypeEnum.getByDesc(TypeEnum.class, "follow");
        TypeEnum attention2 = TypeEnum.getByVal(TypeEnum.class, 4);

        String attention3 = TypeEnum.getCodeByDesc(TypeEnum.class, "follow");
        String attention4 = TypeEnum.getDescByCode(TypeEnum.class, "ATTENTION");
        String attention5 = TypeEnum.getDescByVal(TypeEnum.class, 4);
        Integer attention6 = TypeEnum.getValByDesc(TypeEnum.class, "follow");
    }
}
  1. explain

    • What is VCD type? V corresponds to val, C corresponds to code, and D corresponds to desc. VCD type means to provide support for these three fields. The use method is to inherit the base class enumeration class of VCD,
      Here, we provide five types of enumeration base classes. You can choose the inherited enumeration base class according to your actual needs;
    Enumerating base classesSupport field
    BasicsVEnumOnly val is supported
    BasicsCEnumOnly code is supported
    BasicsVDEnumSupport val and desc
    BasicsCDEnumSupport code and desc
    BasicsVCDEnumSupport val, code and desc
    • The enumeration field needs to be defined as public static final. Please refer to the above example;
    • To use the method provided by the enumeration base class, you need to pass the enumeration subclass into typeenum getByVal(TypeEnum.class, 4); To navigate to subclass objects.

2. Automatically reveal enumeration description

1. Turn on the function

This function is mainly used for weaving based on AOP, so the first step is to start our AOP. There are two ways to start it:

  1. Open it through Enable annotation, and directly add @ EnableEnumDesc to the startup class.
@SpringBootApplication
@EnableEnumDesc
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }
}

When starting, you can see the log of InjectEnumDescAop initialization completion

2021-05-07 09:27:33.831  INFO 6952 --- [           main] com.lsl.rdf.config.InjectEnumDescConfig  : InjectEnumDescAop initialization completed.
  1. Create AOP through the configuration file, add the configuration class ApplicationConfig, and add the Bean of our AOP class.
@Component
public class ApplicationConfig {
    @Bean
    public InjectEnumDescAop injectEnumDescAop() {
        return new InjectEnumDescAop();
    }
}

2. Specify fields that are similar to

@InjectEnumTarget
public class ResultVo {

    private Long id;
    
    private String name;
    
    private String password;
    
    @InjectDescByVal(enumClass = StatusEnum.class)
    private Integer status;
    
    private String statusDesc;
    
    @InjectDescByVal(enumClass = TypeEnum.class, targetFieldName = "typeMsg")
    private Integer type;
    
    private String typeMsg;
    
    @InjectDescByCode(enumClass = StateEnum.class)
    private String state;
    
    private String stateDesc;
}

As shown in the above example, @ InjectEnumTarget annotation is used to specify the class to be woven in, @ InjectDescByVal is used to specify the field corresponding to the enumeration value, and enumClass input parameter is used to specify the corresponding enumeration
Class. The targetFieldName input parameter is used to specify the field used to load the enumeration description. The default is the enumeration value field plus the Desc suffix. If it just matches the default value, there is no need to add targetFieldName
On the contrary, you need to specify the field name, @ InjectDescByCode. The difference is that the corresponding enumeration class is CD type.

3. Specify the interface

@Service
public class DemoServiceImpl implements DemoService {

    @Autowired
    private UserMapper userMapper;

    @Override
    @InjectEnumResult
    public ResultVo selectOne(@LockTarget("id") Long id) {
        return userMapper.queryOne(id);
    }
}

Finally, we also need to specify the woven interface. Just add @ InjectEnumResult annotation to the implementation method of the interface.

Look at the results:

{
    "id": 1,
    "name": "Sleepy insect",
    "password": null,
    "status": 1,
    "statusDesc": "give the thumbs-up",
    "type": 2,
    "typeMsg": "follow",
    "state": "OK",
    "stateDesc": "success"
}

4. Description

  • At present, our weaving only supports simple objects or list s. For nested objects that need to be exposed automatically, we need to customize AOP, such as:
@InjectEnumTarget
public class A {
   @InjectDescByVal(enumClass = StatusEnum.class)
   private Integer status;
   
   private String statusDesc;
}

@InjectEnumTarget
public class B {

    @InjectDescByCode(enumClass = StateEnum.class)
    private String state;
    
    private String stateDesc;

    private A a;
}

At this time, when B is revealed, the fields in object A cannot be revealed automatically.

3. Custom weaving AOP

You may encounter a problem when using our automatic disclosure enumeration to describe the function, that is, the return of the woven interface is not a simple object or list. At this time, our weaving fails. Take a look at our AOP implementation

@Aspect
public class InjectEnumDescAop {

    @Pointcut("@annotation(com.lsl.rdf.annotation.InjectEnumResult)")
    public void pointCut() {
    }

    @AfterReturning(returning = "result", pointcut = "pointCut()")
    public Object afterReturn(Object result) {
        // Custom code quick start
        if (result instanceof List) {
            EnumDescInjectUtil.invadeFieldsSetDesc((List<Object>) result);
        } else {
            EnumDescInjectUtil.invadeFieldSetDesc(result);
        }
        // Custom code fast end
        return result;
    }
}

You can see that we distinguish only list objects and simple objects. If your interface returns your package class, we can not handle nested objects, then we need to customize AOP, then call us.
The method provided by EnumDescInjectUtil can realize customized processing, and then initialize the Bean by configuring the class.

4, Contact author

If you find any bug s, suggestions or have any questions, please feel free to contact us at any time.

  • QQ group: 1083977145
  • Email: LSL yx@foxmail.com

5, More recommendations

The visual Mybatis decompile tool helps you generate entity, dao interface and Mapper mapping files according to the database table.

Distributed lock based on annotation. Distributed lock can be so simple!

Topics: Java IntelliJ IDEA Spring