내가 간략하게 짠 코드는
package com.example.Proxy;
public interface Service {
public String printSomething();
}
public class IService implements Service{
@Override
public String printSomething() {
return "Hello Java";
}
}
Service 를 실행중입니다..
라는 걸 넣어주고 싶다.public class IService implements Service{
@Override
public String printSomething() {
System.out.println("Service 를 실행중입니다");
return "Hello Java";
}
}
public class Proxy implements Service {
Service service;
public Proxy(Service service) {
this.service = service;
}
@Override
public String printSomething() {
System.out.println("Service Loading...");
return service.printSomething();
}
}
이제 Service 가 실행되기전 Proxy 를 거쳐 작업한다면 Service 전 후에서 무언가 처리하는 나만의 로직도 만들수 있다.
public class Main {
public static void main(String[] args) {
Service proxy = new Proxy(new IService());
System.out.println(proxy.printSomething());
}
}
//Method 저거는 없는것인데 그냥 그 설명을 위해 적은것이다.
//뭐 아래처럼 되지 않을까 싶다..?
public class TransactionHandler {
Class transactionTargetClass;
TransactionManager transactionManager;
public TransactionHandler(Class transactionTargetClass) {
this.transactionTargetClass = transactionTargetClass;
}
Method[] methods = transactionTargetClass.getMethods();
// Transaction 이 선언된 타겟 메소드들의 배열
Method[] transactionMethods = methods.filter(transaction);
// transactionMethods 안에 있는 메소드들을 아래 excute 로 실행시킴
public void excute(Method method) {
transactionManager.begin();
try{
method.invoke();
}catch(Exception e) {
transactionManager.rollback();
}
transactionManager.commit();
}
}