[스프링
횡단관심사]Spring aop 네임스페이스를 이용한 AOP구현(Spring 선언적 AOP, aop 네임스페이스)-SmallMart예제
이전에 작성한 SmallMart예제를 aop namespace를 이용한 방식으로 바꾸어 보자.
[pom.xml]
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.samples</groupId>
<artifactId>springonj</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<!--
Generic properties -->
<java.version>1.6</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!--
Spring -->
<spring-framework.version>3.2.3.RELEASE</spring-framework.version>
<!--
Hibernate / JPA -->
<hibernate.version>4.2.1.Final</hibernate.version>
<!--
Logging -->
<logback.version>1.0.13</logback.version>
<slf4j.version>1.7.5</slf4j.version>
<!--
Test -->
<junit.version>4.11</junit.version>
<!--
AspectJ -->
<aspectj.version>1.7.4</aspectj.version>
</properties>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
<!--
Spring and Transactions -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<!--
Logging with SLF4J & LogBack -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>runtime</scope>
</dependency>
<!--
Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!--
Test Artifacts -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-framework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
[SmallMart.java]
public interface SmallMart {
public
void getProducts(String productName) throws Exception;
}
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->
[SmallMartImpl.java]
public class SmallMartImpl implements
SmallMart {
public void getProducts(String
productName) throws Exception {
System.out.println("getProduct()..."+productName);
}
}
[OracleJavaAdvice.java]
import
org.aspectj.lang.JoinPoint;
import
org.aspectj.lang.ProceedingJoinPoint;
public
class OracleJavaAdvice {
public
void afterReturning(JoinPoint joinPoint) {
// TODO
Auto-generated method stub
System.out.println(joinPoint.getSignature().getName() +" :
사후충고");
}
public
void before(JoinPoint joinPoint) {
// TODO
Auto-generated method stub
System.out.println(joinPoint.getSignature().getName()+"::사전충고.");
}
public
void afterThrowing(){
System.out.println("예외충고 발생...");
}
public
Object invoke(ProceedingJoinPoint joinPoint) throws Throwable
{
// TODO
Auto-generated method stub
String
findName = (String)joinPoint.getSignature().getName();
if
(findName == null){
throw
new Exception("예외");
}
System.out.println("메소드 실행전");
Object
returnedObj = joinPoint.proceed();
System.out.println("메소드 실행후");
return
returnedObj;
}
}
[SmallMartApp.java]
import
org.springframework.context.support.GenericXmlApplicationContext;
public class SmallMartApp {
public static void
main(String[] args) throws Exception{
// TODO Auto-generated method
stub
GenericXmlApplicationContext ctx = new
GenericXmlApplicationContext();
ctx.load("small.xml");
ctx.refresh();
SmallMart smallMart =
(SmallMart)ctx.getBean("smallMart");
smallMart.getProducts("과자");
ctx.close();
}
}
[smallmart.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"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<aop:config>
<aop:pointcut __EXPRESSION__="execution(*
onj.edu.small..getProducts*(String)) and bean(smallMart*)"
id="onjpointcut"/>
<aop:aspect ref="advice">
<aop:before method="before"
pointcut-ref="onjpointcut"/>
<aop:after-returning method="afterReturning"
pointcut-ref="onjpointcut"/>
<aop:around method="invoke"
pointcut-ref="onjpointcut"/>
<aop:after-throwing method="afterThrowing"
pointcut-ref="onjpointcut"/>
</aop:aspect>
</aop:config>
<bean id="advice"
class="onj.edu.small.OracleJavaAdvice"/>
<bean id="smallMart"
class="onj.edu.small.SmallMartImpl"/>
</beans>
댓글 없음:
댓글 쓰기