1.在之前的基础上导入以下jar包
aopalliance-1.0.jar
aspectj-1.7.1.jar
aspectjweaver-1.7.1.jar
一、注解的AOP配置
1.编写AspectTest类
@Order(1) //多个AOP切面的执行顺序
@Aspect
public class AspectTest {
//exection("修饰符 返回值 所属类.方法名(参数列表)")
//切点
@Pointcut("execution(* service.impl.*.*(..))")
public void test(){
}
//前置通知
@Before(value="test()")
public void before(JoinPoint jp){
System.out.println("方法名称"+jp.getSignature().getName());
System.out.println("输入参数为:"+Arrays.asList(jp.getArgs()));
}
//后置通知
@After(value="test()")
public void after(JoinPoint jp){
System.out.println(jp.getSignature().getName()+"结束");
}
//返回通知
@AfterReturning(value="test()",returning="obj")
public void afterReturning(JoinPoint jp,Object obj){
System.out.println("返回值为:"+obj);
}
//异常通知
@AfterThrowing(value="test()",throwing="obj")
public void afterThrowing(JoinPoint jp,Exception obj){
System.out.println("异常信息:"+obj.getMessage());
}
}
2.在config目录下新建applicationContext.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: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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!-- 扫描service 层注解 -->
<context:component-scan base-package="service"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!-- bean配置 -->
<bean class="common.AspectTest" id="aspectTest"></bean>
</beans>
3.在service包下编写接口
package service;
public interface TestService {
public String testAspect(String name);
}
4.在service.impl包下写实现类
@Service("testServeice")
public class TestServiceImpl implements TestService {
@Override
public String testAspect(String name) {
return name;
}
}
5.测试:
public static void main(String[] args) {
//获取容器
ApplicationContext ap = new ClassPathXmlApplicationContext(new String[]{"classpath:applicationContext.xml","classpath:applicationContext-dao.xml"});//如果要引用多个XML的话
TestService testServeice = (TestService) ap.getBean("testServeice");
System.out.println(testServeice.testAspect("12"));
System.out.println(testServeice.test());
}
控制台结果:
方法名称testAspect
输入参数为:[12]
testAspect结束
返回值为:12
12
二、XML的AOP配置:
1.修改AspectTest类:
public class AspectTest {
public void before(JoinPoint jp){
System.out.println("方法名称"+jp.getSignature().getName());
System.out.println("输入参数为:"+Arrays.asList(jp.getArgs()));
}
public void after(JoinPoint jp){
System.out.println(jp.getSignature().getName()+"结束");
}
public void afterReturning(JoinPoint jp,Object obj){
System.out.println("返回值为:"+obj);
}
public void afterThrowing(JoinPoint jp,Exception obj){
System.out.println("异常信息:"+obj.getMessage());
}
}
2.XML中的配置:
<!--<aop:aspectj-autoproxy></aop:aspectj-autoproxy>-->
<!-- bean配置 -->
<bean class="common.AspectTest" id="aspectTest"></bean>
<aop:config>
<!-- 配置切点表达式 -->
<aop:pointcut expression="execution(* service.impl.*.*(..))" id="pointcut"/>
<!-- 配置切面及通知 -->
<aop:aspect ref="aspectTest" order="1">
<aop:before method="before" pointcut-ref="pointcut"/>
<aop:after method="after" pointcut-ref="pointcut"/>
<aop:after-returning method="before" pointcut-ref="pointcut" returning="obj"/>
<aop:after-throwing method="before" pointcut-ref="pointcut" throwing="obj"/>
</aop:aspect>
</aop:config>
3.重新测试:
控制台结果:
方法名称testAspect
输入参数为:[12]
testAspect结束
返回值为:12
12
aopalliance-1.0.jar
aspectj-1.7.1.jar
aspectjweaver-1.7.1.jar
一、注解的AOP配置
1.编写AspectTest类
@Order(1) //多个AOP切面的执行顺序
@Aspect
public class AspectTest {
//exection("修饰符 返回值 所属类.方法名(参数列表)")
//切点
@Pointcut("execution(* service.impl.*.*(..))")
public void test(){
}
//前置通知
@Before(value="test()")
public void before(JoinPoint jp){
System.out.println("方法名称"+jp.getSignature().getName());
System.out.println("输入参数为:"+Arrays.asList(jp.getArgs()));
}
//后置通知
@After(value="test()")
public void after(JoinPoint jp){
System.out.println(jp.getSignature().getName()+"结束");
}
//返回通知
@AfterReturning(value="test()",returning="obj")
public void afterReturning(JoinPoint jp,Object obj){
System.out.println("返回值为:"+obj);
}
//异常通知
@AfterThrowing(value="test()",throwing="obj")
public void afterThrowing(JoinPoint jp,Exception obj){
System.out.println("异常信息:"+obj.getMessage());
}
}
2.在config目录下新建applicationContext.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: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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!-- 扫描service 层注解 -->
<context:component-scan base-package="service"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!-- bean配置 -->
<bean class="common.AspectTest" id="aspectTest"></bean>
</beans>
3.在service包下编写接口
package service;
public interface TestService {
public String testAspect(String name);
}
4.在service.impl包下写实现类
@Service("testServeice")
public class TestServiceImpl implements TestService {
@Override
public String testAspect(String name) {
return name;
}
}
5.测试:
public static void main(String[] args) {
//获取容器
ApplicationContext ap = new ClassPathXmlApplicationContext(new String[]{"classpath:applicationContext.xml","classpath:applicationContext-dao.xml"});//如果要引用多个XML的话
TestService testServeice = (TestService) ap.getBean("testServeice");
System.out.println(testServeice.testAspect("12"));
System.out.println(testServeice.test());
}
控制台结果:
方法名称testAspect
输入参数为:[12]
testAspect结束
返回值为:12
12
二、XML的AOP配置:
1.修改AspectTest类:
public class AspectTest {
public void before(JoinPoint jp){
System.out.println("方法名称"+jp.getSignature().getName());
System.out.println("输入参数为:"+Arrays.asList(jp.getArgs()));
}
public void after(JoinPoint jp){
System.out.println(jp.getSignature().getName()+"结束");
}
public void afterReturning(JoinPoint jp,Object obj){
System.out.println("返回值为:"+obj);
}
public void afterThrowing(JoinPoint jp,Exception obj){
System.out.println("异常信息:"+obj.getMessage());
}
}
2.XML中的配置:
<!--<aop:aspectj-autoproxy></aop:aspectj-autoproxy>-->
<!-- bean配置 -->
<bean class="common.AspectTest" id="aspectTest"></bean>
<aop:config>
<!-- 配置切点表达式 -->
<aop:pointcut expression="execution(* service.impl.*.*(..))" id="pointcut"/>
<!-- 配置切面及通知 -->
<aop:aspect ref="aspectTest" order="1">
<aop:before method="before" pointcut-ref="pointcut"/>
<aop:after method="after" pointcut-ref="pointcut"/>
<aop:after-returning method="before" pointcut-ref="pointcut" returning="obj"/>
<aop:after-throwing method="before" pointcut-ref="pointcut" throwing="obj"/>
</aop:aspect>
</aop:config>
3.重新测试:
控制台结果:
方法名称testAspect
输入参数为:[12]
testAspect结束
返回值为:12
12