前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Aop的第一种配置方法:aop:advisor

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

作者头像
马克java社区
修改2021-05-19 11:24:19
8100
修改2021-05-19 11:24:19
举报
文章被收录于专栏:java大数据

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

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;

@Controlle

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.*(..))" />

更多请见下节:https://blog.csdn.net/qq_44591615/article/details/109206288

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档