代码一:接口和实现类
-
package
cn.itcast.server;
-
public
interface
IPersonService {
-
public
void
save(String name);
-
public
String getPersonName(Integer id);
-
public
void
update(String name,Integer id);
-
}
-
package
cn.itcast.server.impl;
-
import
cn.itcast.server.IPersonService;
-
public
class
PersonServiceBean2
implements
IPersonService {
-
public
String getPersonName(Integer id) {
-
System.out.println("这是getPersonName方法"
);
-
return
"xxx"
;
-
}
-
public
void
save(String name) {
-
-
System.out.println("这是save方法"
);
-
}
-
public
void
update(String name, Integer id) {
-
System.out.println("这是update方法"
);
-
}
-
}
代码二:拦截器的定义
-
package
cn.itcast.server;
-
import
org.aspectj.lang.ProceedingJoinPoint;
-
-
public
class
MyInteceptor2 {
-
public
void
doAccessCheck(){
-
System.out.println("这里输出的是前置通知 "
);
-
}
-
public
void
doAfterReturning(){
-
System.out.println("这里输出的是后置通知 "
);
-
}
-
public
void
doAfterReturn(){
-
System.out.println("这里输出的是最终通知"
);
-
}
-
public
void
doAfterThrowing(){
-
System.out.println("这里输出的是例外通知 "
);
-
}
-
public
Object doBasicProfiling(ProceedingJoinPoint pjp)
throws
Throwable{
-
-
System.out.println("进入方法"
);
-
Object result = pjp.proceed();
-
System.out.println("退出方法"
);
-
return
result;
-
}
-
}
代码三:配置文件
-
<?
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:aop
=
"http://www.springframework.org/schema/aop"
-
xsi:schemaLocation
="http://www.springframework.org/schema/beans
-
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
-
http://www.springframework.org/schema/context
-
http://www.springframework.org/schema/context/spring-context-2.5.xsd
-
http://www.springframework.org/schema/aop
-
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
-
">
-
<
aop:aspectj-autoproxy
/>
-
-
<
bean
id
=
"myInteceptor2"
class
=
"cn.itcast.server.MyInteceptor2"
>
</
bean
>
-
<
bean
id
=
"personService2"
class
=
"cn.itcast.server.impl.PersonServiceBean2"
>
</
bean
>
-
<
aop:config
>
-
<
aop:aspect
id
=
"asp"
ref
=
"myInteceptor2"
>
-
-
-
<
aop:pointcut
id
=
"mycut"
expression
=
"execution(* cn.itcast.server.impl.PersonServiceBean2.*(java.lang.String,..) )"
/>
-
-
<
aop:before
pointcut-ref
=
"mycut"
method
=
"doAccessCheck"
/>
-
<
aop:after-returning
pointcut-ref
=
"mycut"
method
=
"doAfterReturning"
/>
-
<
aop:after-throwing
pointcut-ref
=
"mycut"
method
=
"doAfterThrowing"
/>
-
<
aop:after
pointcut-ref
=
"mycut"
method
=
"doAfterReturn"
/>
-
<
aop:around
pointcut-ref
=
"mycut"
method
=
"doBasicProfiling"
/>
-
</
aop:aspect
>
-
</
aop:config
>
-
</
beans
>
测试类:
-
package
junit.test;
-
import
org.junit.BeforeClass;
-
import
org.junit.Test;
-
import
org.springframework.context.ApplicationContext;
-
import
org.springframework.context.support.ClassPathXmlApplicationContext;
-
import
cn.itcast.server.IPersonService;
-
public
class
SpringAOPTest {
-
@BeforeClass
-
public
static
void
setUpBeforeClass()
throws
Exception {
-
}
-
@Test
public
void
interceptorTest(){
-
ApplicationContext ac = new
ClassPathXmlApplicationContext(
"beans.xml"
);
-
IPersonService ipersonService = (IPersonService) ac.getBean("personService2"
);
-
ipersonService.update("xxx"
,
2
);
-
ipersonService.save("xx"
);
-
ipersonService.getPersonName(2
);
-
}
-
}
分享到:
相关推荐
Spring通过XML实现AOP,Spring通过XML实现AOP,Spring通过XML实现AOP
在Xml配置AOP有两种实现方式:一、基于代理实现AOP切面编程;二、通过自动代理实现AOP切面编程
Xml文件配置实现AOP通知
spring xml 实现aop切面编程 内附注释,希望对入门的新手有帮助
XML方式实现AOP编程
spring框架使用xml配置文件实现aop
注解方式实现AOP编程
Spring 使用AspectJ 实现 AOP(基于xml文件、基于注解)
java xml方式实现springaop编程,包含源码+jar包+解释
NULL 博文链接:https://huangminwen.iteye.com/blog/1486876
基于xml实现动态动态代理 aop 请查看
在Spring1.2或之前的版本中,实现AOP的传统方式就是通过实现Spring的AOP API来定义Advice,并设置代理对象。Spring根据Adivce加入到业务流程的时机的不同,提供了四种不同的Advice:Before Advice、After Advice、...
Spring AOP 的实现例子(基于XML配置实现); 具体介绍看这里:https://blog.csdn.net/qq_22078107/article/details/85865543
主要介绍了SpringMVC用XML方式实现AOP的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
所谓连接点是指那些被拦截到的点。在spring中,这些点指的是方法,因为spring只支持方法类型的连接点
里面包括4个例子:(1)Spring实现AOP方式之一:基于XML配置的Spring AOP (2)Spring实现AOP方式之二:使用注解配置 Spring AOP (3)Spring AOP : AspectJ Pointcut 切点 (4)Spring AOP : Advice 声明 (通知注解)
NULL 博文链接:https://huihai.iteye.com/blog/858804
主要介绍了Spring使用AspectJ注解和XML配置实现AOP的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
配置可以通过xml文件来进行,大概有四种方式: 1. 配置ProxyFactoryBean,显式地设置advisors, advice, target等(基于代理的AOP ) 2. 配置AutoProxyCreator,这种方式下,还是如以前一样使用定义的bean,但是从...
springboot spring aop 拦截器 注解方式实现脱敏(涉及到:pom.xml -->application.properties --->启动类-->拦截器)