javascript
一个简单的Spring的AOP例子(JAVA面向切面编程)
公司要求分享技術,一個小組九個人,每周一個人,想想講講AOP吧,用處也挺大的,果斷寫了一個小例子,改了改也就成了。這是個簡單AOP例子,包括前置通知,后置通知,環繞通知,和目標對象。
目標對象的接口:IStudent.java
package ?com.dragon.study;
/**?
?* ?@author ?ysm
?*/?
public ? interface ?IStudent ?{
????public ? void ?addStudent(String name);
}?
目標類:StudentImpl.java
package ?com.dragon.study.Impl;
import ?com.dragon.study.IStudent;
/**?
?* ?@author ?ysm
?*/?
public ? class ?StudentImpl ?implements ?IStudent {
????public ? void ?addStudent(String name) {
????????System.out.println( " 歡迎 ?" + name + " ?加入Spring家庭! " );
????}?
} ?
前置通知:BeforeAdvice.java
package ?com.dragon.Advice;
import ?java.lang.reflect.Method;
import ?org.springframework.aop.MethodBeforeAdvice;
/**?
?* ?@author ?ysm
?*/?
public ? class ?BeforeAdvice ?implements ?MethodBeforeAdvice {
????public ? void ?before(Method method,Object[] args, Object target)?throws ?Throwable {
????????System.out.println( " 這是BeforeAdvice類的before方法. " );
????}?
}
后置通知:AfterAdvice.java
package com.dragon.Advice;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
/**
?* @author ysm
?*/
public class AfterAdvice implements AfterReturningAdvice{
????public void afterReturning(Object returnValue ,Method method,Object[] args,Object target) throws Throwable{
????????System.out.println("這是AfterAdvice類的afterReturning方法.");
????}
}
環繞通知:CompareInterceptor.java
package com.dragon.Advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
/**
?* @author ysm
?*/
public class CompareInterceptor implements MethodInterceptor{
????public Object invoke(MethodInvocation invocation) throws Throwable{
????????Object result = null;
????????String stu_name = invocation.getArguments()[0].toString();
????????if ( stu_name.equals("ysm")){
????????????//如果學生是ysm時,執行目標方法,
????????????result= invocation.proceed();
????????} else{
????????????System.out.println("此學生是"+stu_name+"而不是ysm,不批準其加入.");
????????}
????????return result;
????}
}
配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="beforeAdvice" class="com.dragon.Advice.BeforeAdvice"></bean>
<bean id="afterAdvice" class="com.dragon.Advice.AfterAdvice"></bean>
<bean id="compareInterceptor" class="com.dragon.Advice.CompareInterceptor"></bean>
<bean id="studenttarget" class="com.dragon.study.Impl.StudentImpl"></bean>
<bean id="student" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.dragon.study.IStudent</value>
</property>
<property name="interceptorNames">
<list>
<value>beforeAdvice</value>
<value>afterAdvice</value>
<value>compareInterceptor</value> ?
</list>
</property>
<property name="target">
<ref bean="studenttarget"/>
</property>
</bean>
</beans>
現在開始寫測試類,Test.java
package com;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.dragon.study.IStudent;
/**
?* @author ysm
?*/
public class Test {
????/**
?????* @param args
?????*/
????public static void main(String[] args) {
????????// TODO Auto-generated method stub
????????ApplicationContext ctx =?new FileSystemXmlApplicationContext("/com/dragon/applicationContext.xml");
????????IStudent person = (IStudent)ctx.getBean("student");
????????person.addStudent("ysm");
????????//person.addStudent("javadragon");
????}
}
轉載于:https://blog.51cto.com/yangsm/1427657
總結
以上是生活随笔為你收集整理的一个简单的Spring的AOP例子(JAVA面向切面编程)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JQuery表单验证插件EasyVali
- 下一篇: hwclock