728x90

Spring 34

SpringBoot + JPA 멀티 연동하기

SpringBoot + JPA 다중 연동 1. 다중 DB 설정 시, 알아야 할 것 다중 DB설정은 Auto Configuration 되지 않기 때문에 설정파일(application.yml or application.properties) 값을 읽어와서 연동 할 DB 수 만큼 Datasource를 수동 설정해야함 주요 설정 내용 리소스 경로 설정 Repository basePackages 경로 설정 Entity 경로 설정 DB 정보 설정(Datasource) driver 이름 URL Id/Password Hibernate 설정 ddl-auto dialect 설정된 다중 DB는 Repository package 명으로 구분 초기 설정이 복잡한 편이나, 천천히 살펴보면 크게 어렵지 않음 2. 소스코드 이 글에서..

Spring 2023.01.12

RestTemplate과 WebClient(feat. 서버 간 데이터 주고 받기)

RestTemplate과 WebClient 스프링 어플리케이션에서 HTTP 요청할 때 사용하는 방법으로 RestTemplate과 WebClient가 있다. 스프링 5.0 이전까지는 클라이언트에서 HTTP 접근을 위해 사용한 것은 RestTemplate 이었다. 스프링 5.0 에서 WebClient가 나왔고 현재는 WebClient를 사용하기를 권고하고 있다. 이번 팀 프로젝트를 진행하면서 RestTemplate을 도입하였었다. 하지만 RestTemplate이 deprecated 될 가능성이 있다는 얘기를 들었고 새로 시작하는 프로젝트에서는 WebClient를 쓰는 것이 좋겠다는 의견이 있어 WebClient를 적용해보게 되었다. 그럼 RestTemplate과 WebClient는 어떤 특징이 있으며 왜 W..

Spring 2022.12.02

(Springboot 관련) mongoDB 초기 설정

몽고 DB 초기 설정 몽고 DB가 설치되면 https://start.spring.io/ 로 이동하여 다음 세 가지를 dependencies를 모두 추가하자. Spring Data JPA도 빼먹지 말고 추가하자. 이제 project를 open 하여 다음 코드들을 추가해보자. (공식 문서 참조했습니다.) public class Recipe { @Id public String id; public String name; public String type; public Recipe() {} public Recipe(String name, String type) { this.name = name; this.type = type; } } public interface RecipeRepository extends Mo..

Spring 2022.10.01

AOP - 관점 지향 프로그래밍

스프링 AOP ( Aspect Oriented Programming ) AOP는 Aspect Oriented Programming의 약자로 관점 지향 프로그래밍이라고 불린다. 관점 지향은 쉽게 말해 어떤 로직을 기준으로 핵심적인 관점, 부가적인 관점으로 나누어서 보고 그 관점을 기준으로 각각 모듈화하겠다는 것이다. 여기서 모듈화란 어떤 공통된 로직이나 기능을 하나의 단위로 묶는 것을 말한다. 예로들어 핵심적인 관점은 결국 우리가 적용하고자 하는 핵심 비즈니스 로직이 된다. 또한 부가적인 관점은 핵심 로직을 실행하기 위해서 행해지는 데이터베이스 연결, 로깅, 파일 입출력 등을 예로 들 수 있다. AOP에서 각 관점을 기준으로 로직을 모듈화한다는 것은 코드들을 부분적으로 나누어서 모듈화하겠다는 의미다. 이때..

Spring 2022.09.13

[스프링 부트와 AWS로 혼자 구현하는 웹 서비스] p.62 실행 오류 해결법

오류 로그 package com.jojoldu.book.awsspring.springboot.web; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.web.servlet.Mo..

Spring 2022.05.16

[마이바티스] @Alias 어노테이션

@Alias는 mybatis에서 지원하는 어노테이션으로 TypeAlias, 즉 별칭을 지정할 때 사용합니다. 이 어노테이션을 사용하기 위해서는 sessionFactory 설정 부분에 다음 코드를 추가해야합니다. sessionFactory.setTypeAliasesPackage("@Alias를 적용할 경로") setTypeAliasesPackage() 메서드를 통해 패키지 경로를 정해두면 패키지 내에 @Alias("별칭") 어노테이션이 지정된 클래스는 매퍼파일에서 별칭으로 해당 클래스를 매핑해줍니다. @Alias("user") public class UserDTO { private Long id; private String name; } 매퍼 파일에서 TypeAlias를 지정하지 않으면 com.user.d..

Spring 2022.03.22

JUnit 4 Test 에서 get 메소드 에러 관련

스프링 부트와 AWS로 혼자 구현하는 웹서비스(p. 62, 이동욱님 저)를 따라 소스를 구현하다보니, 아래 JUnit 4 테스트의 get 부분에서 에러가 났다. package com.jojoldu.book.awsspring.springboot.web; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.test.c..

Spring 2022.03.01

Gradle sync failed: Could not find org.springframework.boot:spring-boot:spring-boot-gradle-plugin 오류 해결

(build.gradle 파일) Gradle 4 버전에서 정상동작을 하였던 아래 코드가 오류가 났을 때 dependencies { compile('org.springframework.boot:spring-boot-starter-web') testCompile('org.springframework.boot:spring-boot-starter-test') } 아래와 같이 compile -> implementation, testCompile -> testImplementation 으로 바꾸면 오류가 해소된다. dependencies { implementation('org.springframework.boot:spring-boot-starter-web') testImplementation('org.springf..

Spring 2022.02.28

스프링 시큐리티(Spring Security) 설정

스프링 시큐리티(Spring Security) 설정 추가1 security dependency 추가하기 pom.xml에 security와 관련된 의존성 추가 후 “Reload All Maven Projects”을 클릭하여 의존성을 받아온다. org.springframework.boot spring-boot-starter-security 스프링 시큐리티 설정 추가2 SecurityConfig 소스 작성 유저에게 선택적으로 권한요청이 가능 @Configuration @EnableWebSecurity // (1) public class SecurityConfig extends WebSecurityConfigurerAdapter { // (2) @Autowired MemberService memberServic..

Spring 2022.02.17

Spring 필드 주입 vs 생성자 주입(win)

스프링 필드 주입 대신에 생성자 주입을 권장 변경 불가능한 안전한 객체 생성 가능 final 키워드를 추가하면 컴파일 시점에 좀더 일찍 오류를 체크할 수 있음(보통 기본 생성자를 추가할 때 발견할 수 있는데 미리 발견) public class MemberService{ private final MemberRepository memberRepository; public MemberService(MemberRepository memberRepository) { this.memberRepository = memberRepository; } } (참고) 롬복일때는 아래와 같은 형식(위와 내용은 같음) @RequiredArgsConstructor public class MemberService { private ..

Spring 2022.02.15
728x90