티스토리 뷰

Spring

Filter, Interceptor, AOP

gajy 2022. 4. 13. 20:13
728x90

 

1. Filter: 정제역할, 예) XSS filter

2. Interceptor: 가로채기 예) 권한관리

3. AOP: 관점 지향 프로그래밍 예) 로깅, 트랜젝션, 에러처리

-> 프로그래밍을 하다보면 공통적인 기능이 많이 발생합니다. 이러한 공통 기능은 상속을 통해서 모든 모듈에 적용을 시켜줄 수 있지만 몇 가지 문제가 있지요. 우선 JAVA에서는 다중 상속이 불가능하기 때문에 한계가 있고, 기능을 구현하는 부분에 핵심 기능 코드와 공통 기능 코드가 섞여 있어서 효율이 떨어집니다. 이러한 문제점때문에 핵심 기능과 공통 기능을 분리 시켜놓고, 공통 기능을 필요로 하는 핵심 기능들에서 사용하는 방식의 AOP가 등장을 하게 되었습니다. 스프링은 자체적으로 프록시 기반의 AOP를 지원하고 있습니다.

* proxy: 타겟을 감싸서 요청을 대신 받아주는 랩핑 클래스. Weaving을 통해서 Proxy 객체를 생성하며 Spring AOP Proxy는 CGLIB Proxy, JDK Dynamic Proxy를 사용한다. 스프링의 AOP는 이 프록시 객체를 통해 작동하게 된다.

- Aspect : 공통 기능

- Advice : Aspect의 기능 자체

- Aspect를 공통 기능이라고 크게 묶었으면 Advice는 그 안의 세부적인, 주요 기능이라고 생각하시면 됩니다.

- Joinpoint : Advice를 적용해야 되는 부분(ex 필드, 메소드 - 스프링에서는 메소드만 해당)

- Pointcut : Joinpoint의 부분으로 실제로 Advice가 적용된 부분

- Weaving : Advice를 핵심 기능에 적용 하는 행위

 

예) 배치 트랜젝션 관리

<?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:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
                        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

    <!-- default txManager -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- Annotation 을 사용한 트랜잭션 사용시 활성화 -->
    <tx:annotation-driven transaction-manager="txManager" />

    <!-- PostgreSQL txAdvice -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
       <tx:attributes>
            <tx:method name="get*" read-only="true" />
            <tx:method name="*" propagation="REQUIRED" rollback-for="Exception"/>
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:pointcut id="requiredTx" expression="execution(* com.test.service.testone..*ServiceImpl.*(..)) and !execution(* com.test.service.testone.service.common.impl.MailServiceImpl.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="requiredTx" />
    </aop:config>

</beans>
 

Filter → Interceptor → AOP → Interceptor → Filter 순

 

 

 

 

참고: https://goddaehee.tistory.com/154

 

 

728x90
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31