Skip to content

Blog

Java Stream Guide

Assume we have the following classes,

Java
@Data
public class User {
  private long id;
  private String name;
  private int age;

  private List<Address> address;
}


@Data
public class Address {
  private String postCode;
  private String street;
  private String building;
}

Inject Properties From YAML in Spring

In Spring, we can easily define configuration value in a YAML file, and inject those configuration into a Java class.

Take the following the YAML configuration file as an example.

YAML
server: localhost

# by default, spring split the string with comma
listValues: a,b,c,d
# if we want to split the string with customize character, we can use Spring EL
listValuesWithSpEL: a,b;c,d;e,f

# we can inject the whole complexObject into another class, with annotation ConfigurationProperties
complexObject:
    port: 90
    ssl: false
    listValues: a,b,c,d
    listValuesWithSpEL: a,b;c,d;e,f

Mock Class Property in PyTest

It's easy to use Mock for a python method, staticmethod, classmethod, but for propery, it's not just to use Mock. Here are the example to mock a property of a class in python UT.

  • A simple class

    Python
    class Dog:
        @property
        def name(self):
            return "dog"
    

Spring Security Authorization Filter

Spring Security supports to easily configure role or authority for a specified resource by using Spring security expression.

For example,

Java
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    ...
    .antMatchers("/auth/admin/*").hasRole("ADMIN")
    .antMatchers("/auth/*").hasAnyRole("ADMIN","USER")
    ...
}

What are PO, VO, DAO, BO, POJO, DTO in Java

Definition

Name Definition Scope
DAO Database access object. A standard of J2EE pattern to access the database. It leverages PO to do database operations. DAO
DTO Data transfer object, used to transfer data between controll and client through HTTP or other ptotocol. PO may be not suitable to transfer directly to client, it may contains sensitive information. So, we need DTO to only transfer necessary data to client. Controller, Service
BO Business object, it leverages DAO, PO, VO(value object) to do business operations. Service
View Object View object are used to present a view to end users, it can be a DTO. Controller, Client
Value Object It's an object that may contains all or subset of a PO fields, and also can contain other fields. It's created by new operator, and it's not visible to Dao or database. Service
PO Persistent object, it maps to database object. Usually, entity objects are PO. Created and managed with database opereations. DAO, Service
POJO Plain Java object. All objects above are POJOs

Switch Fallthrough in Go

If you are familar with C or Java, you must know that break is requird for a switch's case branch, if break is missed, then it will fall through to next case.

For example, consider the following code

Java
int a = 1;

switch (a) {
    case 0: System.out.println("0");
    case 1: System.out.println("1");
    case 2: System.out.println("2"); break;
    default: System.out.println("unknown"); break;
}

The output is:

Bash
1
2

Java Virtual Thread Usage

What is Virtual thread?

A Java platform thread is mapped to OS Kernel thread, as the resource is limited, we can't create as many as platform threads as we want.

Even Java provides executor pool, it also can't handle some cases. For example, if we want to create thoudsands of IO tasks, virtual threads is more suitable than platform threads.

Enable Preview Feature in Idea With Gradle 7.6

When I am trying to use the Java virtual thread in Idea with Gradle 7.6, I got error,

Bash
/java-examples/src/main/java/com.veda/app/virtualthread/VirtualTheadExample.java:5: error: ofVirtual() is a preview API and is disabled by default.
        System.out.println(Thread.ofVirtual().factory());
                                 ^
  (use --enable-preview to enable preview APIs)