Aop的第一种配置方法:aop:advisor


1)第一种配置方法:aop:advisor:

advice-ref说明切别人的程序是什么,advice的英文翻译是“通知”,意思是主业务程序执行到某个方法之前之后发出的通知。pointcut-ref说明被切的业务主程序是什么。

例 2.1.1

<?xml version="1.0" encoding="UTF-8"?>
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
       >
 
            base-package="com" />
            base-package="service" />
            class="org.springframework.web.servlet.view.UrlBasedViewResolver">
                    value="org.springframework.web.servlet.view.JstlView" />
       
       
   


   

   

   
   
    
   
   
       
       
   




<%@ page contentType="text/html; charset=GBK" %>


    Spring 3.0


    点击跳转,你好,马克-to-win



package com;
import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContextUtils;
import service.interfac.ILoginService;

@Controller
public class HelloWorldController {
    private ILoginService loginServic;

    @RequestMapping("/helloa")
    public ModelAndView helloWorld(HttpServletRequest request, HttpServletResponse response,
            HttpSession sesssion) {
        ServletContext sc=RequestContextUtils.getWebApplicationContext(request).getServletContext();
        WebApplicationContext wac=WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
        ILoginService loginServic=(ILoginService)wac.getBean("loginService");
        loginServic.login();
        System.out.println("after loginServic.login()");
/*ModelAndView就是在view和model中传数据*/
        return new ModelAndView("/helloq", "message", "你好");
    }
}

package service;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
import com.NiutDAO;
import service.interfac.ILoginService;
public class LoginServiceImpl implements ILoginService {
    public void login() {
        System.out.println("LoginServiceImpl");
    }    
}

package aop;
import java.lang.reflect.Method;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class AOPMyTransactionManagerMark_To_Win implements MethodInterceptor {
    public Object invoke(MethodInvocation arg0) throws Throwable {
        System.out.println("模拟start transaction");
        arg0.proceed();
        System.out.println("模拟commit transaction");
        return null;
    }
}

helloq.jsp

<%@ page contentType="text/html; charset=GBK" %>

  
    ${message}
  


输出结果:

模拟start transaction
LoginServiceImpl
模拟commit transaction
after loginServic.login()



补充语法:execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)throws-pattern?)
returning type pattern,name pattern, and parameters pattern是必须的.
ret-type-pattern:可以为*表示任何返回值,全路径的类名等.
name-pattern:指定方法名,*代表所有,set*,代表以set开头的所有方法.
parameters pattern:指定方法参数(声明的类型),(..)代表所有参数,(*)代表一个参数,(*,String)代表第一个参数为任何值,第二个为String类型.
例如:


更多内容请见原文,文章转载自:https://blog.csdn.net/qq_44591615/article/details/109206288