parent
cc48720d46
commit
931160f9b7
@ -0,0 +1,25 @@
|
||||
<?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: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/aop
|
||||
http://www.springframework.org/schema/aop/spring-aop.xsd">
|
||||
|
||||
<bean id="log" class="com.ssm.aop.Log"/>
|
||||
|
||||
<bean id="category" class="com.ssm.aop.Category"/>
|
||||
<bean id="flower" class="com.ssm.aop.Flower"/>
|
||||
<bean id="announcement" class="com.ssm.aop.Announcement"/>
|
||||
<bean id="content" class="com.ssm.aop.Content"/>
|
||||
|
||||
<aop:config>
|
||||
<aop:pointcut id="myPointcut" expression="execution(* com.ssm.aop.*.print*(..))"/>
|
||||
<aop:aspect ref="log">
|
||||
<aop:before method="beforeAdvice" pointcut-ref="myPointcut"/>
|
||||
</aop:aspect>
|
||||
</aop:config>
|
||||
|
||||
</beans>
|
||||
@ -0,0 +1,12 @@
|
||||
package com.ssm.aop;
|
||||
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
|
||||
public class Log {
|
||||
public void beforeAdvice(JoinPoint joinPoint) {
|
||||
Object target = joinPoint.getTarget();
|
||||
String methodName = joinPoint.getSignature().getName();
|
||||
System.out.println("前置通知:模拟日志的记录...目标类是:" + target
|
||||
+ ", 被切入通知的目标方法为:" + methodName);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,23 @@
|
||||
package com.ssm.aop;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
public class TestAop {
|
||||
public static void main(String[] args) {
|
||||
// 这里必须用我们专门写的 aop 配置文件
|
||||
ApplicationContext ac = new ClassPathXmlApplicationContext("bean-aop.xml");
|
||||
|
||||
Category category = (Category) ac.getBean("category");
|
||||
category.printMessage();
|
||||
|
||||
Flower flower = (Flower) ac.getBean("flower");
|
||||
flower.printFlower();
|
||||
|
||||
Announcement announcement = (Announcement) ac.getBean("announcement");
|
||||
announcement.printPub();
|
||||
|
||||
Content content = (Content) ac.getBean("content");
|
||||
content.printContent();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue