> - 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>
-------------------------------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>
댓글 없음:
댓글 쓰기