728x90
Java & Spring
40

SseEmitter를 사용하여 특정 대상에게 메시지(알람) 전송

Dependency 추가 Spring Boot 2 기준으로 다음과 같이 Dependency를 추가해준다. dependencies { implementation 'org.springframework.boot:spring-boot-starter-security' implementation 'org.springframework.boot:spring-boot-starter-web' ... testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.security:spring-security-test' } Security 설정 Spring Security 설정 부분은 이전에 ..

Java & Spring 2024.03.02

AOP를 적용하여 BindingResult 처리

Spring validation을 사용할 경우 다음과 같이 bindingResult를 받아 처리할 수 있다. @PostMapping public ResponseEntity testMethod( @Valid FormRequest formRequest, BindingResult bindingResult) { ... } 그리고 validation을 수행해야하는 api 요청이 하나 둘씩 추가되면 추가된만큼 bindingResult를 처리하는 코드 또한 추가로 작성될 것이다. 이처럼 boilerplate 코드가 발생하게되면서 비효율적인 코드가 되는 것이다. AOP annotation 정의 annotation을 추가한 method만 bindingResult를 처리할 수 있게 annotation을 정의한다. @Tar..

Java & Spring 2024.03.02

Spring RabbitMQ - publish/subscribe

Docker 먼저 Docker를 설정하여 컨테이너를 생성한다. Dockerfile FROM rabbitmq:latest docker-compose version: '1' services: rabbitmq: container_name: rabbitmq-test build: dockerfile: Dockerfile image: rabbitmq ports: - "15672:15672" - "5672:5672" Dependency spring boot 2 기준으로 dependency를 다음과 같이 추가해준다. implementation 'org.springframework.boot:spring-boot-starter-amqp' implementation 'org.springframework.boot:spring..

Java & Spring 2024.02.28

Spring WebSocket을 사용하여 특정 대상에게 메시지(알람) 전송

Dependency 추가 spring boot 2를 기준으로 다음과 같이 Dependency를 추가해준다. dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-websocket' implementation 'org.springframework.boot:spring-boot-starter-security' ... } Security 설정 특정 대상에게 메시지를 전송하려면 그 대상의 ID 또는 unique한 값을 알아야한다. 현재 예시는 authentication을 수행하여 유저의 ID를 사용한다. Princ..

Java & Spring 2024.02.26

1:N관계의 Join Fetch 결과에서 중복 데이터 제거

ERD 간단하게 위와 같은 구조의 DB를 가지고 있다고 했을 때 Article을 조회하면 UserAccount, ArticleComment, ArticleFile 정보가 필요하므로 같이 조회해야한다. 이때 UserAccount, ArticleFile은 1:1관계이기 때문에 조회한 Article 개수대로 결과가 반환되지만 ArticleComment는 1:N 관계로 ArticleComment 개수만큼 Article이 중복되어 결과가 반환된다. 예를 들어 ArticleComment가 100개가 있는 Article을 left join하여 조회한 결과를 보면 다음처럼 100개만큼 Article 데이터가 중복되어 나타난다. select a.id, a.title, ac.id from article a left out..

Java & Spring 2024.02.19

[Mac] infer를 사용한 static code analyze

infer는 Meta에서 개발한 정적 분석 tool이다. https://fbinfer.com/ Install https://github.com/facebook/infer/blob/main/INSTALL.md # install dependencies brew install autoconf automake cmake opam pkg-config sqlite gmp mpfr java # Checkout Infer git clone https://github.com/facebook/infer.git cd infer # Compile Infer ./build-infer.sh java # install Infer system-wide... sudo make install Analyze 다음과 같이 command l..

Java & Spring 2024.02.16

프로메테우스와 그라파나를 활용한 Response Time 모니터링

프로메테우스 설치 및 실행 brew install prometheus -> 설치 brew services start prometheus -> 실행 ps -ax | grep prometheus -> prometheus.yml 위치 확인 prometheus.yml에 설정된 호스트 주소로 접속 Spring Dependency 의존성 추가 implementation 'org.springframework.boot:spring-boot-starter-actuator' implementation 'io.micrometer:micrometer-core' runtimeOnly 'io.micrometer:micrometer-registry-prometheus' properties 설정 management: server: ..

Java & Spring 2024.02.14

[Spring] Equals와 HashCode를 재정의(Override)해야하는 이유

Background Java의 Class는 Equals 또는 HashCode로 서로 동일한지 판단할 수 있다. 만약 단순히 new 생성자로 Class를 생성 후 ==비교하게되면 서로 다른 주소값을 가지기때문에 두 Class는 다르다고 판단한다. 또한 Class 객체는Equals로 비교할 경우에도 주소값을 사용하여 비교한다. 그리고 hashcode는 객체의 주소값을 해싱한 값(해시 코드)이므로 아래처럼 새로운 Class를 생성하게되면 서로 다른 hashcode를 가진다. public class Dummy { private final int a; private final int b; public Dummy(int a, int b) { this.a = a; this.b = b; } } public class ..

Java & Spring 2024.01.22

[Java] static과 final은 언제 사용해야할까

먼저 JVM 메모리는 다음과 같이 이루어져있다. 여기서 Method Area에 Static 변수가 저장되며 JVM 시작시 생성된다. 그리고 GC(Garbage Collection) 대상이 아니지만 명시적으로 null 선언시 GC 대상이 된다. static은 언제 사용할까 보통 여러 인스턴스 간에 데이터를 공유해야할 때 다음과 같이 클래스 변수를 사용하거나 인스턴스에 종속되지 않는 메소드를 정의할 때 사용한다. public class Example { public static int count; public static void incrementCount() { count++; } } 그리고 내부 클래스를 정의할 때에도 static을 선언해주어야한다. 만약 아래처럼 static을 선언하지 않으면 내부 클래..

Java & Spring 2024.01.20

[JPA] 필요한 특정 필드만 조회하여 쿼리 최적화 수행

먼저 다음과 같이 ArticleFile이라는 엔티티가 있다고 해보자. @Entity public class ArticleFile { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private Long byteSize; private String fileName; private String fileExtension; @OneToOne(fetch = LAZY, cascade = CascadeType.ALL) @JoinColumn(name = "article_id") private Article article; ... } ArticleFile에는 id를 제외하고 byteSize, fileName, fileExtension이 ..

Java & Spring 2024.01.16
728x90