JAVA 框架-Spring-AOP面向切面
Posted w944372441
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA 框架-Spring-AOP面向切面相关的知识,希望对你有一定的参考价值。
AOP(Aspect Orient Programming),我们一般称为面向方面(切面)编程,作为面向对象的一种补充,用于处理系统中分布于各个模块的横切关注点,比如事务管理、日志、缓存等等。AOP实现的关键在于AOP框架自动创建的AOP代理,AOP代理主要分为静态代理和动态代理,静态代理的代表为AspectJ;而动态代理则以Spring AOP为代表。
AOP配置示例:
需要的包:aspectjweaver-1.8.4.jar
数据访问层:
package com.hanqi.dao; public class AppuserDao { public int deleteUser(int ids) { System.out.println(ids +"被删除"); yichang();//抛出一个异常 return 1; } public void yichang() { throw new RuntimeException("出现错误!"); } }
切面代理层:
package com.hanqi.util; public class LogginProxy { public void beforeMethod() { System.out.println("方法之前被调用!"); } public void afterMethod() { System.out.println("方法之后被调用!"); } public void returnMethod() { System.out.println("返回结果时被调用!"); } public void throwMethod() { System.out.println("抛出异常时被调用!"); } /*public void aroundMethod() { System.out.println("方法被调用"); }*/ }
spring.xml配置AOP
?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" xmlns:context="http://www.springframework.org/schema/context" 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-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <bean id="logginProxy" class="com.hanqi.util.LogginProxy"></bean> <bean id="appuserdao" class="com.hanqi.dao.AppuserDao"></bean> <aop:config> <!-- 配置切点 --> <aop:pointcut expression="execution(* com.hanqi.dao.*.*(..))" id="aoppointcut" /><!--execution:执行,这句意为在执行com.hanqi.dao下的所有类时都会执行切面类--> <!-- 指明切面类 --> <aop:aspect ref="logginProxy"> <aop:before method="beforeMethod" pointcut-ref="aoppointcut"/> <aop:after method="afterMethod" pointcut-ref="aoppointcut"/> <aop:after-returning method="returnMethod" pointcut-ref="aoppointcut"/> <aop:after-throwing method="throwMethod" pointcut-ref="aoppointcut"/> </aop:aspect> </aop:config> </beans>
JUnit Test测试:
package com.hanqi.util; import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.hanqi.dao.AppuserDao; class JUnittest { private ClassPathXmlApplicationContext c; private AppuserDao appuserdao; @BeforeEach void setUp() throws Exception { c = new ClassPathXmlApplicationContext("spring.xml"); appuserdao = c.getBean(AppuserDao.class); } @AfterEach void tearDown() throws Exception { c.close(); } @Test void test() { appuserdao.deleteUser(55); } }
打印结果:
使用注解配置AOP
spring.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" xmlns:context="http://www.springframework.org/schema/context" 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-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <context:component-scan base-package="com.hanqi"></context:component-scan><!--配置扫描器--> </beans>
数据访问层:
package com.hanqi.dao;
import org.springframework.stereotype.Repository;
@Repository//加到spring容器中,id名默认为类名首字母小写
public class AppuserDao {
public int deleteUser(int ids) {
System.out.println(ids +"被删除");
yichang();
return 1;
}
public void yichang() {
throw new RuntimeException("出现错误!");
}
}
AppuserService类:
package com.hanqi.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Repository; import com.hanqi.dao.AppuserDao; @Repository public class AppuserService { @Autowired//从spring容器中将该类自动放到当前类中的成员变量中,默认的装配类型时byType //@Qualifier("dao")该注解可将bean中id="dao"的类装配到该成员变量中,用于区分当类型相同时; private AppuserDao appuserDao; public int deleteUser(int ids) { return appuserDao.deleteUser(ids); } }
代理类:
package com.hanqi.util; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.context.annotation.EnableAspectJAutoProxy; import org.springframework.stereotype.Repository; @Repository//加到spring容器中,id名为类名首字母小写 @Aspect//声明当前类为切面类 @EnableAspectJAutoProxy//自动代理 public class LogginProxy { @Before("execution(* com.hanqi.dao.*.*(..))")//声明切点 public void beforeMethod() { System.out.println("方法之前被调用!"); } @After("execution(* com.hanqi.dao.*.*(..))") public void afterMethod() { System.out.println("方法之后被调用!"); } @AfterReturning("execution(* com.hanqi.dao.*.*(..))") public void returnMethod() { System.out.println("返回结果时被调用!"); } @AfterThrowing("execution(* com.hanqi.dao.*.*(..))") public void throwMethod() { System.out.println("抛出异常时被调用!"); } /*public void aroundMethod() { System.out.println("方法被调用"); }*/ }
测试类:
package com.hanqi.util; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.hanqi.dao.AppuserDao; import com.hanqi.service.AppuserService; class JUnittest { private ClassPathXmlApplicationContext c; private AppuserDao appuserdao; private AppuserService appuserService; @BeforeEach void setUp() throws Exception { c = new ClassPathXmlApplicationContext("spring.xml"); //appuserdao = c.getBean(AppuserDao.class); appuserService = c.getBean(AppuserService.class); } @AfterEach void tearDown() throws Exception { c.close(); } @Test void test() { //appuserdao.deleteUser(55); appuserService.deleteUser(66); } }
以上是关于JAVA 框架-Spring-AOP面向切面的主要内容,如果未能解决你的问题,请参考以下文章