本文實(shí)例為大家分享了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)建容器時(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
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 " > <!-- 準(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
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 //表示該類時(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)用 //后置通知(無論是否出現(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ì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!