1.接口和类

aspectj xml-LMLPHP

1.1 ISomeService 接口

public interface ISomeService {

    public void doSome();

    public void dade();
}

1.2  SomeService类

public class SomeService implements ISomeService {
//核心业务
public void doSome(){
System.out.println("我们都要找到Java开发工作,薪资6,7,8,9,10K");
} public void dade() {
//异常
//int sun=5/0;
System.out.println("++++++++++++++de+++++++++++++");
} }

1.3  MyAspect 类

public class MyAspect {

    //前置增强
public void before(){
System.out.println("=====before========");
}
//后置增强
public void afterReturing(){
System.out.println("=====after========");
}
//后置增强 目标方法的返回值
public void afterReturing(String result){
System.out.println("=====后置通知方法 result========"+result);
}
//环绕通知
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("目标方法执行前执行");
Object result = pjp.proceed();
System.out.println("目标方法执行后执行");
String temp=null;
if (result!=null) {
temp = (String) result;
temp=temp.toUpperCase();
}
return temp;
}
//异常通知
public void afterThrowing(){
System.out.println("异常通知");
}
//异常通知
public void afterThrowing(Exception ex){
System.out.println("异常通知 ex="+ex.getMessage());
} //最终通知
public void after(){
System.out.println("最终通知");
} }

2.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
<!--.目标对象-->
<bean id="someService" class="cn.bdqn.spring18.SomeService"></bean> <!--.增强类-->
<bean id="aspect" class="cn.bdqn.spring18.MyAspect"></bean> <!--aop-->
<aop:config>
<!--设置一个切点-->
<aop:pointcut id="mycut" expression="execution(* *..spring18.*.*(..))"></aop:pointcut>
<aop:aspect ref="aspect">
<aop:before method="before" pointcut-ref="mycut"></aop:before>
<aop:after-returning method="afterReturing" pointcut-ref="mycut"></aop:after-returning>
<aop:after-returning method="afterReturing(java.lang.String)" returning="result" pointcut-ref="mycut"></aop:after-returning>
<aop:around method="around" pointcut-ref="mycut"></aop:around>
<aop:after-throwing method="afterThrowing" pointcut-ref="mycut"></aop:after-throwing>
<aop:after-throwing method="afterThrowing(java.lang.Exception)" throwing="ex" pointcut-ref="mycut"></aop:after-throwing>
<aop:after method="after" pointcut-ref="mycut"></aop:after>
</aop:aspect>
</aop:config>
</beans>

3.测试类

 //aspectj xml
@Test
public void test20(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextspring16.xml");
ISomeService service = (ISomeService) ctx.getBean("someService");
service.doSome();
service.dade();
}
05-11 19:21