Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.javabom.toby.chapter6.term.aop용어;

import com.javabom.toby.chapter6.term.aop용어.어드바이스.MessageAdvice;
import com.javabom.toby.chapter6.term.aop용어.팩토리빈.MessageFactoryBean;
import com.javabom.toby.chapter6.term.aop용어.포인트컷.MessagePointcut;
import com.javabom.toby.chapter6.term.aop용어.프록시.DetailMessage;
import com.javabom.toby.chapter6.term.aop용어.프록시.Message;
import com.javabom.toby.chapter6.term.aop용어.프록시팩토리빈.MessageProxyFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
Expand All @@ -15,4 +18,21 @@ public class AopConfiguration {
public MessageFactoryBean messageFactoryBean() {
return new MessageFactoryBean(new DetailMessage("test"), Message.class);
}

@Bean
public MessageAdvice messageAdvice() {
return new MessageAdvice();
}

@Bean
public MessagePointcut messagePointcut() {
return new MessagePointcut("getT*");
}

@Bean
public MessageProxyFactoryBean messageProxyFactoryBean(MessagePointcut messagePointcut, MessageAdvice messageAdvice) {
MessageProxyFactoryBean messageProxyFactoryBean = new MessageProxyFactoryBean(new DetailMessage("hello"));
messageProxyFactoryBean.addAdvisor(messagePointcut, messageAdvice);
return messageProxyFactoryBean;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.javabom.toby.chapter6.term.aop용어.어드바이스;

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

public class MessageAdvice implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
String ret = (String) invocation.proceed();
return ret.toUpperCase();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.javabom.toby.chapter6.term.aop용어.포인트컷;

import org.springframework.aop.support.NameMatchMethodPointcut;

public class MessagePointcut extends NameMatchMethodPointcut {
public MessagePointcut(String methodNamePattern) {
super();
super.addMethodName(methodNamePattern);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@
@RequiredArgsConstructor
public class DetailMessage implements Message{
private final String text;

@Override
public String getPlainText() {
return text;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

public interface Message {
String getText();
String getPlainText();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.javabom.toby.chapter6.term.aop용어.프록시팩토리빈;

import com.javabom.toby.chapter6.term.aop용어.프록시.Message;
import org.aopalliance.aop.Advice;
import org.springframework.aop.Pointcut;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.aop.support.DefaultPointcutAdvisor;

public class MessageProxyFactoryBean {
private final ProxyFactoryBean proxyFactoryBean;

public MessageProxyFactoryBean(Object target) {
this.proxyFactoryBean = new ProxyFactoryBean();
this.proxyFactoryBean.setTarget(target);
}

public void addAdvice(Advice advice) {
this.proxyFactoryBean.addAdvice(advice);
}

public void addAdvisor(Pointcut pointcut, Advice advice) {
this.proxyFactoryBean.addAdvisor(new DefaultPointcutAdvisor(pointcut, advice));
}

public Message getMessage() {
return (Message) proxyFactoryBean.getObject();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.javabom.toby.chapter6.term.확장;

import com.javabom.toby.chapter6.term.확장.model.Message;
import com.javabom.toby.chapter6.term.확장.model.NonTargetMessage;
import com.javabom.toby.chapter6.term.확장.model.TargetMessage;
import com.javabom.toby.chapter6.term.확장.어드바이저.UpperMessageAdvice;
import com.javabom.toby.chapter6.term.확장.어드바이저.MessageAdvisor;
import com.javabom.toby.chapter6.term.확장.확장포인트컷.NameMatchMessagePointcut;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan
public class ExtendedAopConfiguration {

@Bean
public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
return new DefaultAdvisorAutoProxyCreator();
}

@Bean
public NameMatchMessagePointcut nameMatchMessagePointcut() {
NameMatchMessagePointcut nameMatchMessagePointcut = new NameMatchMessagePointcut();
nameMatchMessagePointcut.setMappedClassName("TargetMessage");
nameMatchMessagePointcut.addMethodName("getM*");
return nameMatchMessagePointcut;
}

@Bean
public UpperMessageAdvice upperMessageAdvice() {
return new UpperMessageAdvice();
}

@Bean
public MessageAdvisor messageAdvisor(NameMatchMessagePointcut nameMatchMessagePointcut, UpperMessageAdvice upperMessageAdvice) {
return new MessageAdvisor(nameMatchMessagePointcut, upperMessageAdvice);
}

@Bean
public Message targetMessage() {
return new TargetMessage("hello");
}

@Bean
public Message nonTargetMessage() {
return new NonTargetMessage("hello");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.javabom.toby.chapter6.term.확장.model;

public interface Message {
String getMessage();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.javabom.toby.chapter6.term.확장.model;

public class NonTargetMessage implements Message {
private final String message;

public NonTargetMessage(String message) {
this.message = message;
}

@Override
public String getMessage() {
return message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.javabom.toby.chapter6.term.확장.model;

public class TargetMessage implements Message {
private final String message;

public TargetMessage(String message) {
this.message = message;
}

@Override
public String getMessage() {
return message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.javabom.toby.chapter6.term.확장.어드바이저;

import org.aopalliance.aop.Advice;
import org.springframework.aop.Pointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;

public class MessageAdvisor extends DefaultPointcutAdvisor {
public MessageAdvisor(Pointcut pointcut, Advice advice) {
super(pointcut, advice);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.javabom.toby.chapter6.term.확장.어드바이저;

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

public class UpperMessageAdvice implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
String ret = (String) invocation.proceed();
return ret.toUpperCase();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.javabom.toby.chapter6.term.확장.확장포인트컷;

import org.springframework.aop.ClassFilter;
import org.springframework.aop.support.NameMatchMethodPointcut;
import org.springframework.util.PatternMatchUtils;

public class NameMatchMessagePointcut extends NameMatchMethodPointcut {
public void setMappedClassName(String mappedClassName) {
this.setClassFilter(new NameMatchClassFilter(mappedClassName));
}

private static class NameMatchClassFilter implements ClassFilter {
private final String mappedName;

public NameMatchClassFilter(String mappedName) {
this.mappedName = mappedName;
}

@Override
public boolean matches(Class<?> clazz) {
return PatternMatchUtils.simpleMatch(mappedName, clazz.getSimpleName());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.javabom.toby.chapter6.term.aop용어.어드바이스;

import com.javabom.toby.chapter6.term.aop용어.프록시.DetailMessage;
import com.javabom.toby.chapter6.term.aop용어.프록시.Message;
import com.javabom.toby.chapter6.term.aop용어.프록시팩토리빈.MessageProxyFactoryBean;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class MessageAdviceTest {
@DisplayName("어드바이스는 타깃을 직접 알고 있을 필요가 없다.")
@Test
void adviceTest() {
//given
MessageProxyFactoryBean messageProxyFactoryBean = new MessageProxyFactoryBean(new DetailMessage("hello"));
messageProxyFactoryBean.addAdvice(new MessageAdvice());

//when
Message message = messageProxyFactoryBean.getMessage();

//then
assertThat(message.getText()).isEqualTo("HELLO");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.javabom.toby.chapter6.term.aop용어.포인트컷;

import com.javabom.toby.chapter6.term.aop용어.어드바이스.MessageAdvice;
import com.javabom.toby.chapter6.term.aop용어.프록시.DetailMessage;
import com.javabom.toby.chapter6.term.aop용어.프록시.Message;
import com.javabom.toby.chapter6.term.aop용어.프록시팩토리빈.MessageProxyFactoryBean;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class MessagePointCutTest {
@DisplayName("포인트컷은 어드바이스와 함께 묶어서 어드바이저로 적용한다.")
@Test
void pointcutTest() {
//given
MessageProxyFactoryBean messageProxyFactoryBean = new MessageProxyFactoryBean(new DetailMessage("hello"));
messageProxyFactoryBean.addAdvisor(new MessagePointcut("getT*"), new MessageAdvice());

//when
Message message = messageProxyFactoryBean.getMessage();

//then
assertThat(message.getText()).isEqualTo("HELLO");
assertThat(message.getPlainText()).isEqualTo("hello");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.javabom.toby.chapter6.term.aop용어.프록시팩토리빈;

import com.javabom.toby.chapter6.term.aop용어.AopConfiguration;
import com.javabom.toby.chapter6.term.aop용어.포인트컷.MessagePointcut;
import com.javabom.toby.chapter6.term.aop용어.프록시.DetailMessage;
import com.javabom.toby.chapter6.term.aop용어.프록시.Message;
import com.javabom.toby.chapter6.term.aop용어.어드바이스.MessageAdvice;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import static org.assertj.core.api.Assertions.*;

@SpringBootTest(classes = AopConfiguration.class)
class MessageProxyFactoryBeanTest {
@Autowired
private MessageProxyFactoryBean messageProxyFactoryBean;

@DisplayName("프록시 팩토리 빈 테스트")
@Test
void messageFactoryBeanTest() {
//given

//when
Message message = messageProxyFactoryBean.getMessage();

//then
assertThat(message.getText()).isEqualTo("HELLO");
assertThat(message.getPlainText()).isEqualTo("hello");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.javabom.toby.chapter6.term.확장.확장포인트컷;

import com.javabom.toby.chapter6.term.확장.ExtendedAopConfiguration;
import com.javabom.toby.chapter6.term.확장.model.Message;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.lang.reflect.Proxy;

import static org.assertj.core.api.Assertions.*;

@SpringBootTest(classes = ExtendedAopConfiguration.class)
class NameMatchMessagePointcutTest {
@Autowired
private Message targetMessage;

@Autowired
private Message nonTargetMessage;

@DisplayName("다이나믹 프록시 테스트")
@Test
void extendedPointcutTest() {
assertThat(targetMessage).isInstanceOf(Proxy.class);
assertThat(nonTargetMessage).isNotInstanceOf(Proxy.class);
assertThat(targetMessage.getMessage()).isEqualTo("HELLO");
assertThat(nonTargetMessage.getMessage()).isEqualTo("hello");
}
}