springaop注解配置代碼實(shí)例

本文實(shí)例為大家分享了spring aop注解配置的具體代碼,供大家參考,具體內(nèi)容如下

創(chuàng)新互聯(lián)-專(zhuān)業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性?xún)r(jià)比江干網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式江干網(wǎng)站制作公司更省心,省錢(qián),快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋江干地區(qū)。費(fèi)用合理售后完善,十年實(shí)體公司更值得信賴(là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ì)創(chuàng)新互聯(lián)網(wǎng)站的支持!

本文名稱(chēng):springaop注解配置代碼實(shí)例
當(dāng)前地址:http://muchs.cn/article42/ijcchc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、網(wǎng)站設(shè)計(jì)公司定制網(wǎng)站、做網(wǎng)站App設(shè)計(jì)、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

綿陽(yáng)服務(wù)器托管