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

第一种配置方法:aop:advisor:
马克- to-win:马克 java社区:防盗版实名手机尾号: 73203
advice-ref说明切别人的程序是什么,advice的英文翻译是“通知”,意思是主业务程序执行到某个方法之前之后发出的通知。pointcut-ref说明被切的业务主程序是什么。




例 2.1.1

<?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: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"
       >
 
    <context:component-scan
        base-package="com" />
    <context:component-scan
        base-package="service" />
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="loginService" class="service.LoginServiceImpl" >

    </bean>
 
    <bean id="myTransactionManager" class="aop.AOPMyTransactionManagerMark_To_Win" />
  
    <!-- 配置切面 这种写法也正确"execution(* service.*.*(..))"-->
    <aop:config>
        <aop:pointcut id="myPointcut" expression="execution(* service.LoginServiceImpl.*(..))" />
        <aop:advisor advice-ref="myTransactionManager" pointcut-ref="myPointcut"/>
    </aop:config>
</beans>


<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
    <title>Spring 3.0</title>
</head>
<body>
    <a href="helloa.do">点击跳转,你好,马克-to-win</a>
</body>
</html>





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" %>
<html>
   <body>
    ${message}
   </body>
</html>


输出结果:

模拟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类型.
例如:
<aop:pointcut id="myPointcut" expression="execution(* service.LoginServiceImpl.*(..))" />