不知道从何时开始,流行用注解形式来做配置,要去XML,其实怎么配置都差多!
xml
首先来看Spring的bean xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--扫描所有component、service、repository,注册为bean-->
<context:component-scan base-package="org.lanqiao.springDemo.aop"/>
<!-- 定义扫描@aspectj -->
<aop:aspectj-autoproxy/>
</beans>
功能看注释就可以了解,下面先定义一个新的可以被扫描的业务bean:
业务bean
package org.lanqiao.springDemo.aop;
import org.springframework.stereotype.Service;
@Service
public class NoBiz implements SomeBiz {
public void addBiz() {
System.out.println("NoBiz------新增数据的业务逻辑");
}
public void other() {
System.out.println("NoBiz---------其他方法");
}
}
注意@Service,这个注解配合XML中声明的<context:component-scan
就可以完成bean的声明。
业务bean相比之前无其他特点。
注解+切面
然后是注解配置的切面,Advice2:
package org.lanqiao.springDemo.aop;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class Advice2 {
@Pointcut("execution(* org.lanqiao..*Biz*.*Biz(..))")
public void bizPointcut(){}
@Before("bizPointcut()")
public void invoke(JoinPoint joinPoint) throws Throwable {
System.out.printf("代理逻辑发生在调用方法%s之前\n", joinPoint.toLongString());
}
}
其中@Component是标注该类可以被spring的bean扫描器扫到;
@Aspect标注这是一个切面;
@Pointcut声明了一个切入点表达式;
@Before声明了其下的方法是一个前置通知的逻辑;方法参数一个连接点数据的封装——JoinPoint对象。
测试
测试代码:
package org.lanqiao.springDemo.aop;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import static org.junit.Assert.*;
@ContextConfiguration("classpath:aop3.xml")
public class NoBizTest extends AbstractJUnit4SpringContextTests{
@Autowired
private SomeBiz noBiz;
@Test
public void addBiz() throws Exception {
noBiz.addBiz();
}
@Test
public void other() throws Exception {
}
}
基于类的代理
如果NoBiz并未实现某种接口,那我们就要启动基于类的字节码代理,而不是用JDK的动态代理。
<!--开启基于类的代理-->
<aop:aspectj-autoproxy proxy-target-class="true"/>