Spring中AOP简单实践

目标对象的接口:Student.java

 

package myAop;

public interface Student {
    public void addStudent(String stuName);

}

 

目标类:StudentImpl.java

package myAop;


public class StudentImpl implements Student {

    
    public void addStudent(String stuName) {
        System.out.println("hello ," + stuName + ",welcome to spring");

    }

}

 

前置通知:BeforeAdvice.java

package myAop;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;


public class BeforeAdvice implements MethodBeforeAdvice {

    
    public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
        System.out.println("before advice");

    }

}

 

后置通知:AfterAdvice.java

package myAop;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;


public class AfterAdvice implements AfterReturningAdvice {

    @Override
    public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
        System.out.println("after  advice");

    }

}

 

环绕通知:CompareInterceptor.java

package myAop;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

/**
 * 
 */
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("dragon")) {
            // 如果学生是dragon时,执行目标方法,
            result = invocation.proceed();
        } else {
            System.out.println("此学生是" + stu_name + "而不是dragon,不批准其加入.");
        }
        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="myAop.BeforeAdvice"></bean>
	<bean id="afterAdvice" class="myAop.AfterAdvice"></bean>
	<bean id="compareInterceptor" class="myAop.CompareInterceptor"></bean>
	<bean id="studenttarget" class="myAop.StudentImpl"></bean>
	<bean id="student" class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="proxyInterfaces">
			<value>myAop.Student</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 myAop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

/**
 */
public class Test {

    public static void main(String[] args) {
        ApplicationContext ctx = new FileSystemXmlApplicationContext("/src/applicationContext.xml");
        Student s = (Student) ctx.getBean("student");
        s.addStudent("ss"); 
    }
}
 


转载地址:http://www.blogjava.net/javadragon/archive/2006/12/03/85115.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值