前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring AOP与IOC

Spring AOP与IOC

作者头像
Java架构师必看
发布2020-04-22 11:30:13
3770
发布2020-04-22 11:30:13
举报
文章被收录于专栏:Java架构师必看

Spring AOP实现日志服务

pom.xml需要的jar

代码语言:javascript
复制
<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>

1.Before Advice

Before Advice会在目标方法执行之前被调用,可以通过实现org.springframework.aop.MethodBeforeAdvice接口实现。

LogBeforeAdvice.java

代码语言:javascript
复制
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);
    }
}

beans-config.xml配置

代码语言:javascript
复制
<?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

代码语言:javascript
复制
package blog.csdn.net.unix21;

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

HelloSpeaker.java

代码语言:javascript
复制
package blog.csdn.net.unix21;

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

测试Before Advice

代码语言:javascript
复制
  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包详解

2.After Advice

After Advice会在目标方法执行之后被调用,可以通过实现org.springframework.aop.AfterReturningAdvice接口来实现

代码语言:javascript
复制
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.aop.AfterReturningAdvice;

public class LogAfterAdvice  implements AfterReturningAdvice  {
private Logger logger=Logger.getLogger(this.getClass().getName());
    public void afterReturning(Object object, Method method, Object[] args, Object target) throws Throwable {
       logger.log(Level.INFO,"日志信息<<<method ends..."+method);
    }  
}

修改beans-config.xml配置

代码语言:javascript
复制
<?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>
			<value>LogAfterAdvice</value>
        </list>
    </property>
</bean>
</beans>

成功的执行了LogAfterAdvice

本文由来源 21aspnet,由 system_mush 整理编辑,其版权均为 21aspnet 所有,文章内容系作者个人观点,不代表 Java架构师必看 对观点赞同或支持。如需转载,请注明文章来源。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

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

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
日志服务
日志服务(Cloud Log Service,CLS)是腾讯云提供的一站式日志服务平台,提供了从日志采集、日志存储到日志检索,图表分析、监控告警、日志投递等多项服务,协助用户通过日志来解决业务运维、服务监控、日志审计等场景问题。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档