Spring的AOP開發方式
阿新 • • 發佈:2018-12-17
Spring的AOP中相關術語的介紹
Spring的AOP開發:hello_world
A:(xml方式) 1.匯入AOP開發所需的包 AOP聯盟的jar包:com.springsource.org.aopalliance-1.0.0.jar Spring提供的AOP的jar包:spring-aop-4.2.4.RELEASE.jar AspectJ的jar包:com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar Spring整合AspectJ的jar包:spring-aspects-4.2.4.RELEASE.jar
2.ProductDao介面:
public interface ProductDao {
/**
* 持久層:產品儲存
*/
public void save();
}
ProductDaoImpl實現類:
public class ProductDaoImpl implements ProductDao {
@Override public void save() { System.out.println("持久層:產品儲存..."); } public int delete(){ System.out.println("持久層:產品刪除..."); return 100; } @Override public void update() { System.out.println("持久層:產品更新"); } @Override public void find() { System.out.println("持久層:查詢"); int i = 10/0; }
切面類:
public class MyAspectXml { public void checkPrivilege(JoinPoint point){ System.out.println("許可權校驗..." + point); } public void afterReturn(Object result){ System.out.println("後置通知:" + result); } public Object around(ProceedingJoinPoint joinpoint){ System.out.println("環繞前執行"); Object obj = null; try { obj = joinpoint.proceed(); } catch (Throwable e) { e.printStackTrace(); } System.out.println("環繞後執行"); return obj; } public void afterThrowing(Exception ex){ System.out.println("丟擲異常通知:" + ex.getMessage()); } public void after(){ System.out.println("最終通知"); } }
配置:
<aop:config>
<!-- 配置切入點 -->
<aop:pointcut expression="execution(* cn.icbc.dao.impl.ProductDaoImpl.save(..))" id="pointcut1"/>
<aop:pointcut expression="execution(* cn.icbc.dao.impl.ProductDaoImpl.delete(..))" id="pointcut2"/>
<aop:pointcut expression="execution(* cn.icbc.dao.impl.ProductDaoImpl.update(..))" id="pointcut3"/>
<aop:pointcut expression="execution(* cn.icbc.dao.impl.ProductDaoImpl.find(..))" id="pointcut4"/>
<!-- 配置切面 -->
<aop:aspect ref="myAspectXml">
<!-- 前置通知 -->
<aop:before method="checkPrivilege" pointcut-ref="pointcut1"/>
<!-- 後置通知 -->
<aop:after-returning method="afterReturn" pointcut-ref="pointcut2" returning="result"/>
<!-- 環繞通知 -->
<aop:around method="around" pointcut-ref="pointcut3"/>
<!-- 丟擲異常通知 -->
<aop:after-throwing method="afterThrowing" pointcut-ref="pointcut4" throwing="ex"/>
</aop:aspect>
</aop:config>
B:註解形式的AOP : hello_world 1.引入jar包
2.建立核心配置檔案
<?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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="cn.itcast"></context:component-scan>
<!-- 開啟自動代理註解 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
3.建立介面
package com.icbc.spring.study3_2;
public interface ProductDao {
/**
* 持久層:產品儲存
*/
public void save();
public int delete();
public void update();
public void find();
}
-
實現類
package com.icbc.spring.study3_2;
import org.springframework.stereotype.Repository; @Repository("ProductDao") public class ProductDaoImpl implements ProductDao { @Override public void save() { System.out.println("持久層:產品儲存..."); } public int delete(){ System.out.println("持久層:產品刪除..."); return 100; } @Override public void update() { System.out.println("持久層:產品更新"); } @Override public void find() { System.out.println("持久層:查詢"); }}
5 .編寫切面類
package com.icbc.spring.study3_2;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Component("MyAspect")
@Aspect
public class MyAspect {
@Before("execution(* com.icbc.spring.study3_2.ProductDaoImpl.save(..))")
public void checkPrivilege(JoinPoint joinPoint){
System.out.println("許可權校驗..." + joinPoint.toString());
}
@AfterReturning(value="execution(* com.icbc.spring.study3_2.ProductDaoImpl.delete(..))",returning="result")
public void afterReturning(Object result){
System.out.println("後置通知:" + result);
}
@Around("execution(* com.icbc.spring.study3_2.ProductDaoImpl.update(..))")
public Object after(ProceedingJoinPoint joinpoint) throws Throwable{
System.out.println("環繞通知前增強");
Object obj = joinpoint.proceed();
System.out.println("環繞通知後增強");
return obj;
}}
6.測試:
package com.icbc.spring.study3_2;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestAOP {
@Autowired
private ProductDao productDao;
@Test
public void test1(){
// productDao.save();
// productDao.afterReturning();
// productDao.delete();
productDao.update();
}
}