spring aop注解配置代碼實例
更新時間:2019年04月03日 15:21:54 作者:weixin_43878297
這篇文章主要介紹了spring aop注解配置代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
本文實例為大家分享了spring aop注解配置的具體代碼,供大家參考,具體內(nèi)容如下
Demo.java
package cn.itcast.e_annotation; import javax.annotation.Resource; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import cn.itcast.bean.User; import cn.itcast.service.ISUserService; @RunWith(SpringJUnit4ClassRunner.class)//幫我們創(chuàng)建容器 //指定創(chuàng)建容器時使用哪個配置文件 @ContextConfiguration("classpath:cn/itcast/e_annotation/applicationContext.xml") public class Demo { /* * @Test public void fun1() { //1 創(chuàng)建容器對象 ClassPathXmlApplicationContext ac=new * ClassPathXmlApplicationContext("applicationContext.xml"); //2 向容器“要” user對象 * User u=(User)ac.getBean("user"); User u2=(User)ac.getBean("user"); * * System.out.println(u==u2); //3 打印user對象 System.out.println(u); * * ac.close(); } */ @Resource(name="userServiceTarget") private ISUserService us; @Test public void fun1() { us.save(); } }
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd "> <!-- 準備工作:導(dǎo)入aop(約束)命名空間 --> <!-- 1. 配置目標對象 --> <bean name="userServiceTarget" class="cn.itcast.service.UserServiceImpl"></bean> <!-- 2. 配置通知對象 --> <bean name="myAdvice" class="cn.itcast.e_annotation.MyAdvice"></bean> <!-- 3. 開啟使用注解完成植入 --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
MyAdvice.java
package cn.itcast.e_annotation; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; //通知類 @Aspect//表示該類時一個通知類 public class MyAdvice { //前置通知 -》目標方法運行之前調(diào)用 //后置通知(如果出現(xiàn)異常不會調(diào)用) -》目標方法運行之后調(diào)用 //環(huán)繞通知-》在目標方法之前和之后都調(diào)用 //異常攔截通知-》如果出現(xiàn)異常,就會調(diào)用 //后置通知(無論是否出現(xiàn)異常都會調(diào)用)-》在目標方法運行之后調(diào)用 @Pointcut("execution(* cn.itcast.service.*ServiceImpl.*(..))") public void pc() {} //前置通知 @Before("MyAdvice.pc()")//指定該方法是前置切點 public void before() { System.out.println("這是前置通知"); } //后置通知 public void afterReturning() { System.out.println("這是后置通知(如果出現(xiàn)異常不會調(diào)用?。。?); } //環(huán)繞通知 public Object around( ProceedingJoinPoint pjp) throws Throwable { System.out.println("這是環(huán)繞通知之前的部分"); Object procees=pjp.proceed();//調(diào)用目標方法 System.out.println("這是環(huán)繞通知之后的部分"); return procees; } //異常通知 public void afterException() { System.out.println("出事了,出現(xiàn)異常了"); } //后置通知 public void after() { System.out.println("這是后置通知(出現(xiàn)異常也會調(diào)用)"); } }
以上所述是小編給大家介紹的spring aop注解配置詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Spring使用Redis限制用戶登錄失敗的次數(shù)及暫時鎖定用戶登錄權(quán)限功能
這篇文章主要介紹了Spring使用Redis限制用戶登錄失敗的次數(shù)及暫時鎖定用戶登錄權(quán)限功能,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2024-02-02RabbitMq中channel接口的幾種常用參數(shù)詳解
這篇文章主要介紹了RabbitMq中channel接口的幾種常用參數(shù)詳解,RabbitMQ 不會為未確認的消息設(shè)置過期時間,它判斷此消息是否需要重新投遞給消費者的唯一依據(jù)是消費該消息的消費者連接是否己經(jīng)斷開,需要的朋友可以參考下2023-08-08Servlet+JDBC實現(xiàn)登陸功能的小例子(帶驗證碼)
這篇文章主要介紹了Servlet+JDBC實現(xiàn)登陸功能的小例子(帶驗證碼),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06idea2020導(dǎo)入spring5.1的源碼詳細教程
這篇文章主要介紹了idea2020導(dǎo)入spring5.1的源碼的方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06