本文實例為大家分享了spring aop注解配置的具體代碼,供大家參考,具體內(nèi)容如下
demo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
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
1
2
3
4
5
6
7
8
9
10
|
<?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 " > <!-- 準備工作:導入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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
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注解配置詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網(wǎng)站的支持!