您的位置:首页 > 编程语言 > Java开发

Spring AOP实现日志服务

2016-02-24 19:08 591 查看
Spring AOP实现日志服务

pom.xml需要的jar

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.4</version>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context</artifactId>
	<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-core</artifactId>
	<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-beans</artifactId>
	<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-aop</artifactId>
	<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-web</artifactId>
	<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
	<groupId>cglib</groupId>
	<artifactId>cglib-nodep</artifactId>
	<version>3.2.0</version>
</dependency>
<dependency>
	<groupId>org.aspectj</groupId>
	<artifactId>aspectjweaver</artifactId>
	<version>1.8.8</version>
</dependency>
<dependency>
	<groupId>aopalliance</groupId>
	<artifactId>aopalliance</artifactId>
	<version>1.0</version>
</dependency>
<dependency>
	<groupId>commons-collections</groupId>
	<artifactId>commons-collections</artifactId>
	<version>3.2.2</version>
</dependency>
<dependency>
	<groupId>commons-logging</groupId>
	<artifactId>commons-logging</artifactId>
	<version>1.2</version>
</dependency>

LogBeforeAdvice.java

package com.blog.csdn.net.unix21;

import java.lang.reflect.Method;
import java.util.logging.Level;
import org.springframework.aop.MethodBeforeAdvice;
import java.util.logging.Logger;

/**
 *
 * @author http://blog.csdn.net/unix21/  */
public class LogBeforeAdvice implements MethodBeforeAdvice {
    private Logger logger=Logger.getLogger(this.getClass().getName());
    public void before(Method method,Object[] args,Object target) throws Throwable{
        logger.log(Level.INFO,"日志信息>>>method starts..."+method);
    }
}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="   
           http://www.springframework.org/schema/beans   
           http://www.springframework.org/schema/beans/spring-beans-4.0.xsd   
           http://www.springframework.org/schema/context   
           http://www.springframework.org/schema/context/spring-context-4.0.xsd  
           http://www.springframework.org/schema/mvc   
           http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd            http://www.springframework.org/schema/aop 
	       http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> 
<bean id="LogBeforeAdvice" class="blog.csdn.net.unix21.LogBeforeAdvice"/>
<bean id="HelloSpeaker" class="blog.csdn.net.unix21.HelloSpeake"/>
<bean id="HelloProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="proxyInterfaces" value="blog.csdn.net.unix21.IHello"/>
    <property name="target" ref="HelloSpeaker"/>
    <property name="interceptorNames">
        <list>
            <value>LogBeforeAdvice</value>
        </list>
    </property>
</bean>
</beans>

接口
IHello.java

package blog.csdn.net.unix21;

public interface IHello {
public void hello(String name);
}

HelloSpeaker.java

package blog.csdn.net.unix21;

public class HelloSpeaker implements IHello {
	public void hello(String name){
	System.out.println("Hello, "+name);
	}
}


测试Before Advice
ApplicationContext context=new ClassPathXmlApplicationContext("beans-config.xml");
  IHello h=(IHello)context.getBean("helloProxy");
  h.hello("blog.csdn.net.unix21")


运行成功使用AOP的方式打印日志信息:



参考:《Spring 2.0技术手册》
Spring 3.1编写AOP时需要导入的倚赖jar包汇总

菜鸟学SSH(七)——Spring jar包详解
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: