Spring框架中的aop操作之二 通过配置文件实现增强

配置文件代码:

<?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">
    <!-- 开启注解扫描 -->
    <context:component-scan base-package="com.swift"></context:component-scan>
    
    <bean id="book" class="com.swift.aop.Book"></bean>
    <bean id="adviceBook" class="com.swift.aop.AdviceBook"></bean>
    
    <aop:config>
    <!-- 切入点表达式第一个*表示public private等权限后接空格,第二个*表示任意方法(..)表示参数 -->
    <aop:pointcut expression="execution(* com.swift.aop.Book.*())" id="pointcut1"/>
    
    <!-- 哪个切面(用来增强切入点的类) 名称是什么用ref表示 -->
    <aop:aspect ref="adviceBook">
    <!-- 增强的具体方法,增强哪个切入点 -->
    <aop:before method="before" pointcut-ref="pointcut1"/>
    </aop:aspect>
    </aop:config>
</beans>

包括bean context aop三个约束

以及切面的配置——表达式execution含义、advice通知/增强设置


 

连接点joinpoint的类,即需要被增强的类:

package com.swift.aop;

public class Book {
    public String fun() {
        System.out.println("This is Book's fun()..............");
        return "This is Book's fun()..............";
    }
}

进行切面操作的类:

package com.swift.aop;

public class AdviceBook {
    public String before() {
        System.out.println("This is AdviceBook's before()...............");
        return "This is AdviceBook's before()...............";
    }
}

测试的类:

package com.swift.web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.swift.aop.Book;
@WebServlet("/test")
public class ServleTest extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public ServleTest() {
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().append("Served at: ").append(request.getContextPath());
        ApplicationContext context=new ClassPathXmlApplicationContext("aop.xml");
        Book book=(Book)context.getBean("book");
        response.getWriter().append(book.fun());
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

演示效果图:

浏览器无反应

只有Book的方法,没有前置的before,想来应该是调用了但是返回的字符串没能显示在浏览器上

控制台console成功

前置增强在切入点前输出


 

如果是后置增强,只需要在增强的类中,增加后置增强的方法

public String after() {
System.out.println("This is AdviceBook's after()...............");
return "This is AdviceBook's after()...............";
}

然后在xml配置文件中添加切面配置的代码

<aop:after-returning method="after" pointcut-ref="pointcut1"/>

即可。


 

还有一个环绕的方法,有一个很长的表达不好记 ProceedingJoinPoint这个参数的对象有个方法,执行就是切入点的方法

在增强类中写方法

public String around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("This is AdviceBook's front()...............");
proceedingJoinPoint.proceed();
System.out.println("This is AdviceBook's end()...............");
return "This is AdviceBook's around()...............";
}

在<aop:config></aop:config>中写

<aop:around method="around" pointcut-ref="pointcut1"/>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值