JAVA 框架-Spring-AOP面向切面
阿新 • • 發佈:2018-06-30
類型 .com fig cto assert onf con app urn
AOP(Aspect Orient Programming),我們一般稱為面向方面(切面)編程,作為面向對象的一種補充,用於處理系統中分布於各個模塊的橫切關註點,比如事務管理、日誌、緩存等等。AOP實現的關鍵在於AOP框架自動創建的AOP代理,AOP代理主要分為靜態代理和動態代理,靜態代理的代表為AspectJ;而動態代理則以Spring AOP為代表。
AOP配置示例:
數據訪問層:
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//將該類自動放到當前類中的成員變量中,默認的裝配類型時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面向切面