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. 開(kāi)啟使用注解完成植入 --> <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;
//通知類(lèi)
@Aspect//表示該類(lèi)時(shí)一個(gè)通知類(lèi)
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)文章
Spring使用Redis限制用戶(hù)登錄失敗的次數(shù)及暫時(shí)鎖定用戶(hù)登錄權(quán)限功能
這篇文章主要介紹了Spring使用Redis限制用戶(hù)登錄失敗的次數(shù)及暫時(shí)鎖定用戶(hù)登錄權(quán)限功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-02-02
基于Spring Boot保護(hù)Web應(yīng)用程序
這篇文章主要介紹了基于Spring Boot保護(hù)Web應(yīng)用程序,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
RabbitMq中channel接口的幾種常用參數(shù)詳解
這篇文章主要介紹了RabbitMq中channel接口的幾種常用參數(shù)詳解,RabbitMQ 不會(huì)為未確認(rèn)的消息設(shè)置過(guò)期時(shí)間,它判斷此消息是否需要重新投遞給消費(fèi)者的唯一依據(jù)是消費(fèi)該消息的消費(fèi)者連接是否己經(jīng)斷開(kāi),需要的朋友可以參考下2023-08-08
Servlet+JDBC實(shí)現(xiàn)登陸功能的小例子(帶驗(yàn)證碼)
這篇文章主要介紹了Servlet+JDBC實(shí)現(xiàn)登陸功能的小例子(帶驗(yàn)證碼),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
idea2020導(dǎo)入spring5.1的源碼詳細(xì)教程
這篇文章主要介紹了idea2020導(dǎo)入spring5.1的源碼的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
MVC頁(yè)面之間參數(shù)傳遞實(shí)現(xiàn)過(guò)程圖解
這篇文章主要介紹了MVC頁(yè)面之間參數(shù)傳遞實(shí)現(xiàn)過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
idea同時(shí)打開(kāi)多個(gè)項(xiàng)目的圖文教程
這篇文章主要給大家介紹了idea同時(shí)打開(kāi)多個(gè)項(xiàng)目的圖文教程,文章通過(guò)圖文結(jié)合的形式給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-02-02

