파게로그

여러 Advice 본문

콤퓨타 왕기초/Spring

여러 Advice

파게 2021. 4. 25. 19:16

Program.java

package admin.school.ui;

import admin.school.entity.Exam;

public class InlineExamConsole implements ExamConsole {
    private Exam exam;

    @Override
    public void print() {
        System.out.printf("SUM: %d, AVG: %.2f\n", exam.sum(), exam.avg());
        throw new IllegalStateException("oops...");
    }

    public void setExam(Exam exam) {
        this.exam = exam;
    }
}

 

JavaConfig.java

package admin.school;

import admin.school.entity.Exam;
import admin.school.entity.MidTermExam;
import admin.school.ui.ExamConsole;
import admin.school.ui.InlineExamConsole;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class JavaConfig {
    @Bean
    public Exam exam() {
        Exam exam = new MidTermExam();
        exam.setKor(30);
        exam.setEng(50);
        exam.setMath(40);
        exam.setCom(70);
        return exam;
    }

    @Bean
    public ExamConsole examConsole() {
        return new InlineExamConsole();
    }
}

 

InlineExamConsole.java

package admin.school.ui;

import admin.school.entity.Exam;

public class InlineExamConsole implements ExamConsole {
    private Exam exam;

    @Override
    public void print() {
        System.out.printf("SUM: %d, AVG: %.2f\n", exam.sum(), exam.avg());
        // throw new IllegalStateException("oops...");
    }

    public void setExam(Exam exam) {
        this.exam = exam;
    }
}

 

MidTermExam.java

package admin.school.entity;

public class MidTermExam implements Exam {
    private int kor;
    private int eng;
    private int math;
    private int com;

    @Override
    public int sum() {
        return kor + eng + math + com;
    }

    @Override
    public double avg() {
        return sum() / 4.0;
    }

    public void setKor(int kor) {
        this.kor = kor;
    }

    public void setEng(int eng) {
        this.eng = eng;
    }

    public void setMath(int math) {
        this.math = math;
    }

    public void setCom(int com) {
        this.com = com;
    }
}

 

LogBeforeAdvice.java

package admin.school.advice;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class LogBeforeAdvice implements MethodBeforeAdvice {
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.print("We gonna call this method: ");
        System.out.println(method.getName());
        System.out.println("There are " + args.length + " arguments");
    }
}

 

LogAroundAdvice.java

package admin.school.advice;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class LogAroundAdvice implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        long start = System.currentTimeMillis();

        Object result = invocation.proceed();

        long end = System.currentTimeMillis();
        long duration = end - start;
        String message = duration + "ms elapsed.";
        System.out.println(message);

        return result;
    }
}

 

LogAfterReturningAdvice.java

package admin.school.advice;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class LogAfterReturningAdvice implements AfterReturningAdvice {
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("return value: " + returnValue);
        System.out.println("method: " + method.getName());
    }
}

 

LogAfterThrowing.java

package admin.school.advice;

import org.springframework.aop.ThrowsAdvice;

public class LogAfterThrowingAdvice implements ThrowsAdvice {
    public void afterThrowing(Exception e) throws Throwable {
        System.out.print("An error occured: ");
        System.out.println(e.getMessage());
    }
}

 

setting.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="midTermExam" class="admin.school.entity.MidTermExam"
          p:kor="30" p:eng="50" p:math="40" p:com="50" />

    <bean id="target" class="admin.school.ui.InlineExamConsole">
        <property name="exam" ref="midTermExam" />
    </bean>

    <bean id="logAroundAdvice" class="admin.school.advice.LogAroundAdvice" />

    <bean id="logBeforeAdvice" class="admin.school.advice.LogBeforeAdvice" />

    <bean id="logAfterReturningAdvice" class="admin.school.advice.LogAfterReturningAdvice" />

    <bean id="logAfterThrowingAdvice" class="admin.school.advice.LogAfterThrowingAdvice" />

    <bean id="examConsole" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="target" />
        <property name="interceptorNames">
            <list>
                <value>logBeforeAdvice</value>
                <value>logAroundAdvice</value>
                <value>logAfterReturningAdvice</value>
                <value>logAfterThrowingAdvice</value>
            </list>
        </property>
    </bean>

</beans>
Comments