您的位置:首页 > 编程语言 > Java开发

Spring AOP

2015-12-19 14:36 741 查看

准备Spring AOP需要的jar包



@Before

Mymother.java:

package com.mrw.springtest9;

public class Mymother {

public void back(){

System.out.println("好好准备!");

}

}


Relation.java

package com.mrw.springtest9;

import org.aspectj.lang.annotation.After;

import org.aspectj.lang.annotation.AfterReturning;

import org.aspectj.lang.annotation.AfterThrowing;

import org.aspectj.lang.annotation.Around;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

@Aspect

public class Relation {

@Before("execution(* com.mrw.springtest9.Mymother.back())")//当调用Mymother的back()时,在它之前执行want()方法

public void want(){

System.out.println("我想做事!");

}

}


将Mymother.java与及Relation.java装入beans.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:aop="http://www.springframework.org/schema/aop"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">

<aop:aspectj-autoproxy/>//必须这样写Spring才能扫描到AOP的注解

<bean id="Mymother" class="com.mrw.springtest9.Mymother"></bean>

<bean id="Relation" class="com.mrw.springtest9.Relation"></bean>

</beans>


建立测试类Test1.java进行测试

import org.junit.Test;

import org.springframework.context.ConfigurableApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test1 {

@Test

public void test() {

ConfigurableApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");

Mymother mymother=(Mymother)context.getBean("Mymother");

System.out.println("---------------------");

mymother.back();

System.out.println("---------------------");

}

}


输出测试结果:



@After

将第一个示例中的其它不变,Relation.java中的@Before替换为:

@After("execution(* com.mrw.springtest9.Mymother.back())")

public void dosomething(){

System.out.println("我要开始做事啦!");

}


输出结果为:



@AfterReturning

将第一个示例中的进行改变,Relation.java中的@Before替换为:

@AfterReturning(value="execution(** com.mrw.springtest9.Mymother.caclculate(..))",returning="var") //caclculate()方法有返回值时,下面的returnvalue就打印出了返回值

public void returnvalue(Object var){

System.out.println("返回的值为:"+var);

}


Mymother中添加如下方法:

public double caclculate(int x,int y){

return x/y;

}


改变Test1.java改变为:

package com.mrw.springtest9;

import static org.junit.Assert.*;

import org.junit.Test;

import org.springframework.context.ConfigurableApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test1 {

@Test

public void test() {

ConfigurableApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");

Mymother mymother=(Mymother)context.getBean("Mymother");

System.out.println("---------------------");

mymother.caclculate(10, 5);

System.out.println("---------------------");

}

}


输出的结果为:



@Around

往第一个示例中Mymother.java添加

public void life(){

System.out.println("人生必须活得精彩!");

}



,Relation.java中的@Before替换为:

@Around("execution(** com.mrw.springtest9.Mymother.must())")

public void eat(){

System.out.println("吃饭!");

}


改变Test1.java为:

ConfigurableApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");

Mymother mymother=(Mymother)context.getBean("Mymother");

System.out.println("---------------------");

mymother.must();

System.out.println("---------------------");


输出结果为:



@After注解的方法就是在一个方法执行完之后执行



@After注解的方法就是在一个方法抛出异常时执行

@Pointcut

当要注解的方法进行重复时,可采用该方法

MyPoint.java

package com.mrw.springtest9;

import org.aspectj.lang.annotation.AfterReturning;

import org.aspectj.lang.annotation.Around;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.aspectj.lang.annotation.Pointcut;

@Aspect

public class MyPointcut {

@Pointcut("execution (** com.mrw.springtest9.Mymother.*(..))")

public void selectMymother(){};//这里的方法必须为空,最好别带参数啦

@Before("selectMymother()") //相当于@Before((** com.mrw.springtest9.Mymother.*(..))"))

public void beforeMymother(){

System.out.println("在所有方法之前执行。。。");

}

@AfterReturning(pointcut="selectMymother()",returning="val") //相当于@AfterReturning(value="execution (** com.mrw.springtest9.Mymother.*(..))",returning="var")

public void afterReturnMymother(Object val)

{

System.out.println("返回值为:"+val);

}

@Around("selectMymother()") //相当于 @Around("execution (** com.mrw.springtest9.Mymother.*(..))")

public void alll(){

System.out.println("哪里都有我!");

}

}


相比一下Pointcut是不是更加简洁啦

在beans.xml配置Spring AOP

Life.java

package com.mrw.springtest10;

public class Life {

public void strive()

{

System.out.println("After:奋斗!奋斗!奋斗的人生最美妙!");

}

public void hard()

{

System.out.println("Before:生活必须奋力拼搏!");

}

public void havemoney(Object var)

{

System.out.println("我有"+var+"块钱!");

}

public void need()

{

System.out.println("Around:生活需要空气和水");

}

}


ExolLife.java

public class ExolLife {

public void exol()

{

System.out.println("生活如此美妙!");

}

public double moremoney(){

return 10000;

}

}


在beans.xml中进行配置

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:aop="http://www.springframework.org/schema/aop"
//导入相应的命名空间

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">

<bean id="life" class="com.mrw.springtest10.Life"></bean>

<aop:config>

<aop:aspect ref="life"> //指定相对应的类的id

<aop:pointcut expression="execution(** com.mrw.springtest10.ExolLife.moremoney(..))" id="money"/>
//指定表达式

<aop:pointcut expression="execution(** com.mrw.springtest10.ExolLife.exol())" id="exol"/>
//指定表达式

<aop:after method="strive" pointcut-ref="exol"/> //Life中的strive方法在调用ExolLife.exol方法之后执行

<aop:before method="hard" pointcut-ref="exol"/>//Life中的hard方法在调用ExolLife.exol方法之前执行

<aop:after-returning method="havemoney" returning="var" pointcut-ref="money"/>//Life中的havemoney方法在调用ExolLife.moremoney()有返回值时执行,并捕获返回值

<aop:around method="need" pointcut-ref="exol"/> //Life中的need方法在调用ExolLife.exol方法时执行

</aop:aspect>

</aop:config>

<bean id="ExolLife" class="com.mrw.springtest10.ExolLife"></bean>

</beans>


编写测试类Test1.java进行测试

package com.mrw.springtest10;

import org.junit.Test;

import org.springframework.context.ConfigurableApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test1 {

@Test

public void test() {

ConfigurableApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");

ExolLife life=(ExolLife)context.getBean("ExolLife");

life.exol();

}

}


输出结果为:



要想输出其它结果可改变测试类进行测试

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: