spring aop注解配置代碼實(shí)例
本文實(shí)例為大家分享了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)建容器時(shí)使用哪個(gè)配置文件
@ContextConfiguration("classpath:cn/itcast/e_annotation/applicationContext.xml")
public class Demo {
/*
* @Test public void fun1() { //1 創(chuàng)建容器對(duì)象 ClassPathXmlApplicationContext ac=new
* ClassPathXmlApplicationContext("applicationContext.xml"); //2 向容器“要” user對(duì)象
* User u=(User)ac.getBean("user"); User u2=(User)ac.getBean("user");
*
* System.out.println(u==u2); //3 打印user對(duì)象 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 "> <!-- 準(zhǔn)備工作:導(dǎo)入aop(約束)命名空間 --> <!-- 1. 配置目標(biāo)對(duì)象 --> <bean name="userServiceTarget" class="cn.itcast.service.UserServiceImpl"></bean> <!-- 2. 配置通知對(duì)象 --> <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//表示該類時(shí)一個(gè)通知類
public class MyAdvice {
//前置通知 -》目標(biāo)方法運(yùn)行之前調(diào)用
//后置通知(如果出現(xiàn)異常不會(huì)調(diào)用) -》目標(biāo)方法運(yùn)行之后調(diào)用
//環(huán)繞通知-》在目標(biāo)方法之前和之后都調(diào)用
//異常攔截通知-》如果出現(xiàn)異常,就會(huì)調(diào)用
//后置通知(無(wú)論是否出現(xiàn)異常都會(huì)調(diào)用)-》在目標(biāo)方法運(yùn)行之后調(diào)用
@Pointcut("execution(* cn.itcast.service.*ServiceImpl.*(..))")
public void pc() {}
//前置通知
@Before("MyAdvice.pc()")//指定該方法是前置切點(diǎn)
public void before() {
System.out.println("這是前置通知");
}
//后置通知
public void afterReturning() {
System.out.println("這是后置通知(如果出現(xiàn)異常不會(huì)調(diào)用?。。?);
}
//環(huán)繞通知
public Object around( ProceedingJoinPoint pjp) throws Throwable {
System.out.println("這是環(huán)繞通知之前的部分");
Object procees=pjp.proceed();//調(diào)用目標(biāo)方法
System.out.println("這是環(huán)繞通知之后的部分");
return procees;
}
//異常通知
public void afterException() {
System.out.println("出事了,出現(xiàn)異常了");
}
//后置通知
public void after() {
System.out.println("這是后置通知(出現(xiàn)異常也會(huì)調(diào)用)");
}
}
以上所述是小編給大家介紹的spring aop注解配置詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Java中Dijkstra算法求解最短路徑的實(shí)現(xiàn)
Dijkstra算法是一種解決最短路徑問(wèn)題的常用算法,本文主要介紹了Java中Dijkstra算法求解最短路徑的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09
maven將項(xiàng)目打包上傳到nexus私服的詳細(xì)教程
這篇文章主要介紹了maven將項(xiàng)目打包上傳到nexus私服,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-07-07
Java多線程批量數(shù)據(jù)導(dǎo)入的方法詳解
這篇文章主要介紹了Java多線程批量數(shù)據(jù)導(dǎo)入的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,下面小編和大家來(lái)一起學(xué)習(xí)下吧2019-06-06
JAVA讀取文件流,設(shè)置瀏覽器下載或直接預(yù)覽操作
這篇文章主要介紹了JAVA讀取文件流,設(shè)置瀏覽器下載或直接預(yù)覽操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
基于Java回顧之網(wǎng)絡(luò)通信的應(yīng)用分析
在這篇文章里,我們主要討論如何使用Java實(shí)現(xiàn)網(wǎng)絡(luò)通信,包括TCP通信、UDP通信、多播以及NIO2013-05-05
Spring中@ExceptionHandler注解的工作原理詳解
這篇文章主要介紹了Spring中@ExceptionHandler注解的工作原理詳解,Spring Web注解@ExceptionHandler可以用來(lái)指定處理某類異常的控制器方法,從而在這些異常發(fā)生時(shí),會(huì)有相應(yīng)的控制器方法來(lái)處理此類異常,需要的朋友可以參考下2024-01-01
SpringBoot靜態(tài)視頻實(shí)時(shí)播放的實(shí)現(xiàn)代碼
這篇文章主要介紹了SpringBoot靜態(tài)視頻實(shí)時(shí)播放的實(shí)現(xiàn)代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01

