AOP ( Aspect Oriented Programming ) : ๊ด์ ์งํฅ ํ๋ก๊ทธ๋๋ฐ
- ํต์ฌ ๊ธฐ๋ฅ๊ณผ ๊ณตํต ๊ธฐ๋ฅ์ ๋ถ๋ฆฌ ์์ผ๋๊ณ , ๊ณตํต ๊ธฐ๋ฅ์ ํ์๋ก ํ๋ ํต์ฌ ๊ธฐ๋ฅ๋ค์์ ์ฌ์ฉํ๋ ๋ฐฉ์
- ๊ณตํต ๊ธฐ๋ฅ๊ณผ ํต์ฌ ๊ธฐ๋ฅ์ด ์ง์ ์ ์ผ๋ก ์ ์ดํ์ง ์๊ณ Proxy๋ฅผ ํตํด์ ์ํ
- ์ฉ์ด ์ ๋ฆฌ
ํ๊ฒ (Target)
๋ถ๊ฐ๊ธฐ๋ฅ์ ๋ถ์ฌํ ๋์
์ ์คํํธ (Aspect)
๋ถ๊ฐ๊ธฐ๋ฅ ๋ชจ๋ (๋ถ๊ฐ๋ ๊ธฐ๋ฅ์ ์ ์ํ ์ด๋๋ฐ์ด์ค์ ์ด๋๋ฐ์ด์ค๋ฅผ ์ด๋์ ์ ์ฉํ ์ง๋ฅผ ๊ฒฐ์ ํ๋ ํฌ์ธํธ์ปท์ ํจ๊ป ๊ฐ์ง๊ณ ์๋ค.)
์ด๋๋ฐ์ด์ค (Advice)
๋ถ๊ฐ๊ธฐ๋ฅ์ ๋ด์ ๊ตฌํ์ฒด
ํฌ์ธํธ์ปท (PointCut)
์ด๋๋ฐ์ด์ค๋ฅผ ์ ์ฉํ ์กฐ์ธํฌ์ธํธ๋ฅผ ์ ๋ณํ๋ ๊ธฐ๋ฅ์ ์ ์
์กฐ์ธํฌ์ธํธ (JoinPoint)
์ด๋๋ฐ์ด์ค๊ฐ ์ ์ฉ๋ ์ ์๋ ์์น (Spring์์๋ ํ๋ก์๋ฅผ ์ด์ฉํ์ฌ ๊ตฌํํ๊ธฐ ๋๋ฌธ์ ๋ฉ์๋ ์กฐ์ธํฌ์ธํธ๋ง ์ ๊ณต)
ํ๋ก์ (Proxy)
ํ๊ฒ์ ๊ฐ์ธ์ ํ๊ฒ์ ์์ฒญ์ ๋์ ๋ฐ์์ฃผ๋ ๋ฉํ(Wrapping) ์ค๋ธ์ ํธ
- AOP ๊ตฌํ ๋ฐฉ๋ฒ
๊ณตํต : MVNRepostory์์ spring aop, aspectj weaver, aspectjrt ๋ฅผ ๊ฒ์ํ์ฌ Maven ํ๊ทธ๋ฅผ pom.xml์ ๋ฃ๊ธฐ(๊ท์ฐฎ๋ค๋ฉด ์๋ ํ๊ทธ ๋ถ์ฌ๋ฃ๊ธฐ)
AspectJRT : aspectj ๋ฐํ์ ํ๋ก๊ทธ๋จ. AspectJ์ ๊ธฐ๋ฅ์ ์ฌ์ฉํ ๋ก ํ๋ค.
AspectJ Weaver : aspect์ ์ ๋ณด๋ฅผ ๋ฐํ์ผ๋ก aspect๋ฅผ ๊ตฌ์ฑํ ์ฝ๋๋ฅผ ์์ฑํ๋๋ฐ ํ์ํ ํ๋ก๊ทธ๋จ
์ ๋๊ฐ๋ ์ธํธ.
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.3.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.4</version>
</dependency>
1. xml ์คํค๋ง ๊ธฐ๋ฐ ๊ตฌํ
1. src์์ bean.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">
<!--
๊ฐ์ฒด ์์ฑ
java
LogAOP logAop = new LogAOP();
xml:
<bean id="logAop" class="com.aop.LogAOP"/>
jsp tag
<jsp:useBean id="logAop" class="com.aop.LogAOP"/>
-->
<!-- AOP Objcet ์์ฑ -->
<bean id="logAop" class="com.aop.LogAOP"/>
<!-- AOP ์ค์ (์๋ ํธ์ถ) - callback -->
<aop:config>
<aop:aspect id="logger" ref="logAop">
<aop:pointcut expression="within(com.dto.*)" id="publicM"/> <!-- dto ํจํค์ง ์์์๋ ํด๋์ค๊ฐ ์คํ๋ ๋๋ง๋ค -->
<aop:around method="loggerAop" pointcut-ref="publicM"/>
</aop:aspect>
</aop:config>
<!-- xml Cat Object New -->
<bean id="myCat" class="com.dto.Cat">
<property name="name" value="์ผ์น์ด"/>
<property name="age" value="3"/>
<property name="color" value="๋
ธ๋"/>
</bean>
</beans>
2. Main ํด๋์ค
package com.main;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.dto.Cat;
public class MainClass {
public static void main(String[] args) {
// Java์์ xml ์คํ ์
AbstractApplicationContext ctx = new GenericXmlApplicationContext("bean.xml");
// xml์์ ์์ฑ๋ Object๋ฅผ ์ฝ์ด ์จ๋ค
Cat myCat = ctx.getBean("myCat", Cat.class);
myCat.info();
myCat.setAge(5);
}
}
3. log๋ฅผ ์์ฑํด์ค LogAOP ํด๋์ค
package com.aop;
import org.aspectj.lang.ProceedingJoinPoint;
public class LogAOP {
public Object loggerAop(ProceedingJoinPoint joinpoint) throws Throwable {
String signatureStr = joinpoint.getSignature().toShortString();
System.out.println(signatureStr + " ์์");
try {
Object obj = joinpoint.proceed(); // ์คํ ์
return obj;
}finally {
System.out.println("์คํ ํ : "+System.currentTimeMillis());
System.out.println(signatureStr + " ์ข
๋ฃ"); //์คํ ํ
}
}
}
4. ๊ณ ์์ด DTO
package com.dto;
public class Cat {
private String name;
private int age;
private String color;
public Cat() {
// TODO Auto-generated constructor stub
}
public Cat(String name, int age, String color) {
super();
this.name = name;
this.age = age;
this.color = color;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public void info() {
System.out.println("์ด๋ฆ : "+name);
System.out.println("๋์ด : "+age);
System.out.println("์ : "+color);
}
}
2. @Aspect ์ด๋ ธํ ์ด์ ๊ธฐ๋ฐ ๊ตฌํ (1๋ฒ๋ณด๋ค ๊ฐํธํ๋ค.)
1. src์์ bean.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 Objcet ์์ฑ -->
<bean id="logAop" class="com.aop.LogAOP"/>
<!-- AOP Annotation (XML๊ณผ ์ฐจ์ด๋๋ ๊ณณ) -->
<aop:aspectj-autoproxy/>
<!-- xml Cat Object New -->
<bean id="myCat" class="com.dto.Cat">
<property name="name" value="๊ณ ์์ด"/>
<property name="age" value="2"/>
<property name="color" value="ํฐ์"/>
</bean>
</beans>
2. LogAOP.java
package com.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
/*์ด๋
ธํ
์ด์
์ถ๊ฐ*/
@Aspect
public class LogAOP {
/* com.dto์์ ํด๋์ค์ ์ฐ๊ฒฐ */
@Around("within(com.dto.*)")
public Object loggerAop(ProceedingJoinPoint joinpoint) throws Throwable {
String signatureStr = joinpoint.getSignature().toShortString();
System.out.println(signatureStr + " ์์");
try {
Object obj = joinpoint.proceed(); // ์คํ ์
return obj;
}finally {
System.out.println("์คํ ํ : "+System.currentTimeMillis());
System.out.println(signatureStr + " ์ข
๋ฃ"); //์คํ ํ
}
}
}
MainClass์ CatDTO๋ XML๋ฐฉ์๊ณผ ๋๊ฐ๋ค.
@Before (์ด์ )
- ์ด๋๋ฐ์ด์ค ํ๊ฒ ๋ฉ์๋๊ฐ ํธ์ถ๋๊ธฐ ์ ์ ์ด๋๋ฐ์ด์ค ๊ธฐ๋ฅ์ ์ํ
@After (์ดํ)
- ํ๊ฒ ๋ฉ์๋์ ๊ฒฐ๊ณผ์ ๊ด๊ณ์์ด(์ฆ ์ฑ๊ณต, ์์ธ ๊ด๊ณ์์ด) ํ๊ฒ ๋ฉ์๋๊ฐ ์๋ฃ ๋๋ฉด ์ด๋๋ฐ์ด์ค ๊ธฐ๋ฅ์ ์ํ
@Around (๋ฉ์๋ ์คํ ์ ํ)
- ์ด๋๋ฐ์ด์ค๊ฐ ํ๊ฒ ๋ฉ์๋๋ฅผ ๊ฐ์ธ์ ํ๊ฒ ๋ฉ์๋ ํธ์ถ์ ๊ณผ ํ์ ์ด๋๋ฐ์ด์ค ๊ธฐ๋ฅ์ ์ํ
within() : ํด๋์ค ํน์ ์ธํฐํ์ด์ค ๋จ์๊น์ง๋ง ๋ฒ์ ์ง์ ์ด ๊ฐ๋ฅ
- ex) within(com.dto.*) : dto ํจํค์ง ์๋์ ํด๋์ค์ ์ธํฐํ์ด์ค๊ฐ ๊ฐ์ง ๋ชจ๋ ๋ฉ์๋
- ex) within(com.dto..) : dto ์๋์ ๋ชจ๋ ํ์ ํจํค์ง๊น์ง ํฌํจํ ํด๋์ค์ ์ธํฐํ์ด์ค๊ฐ ๊ฐ์ง ๋ฉ์๋
โป AOP๋ฅผ ์น๊ณผ ์ฐ๋์์๋ WEB-IMF/spring ํด๋์ aop-context.xml์ ์ถ๊ฐํ๊ณ ,
web-xml์ dispatcher 'param-value'์ xml์ ์์น๋ฅผ ์ถ๊ฐํด์ค๋ค.
aop-context.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/>
<!-- AOP ํด๋น ํด๋์ค๋ฅผ ์์ฑ -->
<bean id="myAspect" class="bit.com.a.aop.LogAOP"/>
</beans>
web.xml
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/servlet-context.xml
/WEB-INF/spring/aop-context.xml <!-- ์ด๋ถ๋ถ ์ถ๊ฐ -->
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Tiles
- ๋ทฐํ์ด์ง์ jsp๋ค์ ์๋จ, ์ฌ์ด๋, ๋ฉ์ธ, ํ๋จ ๋ฑ์ ํ์ด์ง include ๋ฐฉ์์ผ๋ก ๋๋์ด ๋ฐ๋ณต๋๋ ๋ถ๋ถ์ ์ค์ ํ์ผ๋ก
๊ด๋ฆฌ๊ฐ ๊ฐ๋ฅ
1 .pom.xml tiles ์ถ๊ฐ
<!-- tiles -->
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-api</artifactId>
<version>3.0.7</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-core</artifactId>
<version>3.0.7</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-jsp</artifactId>
<version>3.0.7</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-servlet</artifactId>
<version>3.0.7</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-template</artifactId>
<version>3.0.7</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-autotag-core-runtime</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-request-api</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-request-jsp</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-request-servlet</artifactId>
<version>1.0.6</version>
</dependency>
2. WEB-INF/spring/servlet-context.xml ์์
๊ธฐ์กด์ viewResolver๋ฅผ ์ง์ฐ๊ณ ํ๋จ๊ณผ ๊ฐ์ด ์ค์ ํ๋ค.
<!-- tiles ์ค์ -->
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/views/layouts.xml</value> <!-- jsp์ ์งํฉ์ฒด -->
</list>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="requestContextAttribute" value="requestContext"/>
<property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView"/>
</bean>
3. WEB-INF/views/layouts.xml ์์ฑ
definitionํ๊ทธ์ name์ ์ปจํธ๋กค๋ฌ ์ด๋ ์์ ์ฌ์ฉ
template์๋ ํ ์ด๋ธ ํ์์ ๊ฐ์ถ jsp์ ์์น๋ฅผ ์์ฑํ๋ค.
extends๋ฅผ ํตํ์ฌ ํด๋น ํ๊ทธ์ attribute๋ฅผ ์์ ๋ฐ์ ์ ์๋ค.
์๋์ ์์๋ bbslist.tiles๋ก ์ด๋ํ ๊ฒฝ์ฐ header, menu, empty, footer๋ง ์์๋ฐ๊ณ content๋ ์๋ก์ด ํ์ด์ง๋ฅผ ๋ถ๋ฌ์จ๋ค.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN"
"http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
<tiles-definitions>
<definition name="login.tiles" template="/WEB-INF/views/layouts-tiles.jsp">
<put-attribute name="header" value="/WEB-INF/views/header.jsp"/>
<put-attribute name="menu" value="/WEB-INF/views/login/menu.jsp"/>
<put-attribute name="content" value="/WEB-INF/views/login/content.jsp"/>
<put-attribute name="empty" value="/WEB-INF/views/empty.jsp"/>
<put-attribute name="footer" value="/WEB-INF/views/footer.jsp"/>
</definition>
<definition name="bbslist.tiles" extends="login.tiles">
<put-attribute name="content" value="/WEB-INF/views/bbs/content.jsp"/>
</definition>
</tiles-definitions>
template
ํ ์ด๋ธ ํ์์ผ๋ก ๊ตฌ์ฑ
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- ๊ธฐ๋ณธ layout -->
<table style="width:100%; height:100%; border-color:gray">
<!-- header -->
<tr align="center" style="background-color:black; color:white;">
<td style="height:10%;" colspan="3">
<tiles:insertAttribute name="header"/>
</td>
</tr>
<!-- menu, main -->
<tr>
<td style="width: 20%; background-color: rgb(204,204,204)" align="left" valign="top">
<tiles:insertAttribute name="menu"/>
</td>
<td>
<tiles:insertAttribute name="content"/>
</td>
<td width="15%" align="center">
<tiles:insertAttribute name="empty"/>
</td>
</tr>
<!-- footer -->
<tr align="center" style="background-color: black; color: white;">
<td height="10%" colspan="3">
<tiles:insertAttribute name="footer"/>
</td>
</tr>
</table>
</body>
</html>
์ถ๋ ฅ ์์
'๐ป ๊ฐ๋ฐ > ๐ TIL (Today I Learned)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
210422 TIL (D+71 ์คํ๋ง File ์ ๋ก๋, ๋ค์ด๋ก๋ ๊ตฌํ) (0) | 2021.04.22 |
---|---|
ajax ํ์ฉ ์บ๋ฆฐ๋ ๋ง๋ค๊ธฐ (0) | 2021.04.21 |
21.04.14 TIL (D+65 Spring, ajax) (0) | 2021.04.14 |
21.04.13 TIL (D+64 MyBatis, Spring) (0) | 2021.04.13 |
21.04.12 TIL (D+63 Spring) (0) | 2021.04.12 |