七、AOP註解開發
阿新 • • 發佈:2018-11-24
1.AOP註解形式
- 宣告GoodsDao介面和GoodsDaoImpl實現類:
//介面
package com.itlike.demo1;
public interface GoodsDao {
public void save();
}
//實現類
package com.itlike.demo1;
public class GoodsDaoImpl implements GoodsDao {
@Override
public void save() {
System.out.println("儲存操作");
}
}
- 編寫切面類:
package com.itlike.demo1;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class GoodsDaoAspect {
/*這裡可以使用 GoodsDaoImpl.*(..)表示配置所有方法的切入點 */
@Before(value = "execution(* com.itlike.demo1.GoodsDaoImpl.save(..))")
public void log(){
System.out.println("日誌記錄");
}
}
- 配置檔案:
<?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
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--AOP開啟註解-->
<aop:aspectj-autoproxy/>
<!--使用AOP註解需要託管的類-->
<bean id="goodsDao" class="com.itlike.demo1.GoodsDaoImpl"/>
<bean id="goodsDaoAspect" class="com.itlike.demo1.GoodsDaoAspect"/>
</beans>
- 編寫測試類:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class GoodsTest {
@Resource(name="goodsDao")
private GoodsDao goodsDao;
@Test
public void test(){
/*這裡可以呼叫goodsDao物件的多個方法,如果切面類配置了所有方法,則所有的方法都會被代理*/
goodsDao.save();
}
}
//日誌記錄
//儲存操作
2.通知型別註解形式
- @Before前置通知 如上,也如下:
- @AfterReturning後置通知
沒有返回值
- 宣告Dao介面和實現類:
//介面
public interface GoodsDao {
public void save();
}
//實現類
package com.itlike.demo1;
public class GoodsDaoImpl implements GoodsDao {
@Override
public void save() {
System.out.println("儲存操作");
}
}
- 編寫切面類:
@Aspect
public class GoodsDaoAspect {
@Before(value = "execution(* com.itlike.demo1.GoodsDaoImpl.save(..))")
public void log(){
System.out.println("前置通知");
}
@AfterReturning(value = "execution(* com.itlike.demo1.GoodsDaoImpl.save(..))")
public void afterreturning(){
System.out.println("後置通知");
}
}
- 配置檔案:
<?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
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--AOP開啟註解-->
<aop:aspectj-autoproxy/>
<bean id="goodsDao" class="com.itlike.demo1.GoodsDaoImpl"/>
<bean id="goodsDaoAspect" class="com.itlike.demo1.GoodsDaoAspect"/>
</beans>
- 測試類:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class GoodsTest {
@Resource(name="goodsDao")
private GoodsDao goodsDao;
@Test
public void test(){
goodsDao.save();
}
}
//前置通知
//儲存操作
//後置通知
有返回值
5. 修改介面和類:
//介面
package com.itlike.demo1;
public interface GoodsDao {
public String save();
}
//實現類
package com.itlike.demo1;
public class GoodsDaoImpl implements GoodsDao {
@Override
public String save() {
System.out.println("儲存操作");
return "success";
}
}
- 切面類:
@Aspect
public class GoodsDaoAspect {
/*有返回值時,使用returning接受,且需要在切面方法接收引數,命名一致*/
@AfterReturning(value = "execution(* com.itlike.demo1.GoodsDaoImpl.save(..))",returning = "res")
public void afterreturning(Object res){
System.out.println("後置通知"+res);
}
}
- 配置檔案,測試類不變,執行結果為:
//儲存操作
//後置通知success
- @Around環繞通知
- 宣告Dao介面和實現類:
//介面
public interface GoodsDao {
public void delete();
}
//實現類
package com.itlike.demo1;
public class GoodsDaoImpl implements GoodsDao {
@Override
public void delete() {
System.out.println("刪除操作");
}
}
- 編寫切面類:
@Aspect
public class GoodsDaoAspect {
@Around(value = "execution(* com.itlike.demo1.GoodsDaoImpl.delete(..))")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("環繞前通知");
Object proceed = joinPoint.proceed();
System.out.println("環繞後通知");
return proceed;
}
}
- 配置檔案:
(不變)
<?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
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--AOP開啟註解-->
<aop:aspectj-autoproxy/>
<bean id="goodsDao" class="com.itlike.demo1.GoodsDaoImpl"/>
<bean id="goodsDaoAspect" class="com.itlike.demo1.GoodsDaoAspect"/>
</beans>
- 測試類:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class GoodsTest {
@Resource(name="goodsDao")
private GoodsDao goodsDao;
@Test
public void test(){
goodsDao.delete();
}
}
//環繞前通知
//刪除操作
//環繞後通知
- @AfterThrowing異常丟擲通知
- @After最終通知
3.定義切入點
- 有時候一個方法要新增前置通知對又要新增異常通知,又要新增最終通知
- 可以在切面當中定義好切入點
- 在通知當中直接使用定義好的切入點表示式
- 可以定義多個切入點(一個通知同時定義到多個方法當中)
4.當使用介面時與不使用介面內部代理區別
- 使用介面(JDK動態代理)
- 不使用介面(Cglib動態代理)