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
3 changes: 1 addition & 2 deletions spring-toby/spring-project/build.gradle
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework:spring-jdbc'

implementation 'com.h2database:h2:1.4.197'

compile 'org.aspectj:aspectjweaver:1.8.9'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public UserRepository sessionUserRepository() {

#173 IoC/DI) userService빈은 UserRepository 빈을 DI 받는다.(IoC 컨테이너에 의해)
*/
@Bean
// @Bean
public UserService userService() {
/*
의존관계 주입 특징 3) 사용할 오브젝트에 대한 레퍼런스 -> defaultUserRepository 는 외부에서 주입(생성자, Setter,,)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.javabom.toby.chapter6.term;

import org.springframework.stereotype.Service;

@Service
public class AnotherAopUserService implements AopTestInterface {
@Override
public void test() {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.javabom.toby.chapter6.term;

interface AopTestInterface {
void test();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.javabom.toby.chapter6.term;

import org.springframework.stereotype.Service;

@Service(value = "aopUserService")
public class AopUserService implements AopTestInterface {

public void hello() {
System.out.println("Hello!");
}

public void bye() {
System.out.println("bye~!");
}

@Override
public void test() {
System.out.println("Test");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.javabom.toby.chapter6;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

@SpringBootTest
class AopUserServiceTest {

@DisplayName("표현식으로 포인트컷을 지정할 수 있다")
@Test
void methodSignature() throws NoSuchMethodException {
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
pointcut.setExpression("execution(* hello(..))");

/**
* 포인트컷은 클래스필터와 매서드매처를 둘다 지정할 수 있다.
*/
assertThat(pointcut.getClassFilter().matches(AopUserService.class)).isTrue();
assertThat(pointcut.getMethodMatcher().matches(AopUserService.class.getMethod("bye"), AopUserService.class)).isFalse();
}

@DisplayName("포인트컷으로 인터페이스를 지정하면 해당 인터페이스를 구현한 메서드만 적용된다")
@Test
void interfacePointcut() throws NoSuchMethodException {
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
pointcut.setExpression("execution(* *..AopTestInterface.*(..))");

assertThat(pointcut.getClassFilter().matches(AopUserService.class)).isTrue();
assertThat(pointcut.getMethodMatcher().matches(AopUserService.class.getMethod("test"), AopUserService.class)).isTrue();
assertThat(pointcut.getMethodMatcher().matches(AopUserService.class.getMethod("hello"), AopUserService.class)).isFalse();
}

@DisplayName("포인트컷 표현식의 클래스 이름에 적용되는 패턴은 타입패턴이다")
@Test
void typePattern() {
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
pointcut.setExpression("execution(* *..AnotherAopUserService.*(..))");

assertThat(pointcut.getClassFilter().matches(AopUserService.class)).isTrue();
}

@Test
void advice() {
AbstractApplicationContext ctx =
new ClassPathXmlApplicationContext("classpath:applicationContext-test.xml");

AopTestInterface aopUserService = ctx.getBean("aopUserService", AopTestInterface.class);
aopUserService.test();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:file:~/toby-db;AUTO_SERVER=TRUE"/>
<property name="username" value="sa"/>
</bean>

<bean id="aopUserService" class="com.javabom.toby.chapter6.AopUserService"/>

<bean id="aopUserServiceAdvice" class="com.javabom.toby.chapter6.AopUserServiceAdvice"/>

<aop:config>
<aop:aspect id="aopUserServiceAdvice" ref="aopUserServiceAdvice">
<aop:pointcut id="testPointcut" expression="execution(* *..AopTestInterface.*(..))"/>
<aop:around pointcut-ref="testPointcut" method="advice"/>
</aop:aspect>
</aop:config>
</beans>