一,用xml的方式配置AOP中的切面、切入点以及各种通知。
1,首先贴出测试代码的架构
1,首先新建一个bean:ScoreServiceImpl
package cn.china.zhen;
/**
* 业务bean
*/
public class ScoreServiceImpl implements ScoreService {
@Override
public void save(){
System.out.println("ScoreServiceImpl...save().....");
}
@Override
public String getScore(String scoreId){
return "score..98";
}
}
2,新建一个log的常规java类作为切面
package cn.china.zhen;
import org.aspectj.lang.ProceedingJoinPoint;
/**
* 测试xml配置的方式实现AOP
*
* 此切面类就是个普通的java类
*/
public class LogInterceptor2 {
public void before(){
System.out.println("前置通知");
}
public void afterReutrning(){
System.out.println("后置通知");
}
public void throwExption(){
System.out.println("throwExption通知");
}
public void after(){
System.out.println("最终通知");
}
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("环绕开始");
Object result = pjp.proceed();//此句为固定
System.out.println("环绕结束");
return result;
}
}
3,最后在beans2.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<aop:aspectj-autoproxy/>
<!-- 切面类和业务bean -->
<bean id="scoreService" class="cn.china.zhen.ScoreServiceImpl"/>
<bean id="logInterceptor2" class="cn.china.zhen.LogInterceptor2"/>
<aop:config>
<aop:aspect id="myAspect" ref="logInterceptor2">
<aop:pointcut id="mycut" expression="execution (* cn.china.zhen.ScoreServiceImpl.*(..))"/>
<aop:before pointcut-ref="mycut" method="before"/>
<aop:after-returning pointcut-ref="mycut" method="afterReutrning"/>
<aop:after-throwing pointcut-ref="mycut" method="throwExption"/>
<aop:after pointcut-ref="mycut" method="after"/>
<aop:around pointcut-ref="mycut" method="around"/>
</aop:aspect>
</aop:config>
</beans>
4,最后看看执行的结果
package cn.china.zhen.test;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.china.zhen.ScoreService;
public class AspectTest2 {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test
public void test() {
ApplicationContext context = new ClassPathXmlApplicationContext("beans2.xml");
ScoreService score = (ScoreService) context.getBean("scoreService");
score.save();
}
}
执行的结果:
5,选择性的增加通知
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<aop:aspectj-autoproxy/>
<!-- 切面类和业务bean -->
<bean id="scoreService" class="cn.china.zhen.ScoreServiceImpl"/>
<bean id="logInterceptor2" class="cn.china.zhen.LogInterceptor2"/>
<aop:config>
<aop:aspect id="myAspect" ref="logInterceptor2">
<aop:pointcut id="mycut" expression="execution <span style="color:#ff0000;">(java.lang.String cn.china.zhen.ScoreServiceImpl.*(java.lang.String,..))</span>"/>
<aop:before pointcut-ref="mycut" method="before"/>
<aop:after-returning pointcut-ref="mycut" method="afterReutrning"/>
<aop:after-throwing pointcut-ref="mycut" method="throwExption"/>
<aop:after pointcut-ref="mycut" method="after"/>
<aop:around pointcut-ref="mycut" method="around"/>
</aop:aspect>
</aop:config>
</beans>