AOP

  • 관점 지향 프로그래밍
  • 어떤 로직을 기준으로 핵심적인 관점, 부가적인 관점으로 나누어서 보고 그 관점을 기준으로 각각 모듈화하겠다는 것
    • 모든 메소드의 호출 시간을 측정하려고 할 때
    • 공통 관심 사항(cross-cutting concern) vs 핵심 관심 사항(core concern)
  • AOP가 필요한 상황
long start = System.currentTimeMillis(); 
try{
	validateDuplicateMember(member); 
    memberRepository.save(member); 
    return member.getId(); 
}finally {
    	long finish = System.currentTimeMillis(); 
        long timeMs=finish-start; 
        System.out.println("join = " + timeMs + "ms"); 
}

AOP를 사용하지 않는다면 메소드 호출시간을 측정해주는 것을 포함시켜야한다.

 

시간측정 AOP 등록

@Aspect //이걸 적어야 aop사용가능 
@Component public class TimeTraceAop {

	@Around("execution(* hello.hellospring..*(..))") 
    public Object execute(ProceedingJoinPoint joinPoint) throws Throwable{
    	long start = System.currentTimeMillis(); 
        System.out.println("START: "+joinPoint.toString()); 
        try{ 
        	return joinPoint.proceed(); 
        }finally {
        	long finish = System.currentTimeMillis(); 
            long timeMs = finish-start; 
            System.out.println("END: "+joinPoint.toString()+" "+timeMs+"ms"); 
        } 
    } 
}

서비스 하위로 메소드 호출시간을 보고 싶을 때 : `@Around("execution( hello.hellospring.service.(..))")`

  • 회원가입, 회원 조회등 핵심 관심사항과 시간을 측정하는 공통 관심 사항을 분리.
  • 시간을 측정하는 로직을 별도의 공통 로직으로 생성.
  • 핵심 관심 사항 깔끔하게 유지가능.
  • 변경이 필요하면 이 로직만 변경하면 된다.
  • 원하는 적용 대상을 선택가능.

@Component가 아닌 직접 @Bean으로 참조 했을 때

@Configuration 
public class SpringConfig {
	@Bean 
    public TimeTraceAop timeTraceAop() {
    	return new TimeTraceAop(); 
    } 
}
  • 이 상태에서 @Around("execution(* hello.hellospring..*(..))")라고 한다면 자기 자신인 TimeTraceAop를 생성하는 코드인 SpringConfig의 timeTraceAop() 메서드도 AOP로 처리하게 된다. 따라서 순환참조 문제가 발생하게 된다.
  • 다음과 같이 AOP 대상에서 SpringConfig를 빼주면 된다.
  • @Around("execution(* hello.hellospring..*(..)) && !target(hello.hellospring.SpringConfig)")

 

Ctrl+Alt+L : 코드정렬

인프런 '스프링 입문 - 코드로 배우는 스프링 부트, 웹 MVC, DB 접근 기술' 통해 학습 중.

스프링 데이터 엑세스

 

1. 순수 JDBC
2. 스프링 JdbcTemplate
3. JPA

 

H2 데이터베이스 설치 방법

 

1) ./h2.bat (mac에서는 ./h2.sh)
2) jdbc:h2:~/test로 DB 생성
3) ~/test.mv.db 파일 생성 확인
4) 이후부터 jdbc:h2:tcp://localhost/~/test로 접속

The Web Console server could not be started. Possible cause: another server is already running at XXX:8082

8082 포트가 겹쳐서 발생한 오류. 포트 죽여줘야함.

 

순수 JDBC

  • build.gradle 파일에 jdbc, h2 데이터베이스 관련 라이브러리 추가
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
runtimeOnly 'com.h2database:h2'
  • 스프링 부트 데이터베이스 연결 설정 추가 (resources/application.properties )
spring.datasource.url=jdbc:h2:tcp://localhost/~/test
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
  • pstmt = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS); : sequence라고 생각하자
  • spring config 변경
@Bean public MemberRepository memberRepository() {
	// return new MemoryMemberRepository(); 
	return new JdbcMemberRepository(dataSource);
}

 

스프링 통합 테스트

  • @SpringBootTest : 스프링 컨테이너와 테스트 함께 실행.
  • @Transactional : 테스트 케이스에 이 애노테이션이 있으면, 테스트 시작 전에 트랜잭션을 시작, 테스트 완료 후에 항상 롤백. 다음 테스트에 영향 주지않기 위해 사용. (DB에 데이터가 남지 않음)

 

스프링 JdbcTemplate

  • 스프링 JdbcTemplate과 MyBatis 같은 라이브러리는 JDBC API에서 본 반복 코드를 제거해준다. 하지만 SQL은 직접 작성.
  • 순수 JDBC와 환경설정은 같다.
  • spring config 변경
@Bean
public MemberRepository memberRepository() {
	// return new MemoryMemberRepository(); 
    // return new JdbcMemberRepository(dataSource); 
    return new JdbcTemplateMemberRepository(dataSource); 
}

 

JPA

  • 기존의 반복 코드는 물론이고, 기본적인 SQL도 JPA가 직접 만들어서 실행.
  • SQL과 데이터 중심의 설계에서 객체 중심의 설계로 패러다임을 전환 가능하게 해준다.
  • build.gradle 파일에 JPA, h2 데이터베이스 관련 라이브러리 추가
dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' 
    implementation 'org.springframework.boot:spring-boot-starter-web' 
    //implementation 'org.springframework.boot:spring-boot-starter-jdbc' 
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa' 
    runtimeOnly 'com.h2database:h2' 
    testImplementation('org.springframework.boot:spring-boot-starter-test') { 
    	exclude group: 'org.junit.vintage', module: 'junit-vintage-engine' 
    } 
}

spring-boot-starter-data-jpa 는 내부에 jdbc 관련 라이브러리를 포함

 

  • 스프링 부트에 JPA 설정 추가(resources/application.properties)
spring.jpa.show-sql=true //JPA가 생성하는 SQL을 출력 
spring.jpa.hibernate.ddl-auto=none //자동으로 테이블 생성하는 것 해제
  • member 클래스 엔티티매핑
@Entity 
public class Member {
	@Id @GeneratedValue(strategy = GenerationType.IDENTITY) //DB가 알아서 생성되게 한 것을 identity전략이라고 함.
    private Long id; 
    private String name;
  • JpaMemberRepository 생성 (JPQL 사용)
  • 서비스 계층에 트랜잭션 추가
  • spring config 변경
@Configuration 
public class SpringConfig { 
	private final DataSource dataSource; 
    private final EntityManager em; 
    public SpringConfig(DataSource dataSource, EntityManager em) {
    	this.dataSource = dataSource; this.em = em; 
    } 

	@Bean 
	public MemberService memberService() {
		return new MemberService(memberRepository());
	} 

	@Bean 
	public MemberRepository memberRepository() {
		// return new MemoryMemberRepository(); 
    	// return new JdbcMemberRepository(dataSource); 
   	 	// return new JdbcTemplateMemberRepository(dataSource); 
   		return new JpaMemberRepository(em); 
 	}
}

 

스프링 데이터 JPA

  • 리포지토리에 구현 클래스 없이 인터페이스 만으로 개발을 완료할 수 있다.
  • 기본 CRUD 기능도 스프링 데이터 JPA가 모두 제공
  • 앞의 JPA 설정을 그대로 사용
  • 스프링 데이터 JPA 회원 리포지토리(인터페이스) 생성
  • spring config 변경
@Configuration
public class SpringConfig {
	private final MemberRepository memberRepository;
    
    @Autowired
    public SpringConfig(MemberRepository memberRepository) {
    	this.memberRepository = memberRepository;
    } 
    
    @Bean 
    public MemberService memberService(){
    	return new MemberService(memberRepository);
    } 
}
  • 스프링 데이터 JPA가 SpringDatJpaMemberRepository 를 스프링 빈으로 자동 등록

인프런 '스프링 입문 - 코드로 배우는 스프링 부트, 웹 MVC, DB 접근 기술' 통해 학습 중.

'JAVA' 카테고리의 다른 글

Spring 핵심 01 : 객체 지향 설계와 스프링  (0) 2021.05.18
Spring 05 : AOP  (0) 2021.05.18
Spring 03 : 스프링 빈과 의존관계  (0) 2021.05.18
Spring02 : 예제(회원관리)  (0) 2021.05.18
Spring 01 : 프로젝트 환경설정  (0) 2021.05.18

스프링 빈 등록방법

  1. 컴포넌트 스캔과 자동 의존관계 설정
  2. 자바 코드로 직접 스프링 빈 등록하기

컴포넌트 스캔과 자동 의존관계 설정

  • 회원 컨트롤러가 회원서비스와 회원 리포지토리를 사용할 수 있게 의존관계 준비
@Controller
public class MemberController {
    private final MemberService memberService;    

    @Autowired
    public MemberController(MemberService memberService) {
        this.memberService = memberService;
    }
}
  • 생성자 주입을 쓰는 것이 제일 좋다.
  • @Autowired : 스프링이 연관된 객체를 스프링 컨테이너에서 찾아서 넣어준다.
  • @Component : 이 애노테이션이 있으면 스프링 빈으로 자동 등록된다.
  • @Component / @Controller, @Service, @Repository
@Autowired
private MemberService memberService;
  • 필드주입 : 스프링이 뜰 때만 넣어주고 바꿀 수 있는 방법이 없다. 잘 안쓴다.

 

자바 코드로 직접 스프링 빈 등록

  • @Service, @Repository, @Autowired 애노테이션 제거 후 진행

SpringConfig.java 파일 생성

@Configuration
public class SpringConfig {

    @Bean
    public MemberService memberService(){
        return new MemberService(memberRepository());
    }
    @Bean
    public MemberRepository memberRepository(){
        return new MemoryMemberRepository();
    }
  }
  • 장점 : 정형화 되지 않거나, 상황에 따라 구현 클래스를 변경해야 할 경우 직접 스프링 빈을 등록하는 것이 더 편하다.
  • 아직 데이터 저장소가 정해지지 않았다는 가상의 시나리오를 정했기 때문에 직접 스프링 빈을 등록하는 것으로 한다.

보충

  • thymeleaf : JSP와 달리 Servlet으로 변환X, 비즈니스 로직과 분리되어 View에 집중.
  • 스프링부트에서는 JSP 자동 설정 지원X. JSP는 jar패키징 불가하여 war패키징해야한다.
  • get : 주로 조회할 때 / post : 폼에 등록할 때

jar

  • 자바 프로젝트를 압축한 파일

war

  • 웹 관련 자원만 포함, 웹 어플리케이션 압축 파일 포맷
  • JAR 포맷과 달리 WEB-INF 및 META-INF 디렉토리로 사전 정의 된 구조를 사용
  • WAR파일을 실행하려면 Tomcat, Weblogic, Websphere 등의 웹 서버 (WEB)또는 웹 컨테이너(WAS)가 필요

인프런 '스프링 입문 - 코드로 배우는 스프링 부트, 웹 MVC, DB 접근 기술' 통해 학습 중.

'JAVA' 카테고리의 다른 글

Spring 핵심 01 : 객체 지향 설계와 스프링  (0) 2021.05.18
Spring 05 : AOP  (0) 2021.05.18
Spring 04 : 스프링 DB 접근  (0) 2021.05.18
Spring02 : 예제(회원관리)  (0) 2021.05.18
Spring 01 : 프로젝트 환경설정  (0) 2021.05.18

+ Recent posts