环绕通知

环绕通知:
1.创建一个MyAroundAdvice.java
package cn.csdn.aop.advice;

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

public class MyAroungAdvice implements MethodInterceptor {

@Override
public Object invoke(MethodInvocation arg0) throws Throwable {
System.out.println("around完成----登录到网站的操作。。。");
Object obj =arg0.proceed();
System.out.println("around完成----提示你注册成功。。。");
return obj;
}

}


2.创建一个接口CustormService.java
package cn.csdn.aop.service;
/**/
public interface CustormService {
//客户注册的方法,用户名,邮箱,密码
void register(String name,String Email,String Password);

}


3.创建一个CustormServiceImpl.java

package cn.csdn.aop.service;
//目标对象的类
public class CustormServiceImpl implements CustormService{

@Override
public void register(String name, String Email, String Password) {

System.out.println("---------用户在这里实现注册的功能..........");
}

}


4.该配置文件
<!-- 环绕通知的配置 -->
<bean id="myAroungAdvice" class="cn.csdn.aop.advice.MyAroungAdvice"></bean>
<bean id="custormServiceTarget" class="cn.csdn.aop.service.CustormServiceImpl"></bean>
<!-- 配置代理对象 -->
<bean id="custormServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 指明你的代理接口 -->
<property name="proxyInterfaces">
<list>
<value>cn.csdn.aop.service.CustormService</value>
</list>
</property>
<!-- 指明拦截器的名称 -->
<property name="interceptorNames">
<list>
<value>myAroungAdvice</value>
</list>
</property>
<!-- 指明目标对象 -->
<property name="target">
<ref bean="custormServiceTarget"/>
</property>

</bean>


5.创建一个测试类App.java

package cn.csdn.aop.service;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class App {
@Test
public void app(){
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:a*.xml");
CustormService d= (CustormService) ac.getBean("custormServiceProxy");
d.register("zhang", "670109809", "12345");
}

}


输出结果:
around完成----登录到网站的操作。。。
---------用户在这里实现注册的功能..........
around完成----提示你注册成功。。。

你可能感兴趣的:(java,AOP,xml,JUnit)