ITeye博客

spring简单aop切面示例

linlargy · 2013-04-15 22:48 · java框架技术 · java框架组件应用 · java技术模块开发应用 · 行业应用 · Spring · AOP · Markdown

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("AServiceImpl.barA(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]

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。