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

第一種配置方法:aop:advisor:
馬克- to-win:馬克 java社區(qū):防盜版實(shí)名手機(jī)尾號(hào): 73203
advice-ref說(shuō)明切別人的程序是什么,advice的英文翻譯是“通知”,意思是主業(yè)務(wù)程序執(zhí)行到某個(gè)方法之前之后發(fā)出的通知。pointcut-ref說(shuō)明被切的業(yè)務(wù)主程序是什么。




例 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" />
  
    <!-- 配置切面 這種寫(xiě)法也正確"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">點(diǎn)擊跳轉(zhuǎn),你好,馬克-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中傳數(shù)據(jù)*/
        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>


輸出結(jié)果:

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



補(bǔ)充語(yǔ)法: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開(kāi)頭的所有方法.
parameters pattern:指定方法參數(shù)(聲明的類型),(..)代表所有參數(shù),(*)代表一個(gè)參數(shù),(*,String)代表第一個(gè)參數(shù)為任何值,第二個(gè)為String類型.
例如:
<aop:pointcut id="myPointcut" expression="execution(* service.LoginServiceImpl.*(..))" />