spring-aop示例

时间:2023-12-20 21:09:38

具体案例放在github上,主要是jar包在上面

https://github.com/guoyansi/spring-aop-example

spring-aop示例

knights.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="knight" class="p1.BraveKnight">
<constructor-arg ref="quest" />
</bean>
<bean id="quest" class="p1.SlayDragonQuest"></bean> <bean id="minstrel" class="p1.Minstrel"></bean> <aop:config>
<aop:aspect ref="minstrel">
<aop:pointcut id="embark" expression="execution(* *.embarkOnQuest(..))" />
<aop:before pointcut-ref="embark" method="singBeforeQuest"/>
<aop:after pointcut-ref="embark" method="singAfterQuest"/>
</aop:aspect>
</aop:config>
</beans>

IQuest.java

package p1;

/**
* 探险
*
*/
public interface IQuest {
void embark();
}

IKnight.java

package p1;

public interface IKnight {
void embarkOnQuest();
}

BraveKnight.java

package p1;

/**
* 骑士
*
*/
public class BraveKnight implements IKnight{
private IQuest quest; public BraveKnight(IQuest quest){
this.quest=quest; }
@Override
public void embarkOnQuest (){
quest.embark();
} }

SlayDragonQuest.java

package p1;

public class SlayDragonQuest implements IQuest{
@Override
public void embark() {
System.out.println("SlayDragonQuest的embark......");
}
}

Minstrel.java

package p1;

public class Minstrel {
public void singBeforeQuest(){
System.out.println("探险之前...");
}
public void singAfterQuest(){
System.out.println("探险之后......");
} }
package p1;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Run {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("classpath*:knights.xml");
IKnight knight=(IKnight)context.getBean("knight");
knight.embarkOnQuest();
}
}

执行run的结果:

spring-aop示例

上面是一个完整的例子.如果没有commons-logging.jar

控制台上的那些红色字样就不会输出,还会出现异常.

如果没有aopalliance.jar:

nested exception is java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice

spring-aop示例

如果没有aspectjweaver.jar

nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException

spring-aop示例