當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
JAVA 框架-Spring-AOP面向切面
生活随笔
收集整理的這篇文章主要介紹了
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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://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:pointcutexpression="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;@BeforeEachvoid setUp() throws Exception {c = new ClassPathXmlApplicationContext("spring.xml");appuserdao = c.getBean(AppuserDao.class);}@AfterEachvoid tearDown() throws Exception {c.close();}@Testvoid 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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://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;@BeforeEachvoid setUp() throws Exception {c = new ClassPathXmlApplicationContext("spring.xml");//appuserdao = c.getBean(AppuserDao.class);appuserService = c.getBean(AppuserService.class);}@AfterEachvoid tearDown() throws Exception {c.close();}@Testvoid test() {//appuserdao.deleteUser(55);appuserService.deleteUser(66);}}?
轉載于:https://www.cnblogs.com/wyc1991/p/9245908.html
總結
以上是生活随笔為你收集整理的JAVA 框架-Spring-AOP面向切面的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【FBI WARNING】一些Noip的
- 下一篇: Spring Cloud Sleuth