2014년 4월 24일 목요일

aop네임스페이스를 이용하여 AOP구현(Spring3.X,박태근) ,Spring교육잘하는곳/자바,spring교육추천/자바실무교육/JAVA/JAVA교육

aop네임스페이스를 이용하여 AOP구현(Spring3.X,박태근) ,Spring교육잘하는곳/자바,spring교육추천/자바실무교육/JAVA/JAVA교육

- Spring3.X의 aop Namespace를 이용하여

-------------------------------OnjWorldImpl --------------------------------------

package onj.edu.project_Namespace;

import onj.edu.project.OnjWorld;

public class OnjWorldImpl {
public void onjHello(){
System.out.println("Onj에 오신것을 환영합니다. ----Hello1----");
}

public void onjHello1() throws Exception{
System.out.println("Onj에 오신것을 환영합니다.---- Hello2----");
//throw new Exception("Exception");
}

public void onjGoodBye(){
System.out.println("끝났어요~~~~~~");
}
 
}

----------------------------------- OnjWorldBean -----------------------------

package onj.edu.project_Namespace;


public class OnjWorldBean {

private OnjWorldImpl dependency;

public void run() throws Exception{
dependency.onjHello();
dependency.onjHello1();
dependency.onjGoodBye();
}

public void setDependency(OnjWorldImpl dependency){
this.dependency = dependency;
}
}

-------------------- OnjWorldAdvice ----------------------------------------------------
package onj.edu.project_Namespace;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

public class OnjWorldAdvice {
      public void beforeAdvice(JoinPoint joinPoint){
System.out.println("----비포 실행----"
                                        + joinPoint.getSignature().getDeclaringTypeName()+ ","
        +  joinPoint.getSignature().getName());
}

public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("----어라운드 비포 실행----"
                                          + joinPoint.getSignature().getDeclaringTypeName()+ ","
          +  joinPoint.getSignature().getName());
Object rVal = joinPoint.proceed();
                      System.out.println("----어라운드 에프터 실행----"
                                                + joinPoint.getSignature().getDeclaringTypeName()+ ","
+  joinPoint.getSignature().getName());
return rVal;
}
}

------------------------------ OnjWorldExam  -------------------------------------
package onj.edu.project_Namespace;

import org.springframework.context.support.GenericXmlApplicationContext;

public class OnjWorldExam {
public static void main(String args[]) throws Exception{
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("worldName.xml");
ctx.refresh();

OnjWorldBean bean = (OnjWorldBean)ctx.getBean("onjWorldBean");
System.out.println("--------bean------------");
bean.run();
ctx.close();
}
}

----------------------------- worldName ---------------------------------------------

<?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.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  id="onjpointcut" __EXPRESSION__="execution(* onj.edu.project_Namespace..onjHello(..))"/>
      <aop:aspect ref="advice">
<aop:before pointcut-ref ="onjpointcut" method="beforeAdvice"/>
<aop:around pointcut-ref="onjpointcut"  method="aroundAdvice"/>
      </aop:aspect>
</aop:config>

<bean id="advice" class="onj.edu.project_Namespace.OnjWorldAdvice"/>

<bean id="onjWorldImpl" class="onj.edu.project_Namespace.OnjWorldImpl"/>

<bean id="onjWorldBean" class="onj.edu.project_Namespace.OnjWorldBean">
<property name="dependency" ref="onjWorldImpl"/>
</bean>
</beans> 

[출처] 오라클자바커뮤니티 - http://www.oraclejavanew.kr/bbs/board.php?bo_table=Cyber&wr_id=642 
 
 

댓글 없음:

댓글 쓰기