AOP Simple example

原创 2004年07月08日 00:24:00

There is a simple Spring AOP Style example that took me some time to finish it. It's so easy and enjoyable. Hope it would do some help to you. <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

代码:


import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * User: <a href="mailto:xxxxx@utstar.com">xxxxxxx</a>
 * Date: Dec 17, 2003
 * Time: 7:54:56 PM
 * all rights reserved by utstarcom
 */
public class MyInterceptor implements MethodInterceptor {
    private final Log logger=LogFactory.getLog(MyInterceptor.class);
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        logger.info("Beginning method: " + methodInvocation.getMethod().getDeclaringClass() + "::" + methodInvocation.getMethod().getName());
        for(int i=0;i<methodInvocation.getArgumentCount();i++){
            logger.info(methodInvocation.getArgument(i));
        }
        long startTime = System.currentTimeMillis();
        try {
            Object retVal = methodInvocation.proceed();
            return retVal;
        } finally {
            logger.info("Ending method: " + methodInvocation.getMethod().getDeclaringClass() + "::" + methodInvocation.getMethod().getName());
            logger.info("Method invocation time: " + (System.currentTimeMillis() - startTime) + " msecs.");
        }
    }
}



代码:

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * User: <a href="mailto:xxxxx@utstar.com">xxxxxx</a>
 * Date: Dec 17, 2003
 * Time: 8:07:41 PM
 * all rights reserved by utstarcom
 */
public class HelloWorldImpl implements HelloWorld{
    private final static Log logger = LogFactory.getLog(HelloWorldImpl.class);

    public void sayHello(int age,String name){
        logger.info("age:"+age+" name:"+name);
    }
}




代码:

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;

import java.io.InputStream;
import java.io.FileInputStream;

/**
 * User: <a href="mailto:xxxxxx@utstar.com">xxxxx</a>
 * Date: Dec 17, 2003
 * Time: 8:00:38 PM
 * all rights reserved by utstarcom
 */
public class StudyAOP {
    public static void main(String[] args){
        //try to initialize the BeanFactory
        BeanFactory factory = null;
        InputStream in=null;
        try{
            in = Thread.currentThread().getContextClassLoader().getResourceAsStream("config.xml");
            factory=new XmlBeanFactory(in);
            HelloWorld greet=(HelloWorld)factory.getBean("greeting");
            greet.sayHello(10,"zhonglin");
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try{in.close();}catch(Exception e){}
        }

    }
}

 

代码:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">


<beans>

    <bean id="helloworld" class="HelloWorldImpl"/>
    <bean id="myInterceptor" class="MyInterceptor"/>

    <bean id="greeting" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="proxyInterfaces">
            <value>HelloWorld</value>
        </property>
        <property name="interceptorNames">
            <list>
                <value>myInterceptor</value>
                <value>helloworld</value>
            </list>
        </property>
    </bean>
</beans>




代码:

/**
 * User: <a href="mailto:xxxxxx@utstar.com">xxxxx</a>
 * Date: Dec 17, 2003
 * Time: 8:17:19 PM
 * all rights reserved by utstarcom
 */
public interface HelloWorld {
    void sayHello(int age,String name);
}

 

         come from http://spring.jactiongroup.net/viewtopic.php?t=144

MAC OS X socket 1 [a simple example]

MAC OS X socket 1 [a simple example] Server Code C: // // main.c // unix_socket_very_simple_ser...
  • u013317006
  • u013317006
  • 2014年07月04日 10:22
  • 799

SpringBoot AOP统一处理请求日志

SpringBoot AOP统一处理请求日志有一天,项目经理老吴对小吴说:现在有这样一个需求:在SpringBootDemo01项目上的每个方法执行之前之后各打印一句话,例如:方法执行前打印如下内容:...
  • u010412719
  • u010412719
  • 2017年04月15日 11:01
  • 896

Spring 4 学习笔记5:Spring AOP

面向切面编程(Aspect Oriented Programming)是Spring的重要内容,本文主要介绍AOP的概念,以及一些主要用法。...
  • w1196726224
  • w1196726224
  • 2016年01月18日 11:35
  • 745

Spring的AOP配置 第一个AOP例子

Spring的AOP配置 1.先写一个普通类: package com.spring.aop; public class Common {  public void execute...
  • jQuerys
  • jQuerys
  • 2015年08月28日 18:23
  • 662

spring boot aop的使用

spring boot aop的使用 1.aop的官网介绍 AOP concepts Let us begin by defining some central AOP concepts and...
  • long290046464
  • long290046464
  • 2017年08月05日 19:04
  • 723

A simple JNI example

tested on CentOS 5.8 Step 1: declare a native function in the java source file  public class Add...
  • Andrew659
  • Andrew659
  • 2012年11月03日 22:16
  • 616

Simple example for Linq To XML

The XML Source file is:xml version="1.0" encoding="GB2312"?>Books>  Book genre="Mystery" publication...
  • diandian82
  • diandian82
  • 2008年12月12日 11:36
  • 758

a simple example of Ado.net

Imports System.Data.SqlClientImports System.DataImports System.Data.CommonPublic Class Form1    Inhe...
  • cyberpeng
  • cyberpeng
  • 2006年12月24日 22:24
  • 480

A simple makefile example

The code is shown below: edit : main.o kbd.o command.omain.o : main.c defs.h         cc -c main. ...
  • edward_chan
  • edward_chan
  • 2010年02月09日 21:12
  • 512

Java: Simple HTTPUrlConnection example

Normal 0 MicrosoftInternetExplorer4
  • dingxy
  • dingxy
  • 2010年01月28日 16:06
  • 4217
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:AOP Simple example
举报原因:
原因补充:

(最多只允许输入30个字)