---
title: "spring简单aop切面示例"
author: "linlargy"
date: "2013-04-15T22:48:49Z"
tags: ["AOP", "Spring"]
categories: ["行业应用"]
slug: "spring-aop"
draft: false
lastmod: "2013-04-15T22:48:49Z"
id: 1847908
url: "https://linlargy.iteye.com/blog/1847908"
---

spirng简单AOP切面示例 配置文件: \[code="xml"] \[/code] 切面类: \[code="java"] public class MyAspect { public void doAfter(JoinPoint jp) { System.out.println("调用后"+jp.getTarget().getClass().getName() + "." + jp.getSignature().getName()); } public void doBefore(JoinPoint jp) { System.out.println("调用前"+jp.getTarget().getClass().getName() + "调用方法." + jp.getSignature().getName()); } } \[/code] 普通业务类 \[code="java"] public interface IService { public void foo(String msg ,int type); public void bar(); } public class Aservice implements IService { public void bar() { // TODO Auto-generated method stub } public void foo(String msg, int type) { // TODO Auto-generated method stub System.out.println("ＡServiceImpl.barＡ(msg:"+msg+" type:"+type+")"); } } public class Bservice implements IService { public void bar() { // TODO Auto-generated method stub System.out.println("BServiceImpl.fooB()"); } public void foo(String msg, int type) { // TODO Auto-generated method stub System.out.println("BServiceImpl.barB(msg:"+msg+" type:"+type+")"); } } \[/code] 测试类: \[code="java"] public class TestAop { /\** * @param args \*/ @SuppressWarnings("deprecation") public static void main(String\[] args) { // TODO Auto-generated method stub ApplicationContext ac = new FileSystemXmlApplicationContext( "src/applicationContext.xml"); IService aService = (IService) ac.getBean("aService"); IService bService = (IService) ac.getBean("bService"); System.out.println("开始调用aService方法"); aService.foo("hello", 0); System.out.println("--------------------"); System.out.println("开始调用bService方法"); bService.foo("world", 1); } } \[/code]