voucher 과제를 진행하면서 배운 Java 지식들

어겐어갠·2022년 4월 7일
0

스프링 부트 애플리케이션 실행할 때 코드를 실행하는 방법

CommandLineRunner

	@Bean
	public CommandLineRunner run(VoucherEnrollSystem voucherEnrollSystem) {
		return args -> voucherEnrollSystem.run();
	}

이 외에도 ApplicationReadyEvent 도 있음.
https://madplay.github.io/post/run-code-on-application-startup-in-springboot

Enum에 int type향 첨가.

public enum VoucherType {
    FIXED(1), PERCENT(2);

    private final int number;

    VoucherType(int number) {
        this.number = number;
    }

    public int getNumber() {
        return number;
    }
}

getNumber메서드를 통해 number값도 얻을 수 있다.

Enum값을 stream돌려서 무언가 하고싶을때

boolean validateInputString = Arrays.stream(InputCommend.values()).map(String::valueOf).anyMatch(v -> v.equals(inputString.toUpperCase()));

stream으로 꺼낸 값들의 type은 InputCommend라서 형변환이 필요했다.
anyMatch로 판별할 수 있었다.

application.yml 설정으로 log를 file로 저장하는 법.

  file:
    name: logs
    max-history: 7
    max-size: 5KB
    total-size-cap: 1MB
  level:
    org.voucherProject.voucherProject: warn
profile
음그래

0개의 댓글