好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

spring aop注解配置代码实例

本文实例为大家分享了 spring aop注解配置的具体代码,供大家参考,具体内容如下

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 ) //帮我们创建容器

//指定创建容器时使用哪个配置文件

@contextconfiguration ( "classpath:cn/itcast/e_annotation/applicationcontext.xml" )

public class demo {

     /*

      * @test public void fun1() { //1 创建容器对象 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://HdhCmsTestw3.org/2001/xmlschema-instance" xmlns= "http://HdhCmsTestspringframework.org/schema/beans" xmlns:context= "http://HdhCmsTestspringframework.org/schema/context" xmlns:aop= "http://HdhCmsTestspringframework.org/schema/aop" xsi:schemalocation= "http://HdhCmsTestspringframework.org/schema/beans http://HdhCmsTestspringframework.org/schema/beans/spring-beans-4.2.xsd http://HdhCmsTestspringframework.org/schema/context http://HdhCmsTestspringframework.org/schema/context/spring-context-4.2.xsd http://HdhCmsTestspringframework.org/schema/aop http://HdhCmsTestspringframework.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 {

     //前置通知 -》目标方法运行之前调用

     //后置通知(如果出现异常不会调用) -》目标方法运行之后调用

     //环绕通知-》在目标方法之前和之后都调用

     //异常拦截通知-》如果出现异常,就会调用

     //后置通知(无论是否出现异常都会调用)-》在目标方法运行之后调用

     @pointcut ( "execution(* cn.itcast.service.*serviceimpl.*(..))" )

     public void pc() {}

    

     //前置通知

     @before ( "myadvice.pc()" ) //指定该方法是前置切点

     public void before() {

         system.out.println( "这是前置通知" );

     }

     //后置通知

     public void afterreturning() {

         system.out.println( "这是后置通知(如果出现异常不会调用!!)" );

     }

     //环绕通知

     public object around( proceedingjoinpoint pjp) throws throwable {

         system.out.println( "这是环绕通知之前的部分" );

         object procees=pjp.proceed(); //调用目标方法

         system.out.println( "这是环绕通知之后的部分" );

         return procees;

     }

     //异常通知

     public void afterexception() {

         system.out.println( "出事了,出现异常了" );

     }

     //后置通知

     public void after() {

         system.out.println( "这是后置通知(出现异常也会调用)" );

     }

}

以上所述是小编给大家介绍的spring aop注解配置详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

查看更多关于spring aop注解配置代码实例的详细内容...

  阅读:14次