Spring Aop 예 (인터페이스 구현)
4699 단어 spring aop
다음은 예 입 니 다.
beans.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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
default-lazy-init="false" default-autowire="no">
<bean id="around" class="org.hzy.advices.AroundAdvice"></bean>
<bean id="before" class="org.hzy.advices.BeforeAdvice"></bean>
<bean id="after" class="org.hzy.advices.AfterAdvice"></bean>
<bean id="student" class="org.hzy.dao.impl.StudentImpl"></bean>
<aop:config>
<aop:pointcut id="target" expression="execution(* org.hzy.dao.impl.StudentImpl.addStudent(..))"/>
<!-- <aop:advisor id="be" advice-ref="before" pointcut-ref="target"/> -->
<!-- <aop:advisor id="af" advice-ref="after" pointcut-ref="target"/> -->
<aop:advisor id="ar" advice-ref="around" pointcut-ref="target"/><!-- , -->
</aop:config>
</beans>
AroundAdvice.java
package org.hzy.advices;
import java.lang.reflect.Method;
import org.aopalliance.intercept.Invocation;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class AroundAdvice implements MethodInterceptor{//
public Object invoke(MethodInvocation arg0) throws Throwable {//arg0 struts2 actionInvocation
// TODO Auto-generated method stub
System.out.println(" !!!!!!!!");
Method me=arg0.getMethod();
System.out.println(me.getName());
System.out.println(arg0.getArguments()[0]);
Object result=null;
String stu_name=arg0.getArguments()[0].toString();
if("hzy".equals(stu_name)){
result= arg0.proceed();
}else{
System.out.println(" "+stu_name+" dragon, .");
}
return result;
}
}
BeforeAdvice.java
package org.hzy.advices;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class BeforeAdvice implements MethodBeforeAdvice{//
public void before(Method method, Object[] args, Object target)
throws Throwable {
// TODO Auto-generated method stub
System.out.println( " BeforeAdvice before . " );
// args[0]="hzy";
System.out.println(args[0]);
}
}
AfterAdvice.java
package org.hzy.advices;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class AfterAdvice implements AfterReturningAdvice{//
public void afterReturning(Object returnValue, Method method,
Object[] args, Object target) throws Throwable {
// TODO Auto-generated method stub
System.out.println(" AfterAdvice afterReturning .");
System.out.println(target.getClass());
}
}
IStudent.java
package org.hzy.dao;
public interface IStudent {
public void addStudent(String name);
}
StudentImpl.java
package org.hzy.dao.impl;
import org.hzy.dao.IStudent;
public class StudentImpl implements IStudent {
public void addStudent(String name) {
// TODO Auto-generated method stub
System.out.println( " " + name + " Spring ! " );
}
}
package org.hzy.test;
import org.hzy.dao.IStudent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest2 {
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans1.xml");
IStudent st=(IStudent) ctx.getBean("student");
st.addStudent("aa");
}
}
메모: 이것 은 Spring 의 Aop 모방 일 뿐 입 니 다. Aop 을 설정 하여 조작 할 수 있 습 니 다.