AOP
- AOP 주요 디자인 패턴 : Proxy 디자인 패턴
- AOP advice종류
1. Before : 메소드 실행 전 Advice실행
2. After : 메서드 실행 후 Advice실행
3. After-returning : 메서드가 성공 후 (예외없이) Advice실행
4. After-throwing : 메서드가 예외발생 후 Advice 실행
5. Around : 메소드 실행 전과 후 Advice실행(Before + Afeter) - AOP Pointcut표현식
1. execution : 리턴 타임, 패키지 경로, 클래스명, 메서드명(매개변수)
2. within : 타입 패턴 내에 해당하는 모것들을 포인트 컷
3. bean : bean 이름으로 포인트컷
aop_3패키지에
Person.java 인터페이스 생성
package aop_3;
public interface Person {
void runSomething();
}
Boy.java생성
package aop_3;
public class Boy implements Person{
@Override
public void runSomething() {
System.out.println("컴퓨터");
}
}
Girl.java생성
package aop_3;
public class Girl implements Person {
@Override
public void runSomething() {
System.out.println("요리");
}
}
MyAspect.java생성
package aop_3;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class MyAspect {
public void before(JoinPoint joinpoint) {
System.out.println("얼굴 인식 확인 : open");
}
}
aop_3.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:aop="http://www.springframework.org/schema/aop"
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-4.3.xsd">
<aop:aspectj-autoproxy/>
<!--
AspectJ를 위한 태그이며, 먼저 Spring AOP에 대한 ProxyFactoryBean에 해당하는 것을
자동으로 생성하는 태그
ProxyFactoryBean으로 준비된 기능이 자동으로 포함됨
-->
<bean id="myAspect" class="aop_3.MyAspect"></bean>
<bean id="boy" class="aop_3.Boy"></bean>
<bean id="girl" class="aop_3.Girl"></bean>
<aop:config>
<aop:aspect ref="myAspect">
<aop:before method="before" pointcut="execution(* runSomething())"/>
</aop:aspect>
</aop:config>
</beans>
Start.java생성
package aop_3;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Start {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("aop_3/aop_3.xml");
Person romeo = context.getBean("boy", Person.class);
Person juliet = context.getBean("girl", Person.class);
romeo.runSomething();
System.out.println();
juliet.runSomething();
}
}
↓결과
얼굴 인식 확인 : open
컴퓨터
얼굴 인식 확인 : open
요리
aop_4패키지에
Person.java인터페이스,
Boy.java,
Girl.java,
MyAspect.java생성
package aop_4;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class MyAspect {
public void before(JoinPoint joinpoint) {
System.out.println("얼굴 인식 확인 : open/aop_4");
}
public void lockDoor(JoinPoint joinpoint) {
System.out.println("얼굴 인식 확인 : closed/aop_4");
}
}
aop_4.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:aop="http://www.springframework.org/schema/aop"
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-4.3.xsd">
<aop:aspectj-autoproxy/>
<bean id="myAspect" class="aop_4.MyAspect"></bean>
<bean id="boy" class="aop_4.Boy"></bean>
<bean id="girl" class="aop_4.Girl"></bean>
<aop:config>
<!-- id는 별칭이기때문에 임의적인것으로 설정가능 -->
<aop:pointcut expression="execution(* runSomething())" id="iamking"/>
<aop:aspect ref="myAspect">
<aop:before method="before" pointcut-ref="iamking"/>
<aop:after method="lockDoor" pointcut-ref="iamking"/>
</aop:aspect>
</aop:config>
</beans>
Start.java생성
package aop_4;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Start {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("aop_4/aop_4.xml");
Person romeo = context.getBean("boy", Person.class);
Person juliet = context.getBean("girl", Person.class);
romeo.runSomething();
System.out.println();
juliet.runSomething();
}
}
↓결과
얼굴 인식 확인 : open/aop_4
컴퓨터/aop_4
얼굴 인식 확인 : closed/aop_4
얼굴 인식 확인 : open/aop_4
요리/aop_4
얼굴 인식 확인 : closed/aop_4
aop_5패키지에
Person.java인터페이스,
Boy.java,
Girl.java,
MyAspect.java생성
package aop_5;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class MyAspect {
@Pointcut("execution(* runSomething())")
private void abc() {
//여기는 무엇을 작성해도 의미없음
}
@Before("abc()")
public void before(JoinPoint joinPoint) {
System.out.println("얼굴 인식 : open / aop_5");
}
@After("abc()")
public void lockDoor(JoinPoint joinPoint) {
System.out.println("얼굴 인식 : closed / aop_5");
}
}
aop_5.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:aop="http://www.springframework.org/schema/aop"
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-4.3.xsd">
<aop:aspectj-autoproxy/>
<bean id="myAspect" class="aop_5.MyAspect"></bean>
<bean id="boy" class="aop_5.Boy"></bean>
<bean id="girl" class="aop_5.Girl"></bean>
</beans>
Start.java 생성
package aop_5;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Start {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("aop_5/aop_5.xml");
Person romeo = context.getBean("boy", Person.class);
Person juliet = context.getBean("girl", Person.class);
romeo.runSomething();
System.out.println();
juliet.runSomething();
}
}
↓결과
얼굴 인식 : open / aop_5
컴퓨터/aop_5
얼굴 인식 : closed / aop_5
얼굴 인식 : open / aop_5
요리/aop_5
얼굴 인식 : closed / aop_5
aop_6패키지에
Person.java인터페이스,
Boy.java생성
package aop_6;
import org.springframework.stereotype.Component;
@Component("sunflower")
public class Boy implements Person{
@Override
public void runSomething() {
System.out.println("컴퓨터/aop_6");
}
}
Girl.java생성
package aop_6;
import org.springframework.stereotype.Component;
@Component
public class Girl implements Person {
@Override
public void runSomething() {
System.out.println("요리/aop_6");
}
}
MyAspect.java생성
package aop_6;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/*
@Component : Bean Configuration파일에 Bean을 등록하지 않고 사용가능
Bean클래스 자체에 할 수 있다는 의미
type기반의 자동주입 어노테이션
@Autowired, @Resource와 비슷한 기능을 수행한다고 할 수 있으
*/
@Component
@Aspect
public class MyAspect {
@Pointcut("execution(* runSomething())")
private void abc() {
//여기는 무엇을 작성해도 의미없음
}
@Before("abc()")
public void before(JoinPoint joinPoint) {
System.out.println("얼굴 인식 : open / aop_6");
}
@After("abc()")
public void lockDoor(JoinPoint joinPoint) {
System.out.println("얼굴 인식 : closed / aop_6");
}
}
aop_6.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!--
context : component-scan
bean으로 등록 될 준비를 마친 클래스들을 스캔하며, bean으로 등록해주는것
@Component 어노테이션을 빈 등록 대상으로 포함
@Controller나 @Service가 @Component를 포함하고 있기때문에 인식가능
<content:component-scan base-package="com.korea.myapp"/>
: 다음과 같이 xml 파일에 설정하고, base package를 적어주면 base package기준으로 클래스들을 스캔하여 빈에 등록
base-package : 패키지를 어디서 부터 스캔할지 지정해주는 부분, 여러개 지정 가능
-->
<context:component-scan base-package="aop_6"/>
<aop:aspectj-autoproxy/>
</beans>
Start.java생성
package aop_6;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Start {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("aop_6/aop_6.xml");
//Boy.java의 Conponetn에 별칭을 줬기 때문에 sunflower사용 가능
//sunfower대신 boy를 쓸 경우 error가 나옴
Person romeo = context.getBean("sunflower", Person.class);
Person juliet = context.getBean("girl", Person.class);
romeo.runSomething();
System.out.println();
juliet.runSomething();
}
}
↓결과
얼굴 인식 : open / aop_6
컴퓨터/aop_6
얼굴 인식 : closed / aop_6
얼굴 인식 : open / aop_6
요리/aop_6
얼굴 인식 : closed / aop_6
'Spring' 카테고리의 다른 글
[Spring]Lambda 표현식 (0) | 2023.06.05 |
---|---|
221125_Spring_국비_암호화/AOP (1) | 2022.11.25 |
221124_Spring_국비_DI/DesignPattern/@Resource/@Autowired/@Inject (0) | 2022.11.24 |