SpringAOP
定义:面向切面编程。本人的理解就是,给代码包装,这个包装可以在方法执行前面,也可在方法执行后面。
有了这样的方法就可以做一些操作,方法执行前或后的判断。下面是我配置的两种SpringAOP:
一、XML配置
dao层接口代码:
public interface StudentDao {
String getInfo(String name);
}
dao层实现类:
@Component
public class StudentDaoImpl implements StudentDao{
@Override
public String getInfo(String name){
System.out.println("-----getInfo----");
// int i =1/0;
// return i;
return "11111";
}
}
切面代码块:
public class AspectJ {
public void before(String name){
System.out.println("方法执行前:"+name);
}
public void after(String name){
System.out.println("方法执行后:"+name);
}
public void afterReturning(String name,String msg){
System.out.println("方法执行后返回:"+name+",返回值为:"+msg);
}
public void afterReturnThrowable(String name,Throwable throwable){
System.out.println("方法执行后返回:"+name+",返回异常为:"+throwable);
}
public void round(ProceedingJoinPoint joinPoint){
String name = (String)joinPoint.getArgs()[0];
try {
System.out.println("方法执行前:"+name);
joinPoint.proceed();
System.out.println("方法执行后:"+name);
} catch (Throwable throwable) {
throwable.printStackTrace();
System.out.println("方法执行后抛出异常:"+throwable);
}
}
}
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: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/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="studentDao" class="com.speark.demo.test.SpringAOP.Dao.StudentDaoImpl"/>
<bean id="aspectJ" class="com.speark.demo.test.SpringAOP.AOP.AspectJ"/>
<!--aop配置-->
<aop:config>
<!--切面配置-->
<aop:aspect id="student" ref="aspectJ">
<!--切点配置-->
<aop:pointcut id="studentAOP" expression="execution(* com.speark.demo.test.SpringAOP.Dao.StudentDaoImpl.*(..)) and args(name)"/>
<!--前置(方法执行前)-->
<aop:before method="before" arg-names="name" pointcut-ref="studentAOP"/>
<!-- -->
<!--后置(方法执行后)-->
<aop:after method="after" arg-names="name" pointcut-ref="studentAOP"/>
<!-- -->
<!--后置返回(方法执行后返回)-->
<aop:after-returning method="afterReturning" arg-names="name,msg" pointcut-ref="studentAOP" returning="msg"/>
<!--后置抛出异常(方法执行后抛出异常)-->
<aop:after-throwing method="afterReturnThrowable" pointcut-ref="studentAOP" arg-names="name,throwable" throwing="throwable"/>
<!--环绕-->
<!-- <aop:around method="round" pointcut-ref="studentAOP" />-->
</aop:aspect>
</aop:config>
</beans>
测试类:
public class AopTest {
@Test
public void test(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("SpringAOP.xml");
StudentDao studentDao = (StudentDao)context.getBean("studentDao");
studentDao.getInfo("你好");
}
}
测试结果:
二、注解配置
切面代码块:
@Aspect
@Component
public class AspectJ {
@Pointcut(value = "execution(* com.speark.demo.test.SpringAOP.Dao.StudentDaoImpl.getInfo(..)) && args(name)",argNames = "name")
public void geDemo(String name){
}
@Before("geDemo(name)")
public void before(String name){
System.out.println("方法执行前:"+name);
}
@After("geDemo(name)")
public void after(String name){
System.out.println("方法执行后:"+name);
}
@AfterReturning(value = "geDemo(name)",returning = "msg")
public void afterReturning(String name,String msg){
System.out.println("方法执行后返回:"+name+",返回值为:"+msg);
}
public void afterReturnThrowable(String name,Throwable throwable){
System.out.println("方法执行后返回:"+name+",返回异常为:"+throwable);
}
public void round(ProceedingJoinPoint joinPoint){
String name = (String)joinPoint.getArgs()[0];
try {
System.out.println("方法执行前:"+name);
joinPoint.proceed();
System.out.println("方法执行后:"+name);
} catch (Throwable throwable) {
throwable.printStackTrace();
System.out.println("方法执行后抛出异常:"+throwable);
}
}
}
代理类:
@Configuration
@EnableAspectJAutoProxy//开启自动代理默认jdk代理
@ComponentScan(basePackages = "com.speark.demo.test.SpringAOP")//自动扫描注解
public class AopConfig {
}
测试类代码:
public class AopTest {
@Test
public void AnnotationAopTest(){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AopConfig.class);
StudentDao studentDao = (StudentDao)context.getBean("studentDaoImpl");
studentDao.getInfo("123");
}
}
测试结果:
个人总结:
AOP添加判读是否准许执行方法有二种方式:
1、在方法执行前,即before方法,添加判断如果成立执行否则将其作为异常抛出:
2、在around方法,添加判断.ProceedingJoinPoint中有方法proceed(),此方法为执行UserDao类中的方法。如果不写则不会执行UserDao的方法。