BackEnd/Spring & Springboot Study

예제로 배우는 스프링 프레임워크 입문 - 프록시 패턴

프록시 패턴

기존 코드 건드리지 않고 새 기능 추가하기

프록시 패턴

 

package org.springframework.samples.petclinic.proxy;

public class Cash implements Payment{

	@Override
	public void pay(int amount) {
		System.out.println(amount + " 현금 결제");
	}
}
------------------------------------------------------------
package org.springframework.samples.petclinic.proxy;

import org.springframework.util.StopWatch;

public class Cashperf implements Payment{

	Payment cash = new Cash();

	@Override
	public void pay(int amount) {
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();

		cash.pay(amount);

		stopWatch.stop();
		System.out.println(stopWatch.prettyPrint());
	}
}
------------------------------------------------------------
package org.springframework.samples.petclinic.proxy;

public interface Payment {

	void pay(int amount);
}
------------------------------------------------------------
package org.springframework.samples.petclinic.proxy;

public class Store {

	Payment payment;

	public Store(Payment payment){
		this.payment = payment;
	}

	public void buySomething(int amount) {
		payment.pay(amount);
	}
}
------------------------------------------------------------
package org.springframework.samples.petclinic.proxy;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class StoreTest {

	@Test
	public void testPay() {
    //  Payment cashperf = new Cash(); Cashperf가 없는 코드라면 단순 출력만 될 것
    //  Cashperf를 변수로 넣어서 cash 기능 + stopwatch기능 추가
		Payment cashperf = new Cashperf();
		Store store = new Store(cashperf);
		store.buySomething(100);
	}
}

 

jdbc transaction

setautocommit을 first sql

commit rollback 생략

 

https://github.com/devjun63/spring-petclinic/commit/3ebae4532a46319568ce9c99272dd39be2cedde5

 

AOP with Proxy · devjun63/spring-petclinic@3ebae45

Permalink This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository. Browse files AOP with Proxy Loading branch information Showing 5 changed files with 63 additions and 0 deletions. +9 −0 src/main/j

github.com