Skip to content

Commit 785e47b

Browse files
author
chenhaoxiang
committed
AOP拦截-使用切点:AspectJExpressionPointcut-切点语言
1 parent 61555ab commit 785e47b

8 files changed

Lines changed: 167 additions & 1 deletion

File tree

‎Spring/mySpringAopDemo/README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ http://blog.csdn.net/qq_26525215/article/details/52412901
1111
http://blog.csdn.net/qq_26525215/article/details/52420658
1212

1313

14-
14+
【框架】[Spring]AOP拦截-使用切点:AspectJExpressionPointcut-切点语言:
15+
http://blog.csdn.net/qq_26525215/article/details/52422395
1516

1617

1718

4.76 KB
Binary file not shown.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package cn.hncu.xmlImpl.aspectj;
2+
3+
import org.aopalliance.intercept.MethodInterceptor;
4+
import org.aopalliance.intercept.MethodInvocation;
5+
6+
public class AroundAdvice implements MethodInterceptor{
7+
@Override
8+
public Object invoke(MethodInvocation invocation) throws Throwable {
9+
System.out.println("前面拦截....");
10+
Object resObj = invocation.proceed();//放行
11+
System.out.println("后面拦截.....");
12+
return resObj;
13+
}
14+
15+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package cn.hncu.xmlImpl.aspectj;
2+
3+
import org.aopalliance.aop.Advice;
4+
import org.aopalliance.intercept.MethodInterceptor;
5+
import org.aopalliance.intercept.MethodInvocation;
6+
import org.junit.Test;
7+
import org.springframework.aop.Advisor;
8+
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
9+
import org.springframework.aop.framework.ProxyFactoryBean;
10+
import org.springframework.aop.support.DefaultPointcutAdvisor;
11+
12+
public class AspectjDemo {
13+
14+
@Test
15+
public void demo(){
16+
17+
ProxyFactoryBean factory = new ProxyFactoryBean();
18+
factory.setTarget(new Person());
19+
20+
//声明一个aspectj切点
21+
AspectJExpressionPointcut cut = new AspectJExpressionPointcut();
22+
23+
//设置需要拦截的方法-用切点语言来写
24+
cut.setExpression("execution( int cn.hncu.xmlImpl.aspectj.Person.run() )");//拦截:空参返回值为int的run方法
25+
//cut.setExpression("execution( void cn.hncu.xmlImpl.aspectj.Person.*() )");//拦截:空参空返回值的任意方法
26+
//cut.setExpression("execution (void cn.hncu.xmlImpl.aspectj.Person.*(String))");//拦截:只有1个String类型参数,空返回值的任意方法
27+
//cut.setExpression("execution( void cn.hncu.xmlImpl.aspectj.Person.*(*) )");//拦截:有1个参数(类型不限),空返回值的任意方法
28+
//cut.setExpression("execution( void cn.hncu.xmlImpl.aspectj.Person.*(*,*) )");//拦截:有2个参数(类型不限),空返回值的任意方法
29+
//cut.setExpression("execution( void cn.hncu.xmlImpl.aspectj.Person.*(..) )");//拦截:任意(个数和类型)参数,空返回值的任意方法
30+
//cut.setExpression("execution( int cn.hncu.xmlImpl.aspectj.Person.*(*,..) )");//拦截:至少有1个参数(类型不限),返回值类型是int的任意方法
31+
//cut.setExpression("execution( * cn.hncu.xmlImpl.aspectj.Person.*(*,..) )");//拦截:至少有1个参数(类型不限),返回值类型任意的方法
32+
//cut.setExpression("execution( * cn.hncu..*son.*(*,..) )");//拦截:cn.hncu包下,类名以"son"结束,至少有1个参数(类型不限),返回值类型任意的方法
33+
34+
Advice advice = new MethodInterceptor() {
35+
@Override
36+
public Object invoke(MethodInvocation invocation) throws Throwable {
37+
System.out.println("放行前拦截...");
38+
Object obj = invocation.proceed();//放行
39+
System.out.println("放行后拦截...");
40+
return obj;
41+
}
42+
};
43+
44+
//切面=切点+通知
45+
Advisor advisor = new DefaultPointcutAdvisor(cut,advice);
46+
factory.addAdvisor(advisor);
47+
Person p = (Person) factory.getObject();
48+
49+
p.run();
50+
p.run(10);
51+
p.say();
52+
p.sayHi("Jack");
53+
p.say("Tom", 666);
54+
}
55+
56+
57+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cn.hncu.xmlImpl.aspectj;
2+
3+
import org.junit.Test;
4+
import org.springframework.context.ApplicationContext;
5+
import org.springframework.context.support.ClassPathXmlApplicationContext;
6+
7+
public class AspectjXmlDemo {
8+
@Test
9+
public void demo1(){
10+
ApplicationContext ctx = new ClassPathXmlApplicationContext("cn/hncu/xmlImpl/aspectj/aspectj.xml");
11+
Person p = ctx.getBean(Person.class);
12+
p.run();
13+
p.run(10);
14+
p.say();
15+
p.sayHi("Jack");
16+
p.say("Tom", 666);
17+
}
18+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package cn.hncu.xmlImpl.aspectj;
2+
3+
public class Person {
4+
public int run(){
5+
System.out.println("返回值为int-我在run...");
6+
return 0;
7+
}
8+
9+
public void run(int i){
10+
System.out.println("我在run...<"+i+">");
11+
}
12+
13+
public void say(){
14+
System.out.println("我在say...");
15+
}
16+
public void sayHi(String name){
17+
System.out.println("Hi,"+name+",你好");
18+
}
19+
public int say(String name, int i){
20+
System.out.println(name+ "----"+ i);
21+
return 0;
22+
}
23+
24+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:context="http://www.springframework.org/schema/context"
5+
xmlns:tx="http://www.springframework.org/schema/tx"
6+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
7+
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
8+
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
9+
<!-- 自动代理 -->
10+
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>
11+
12+
<bean id="p" class="cn.hncu.xmlImpl.aspectj.Person"></bean>
13+
14+
<!-- 切面=切点+通知 (把切点和通知写成内部bean)-->
15+
<bean id="cut" class="org.springframework.aop.aspectj.AspectJExpressionPointcut">
16+
<!-- 拦截:cn.hncu包下,类名以"son"结束,至少有1个参数(类型不限),返回值类型任意的方法 -->
17+
<property name="expression" value="execution( * cn.hncu..*son.*(*,..) )"></property>
18+
</bean>
19+
20+
<bean id="advisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
21+
<property name="pointcut" ref="cut"></property>
22+
<property name="advice">
23+
<bean id="advice" class="cn.hncu.xmlImpl.aspectj.AroundAdvice"></bean>
24+
</property>
25+
</bean>
26+
27+
28+
</beans>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:context="http://www.springframework.org/schema/context"
5+
xmlns:tx="http://www.springframework.org/schema/tx"
6+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
7+
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
8+
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
9+
10+
<!-- 自动代理 -->
11+
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>
12+
13+
<bean id="p" class="cn.hncu.xmlImpl.aspectj.Person"></bean>
14+
15+
<!-- 切面=切点+通知 (※※采用面向切点语言进行配置切面)-->
16+
<bean id="advisor" class="org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor">
17+
<property name="expression" value="execution( * cn.hncu..*son.*(*,..) )"></property>
18+
<property name="advice">
19+
<bean id="advice" class="cn.hncu.xmlImpl.aspectj.AroundAdvice"></bean>
20+
</property>
21+
</bean>
22+
23+
</beans>

0 commit comments

Comments
 (0)