【spring】aop切面通知,日志处理
1.spring的切面编程
概念原理可以看这里:http://blog.csdn.net/moreevan/article/details/11977115
2.所需要的jar包
maven引入jar包(aspectj+aopalliance+cglib+spring):另外还有log日志jar包
<properties>
<spring.version>4.1.6.RELEASE</spring.version>
<aspectj.version>1.8.4</aspectj.version>
<aopalliance.version>1.0</aopalliance.version>
<cglib.version>3.1</cglib.version>
</properties> <dependencies>
<!--Spring 系列-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency> <!--log4j 2-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.4.1</version>
</dependency> <!--aspect,织入库,Spring依赖-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency> <!--aspect库,Spring依赖-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency> <!-- aop联盟定义的一组关于AOP的公共接口,Spring Aop依赖它 -->
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>${aopalliance.version}</version>
</dependency> <!--Cglib代理,Spring依赖-->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>${cglib.version}</version>
</dependency>
</dependencies>
3.自己写的hello World(不知道这里写的@Before前置通知并没有运行,还请懂得指点一下)
编写一个切面bean用于管理切面(基于注解) HelloWorldAspact :
package com.raipeng.work.spring.aspact; import org.apache.logging.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component; /**
* Created by 111 on 2015/11/21.
*/
@Aspect
@Component
public class HelloWorldAspact { //使用的是log4j2
private Logger logger = org.apache.logging.log4j.LogManager.getLogger(HelloWorldAspact.class); @Pointcut("execution(* com.raipeng.work.spring.model.HelloWorldServiceImp.*(..))")
private void anyMethod(){
} @Before("anyMethod()")
public void Advice(JoinPoint joinPoint){
System.out.println("this is before Advice");
} @Around("anyMethod()")
public void aroundAdvice(ProceedingJoinPoint joinPoint){
System.out.println("this is around Advice");
logger.info("the method {}.{}() run", joinPoint.getTarget().getClass().getSimpleName(), joinPoint.getSignature().getName());
} @After("anyMethod()")
public void afterAdvice(){
System.out.println("this is after Advice");
} @AfterReturning("anyMethod()")
public void afterReturningAdvice(){
System.out.println("this is afterReturning Advice");
}
}
在spring的配置文件中配置如下(必须加上<aop:aspectj-autoproxy/>):
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd"> <aop:aspectj-autoproxy/>
<context:component-scan base-package="com.raipeng.work.spring.aspact"/>
<bean id="helloWorld" class="com.raipeng.work.spring.model.HelloWorldServiceImp">
</bean> <!--<bean id="helloWorldAspact" class="com.raipeng.work.spring.aspact.HelloWorldAspact"></bean>--扫描包加入了@Component就不需>
</beans>
需要aop监控的类的接口 HelloWorldService
package com.raipeng.work.spring.model; /**
* Created by 111 on 2015/11/21.
*/
public interface HelloWorldService {
void hello(String name);
}
上面接口的实现类 HelloWorldServiceImp:
package com.raipeng.work.spring.model; /**
* Created by 111 on 2015/10/26.
*/
public class HelloWorldServiceImp implements HelloWorldService { @Override
public void hello(String name){
System.out.println(name+",hello!");
} }
测试方法如下:
package com.raipeng.work.spring.test; import com.raipeng.work.spring.model.HelloWorldService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Created by 111 on 2015/11/19.
*/
public class MainTest {
public static void main(String[]args){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorldService helloWorld = (HelloWorldService)applicationContext.getBean("helloWorld");
helloWorld.hello("xl");
}
}
控制台信息:
"D:\Program Files\Java\jdk1.7.0_13\bin\java" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:60621,suspend=y,server=n -Dfile.encoding=UTF-8 -classpath "D:\Program Files\Java\jdk1.7.0_13\jre\lib\charsets.jar;D:\Program Files\Java\jdk1.7.0_13\jre\lib\deploy.jar;D:\Program Files\Java\jdk1.7.0_13\jre\lib\javaws.jar;D:\Program Files\Java\jdk1.7.0_13\jre\lib\jce.jar;D:\Program Files\Java\jdk1.7.0_13\jre\lib\jfr.jar;D:\Program Files\Java\jdk1.7.0_13\jre\lib\jfxrt.jar;D:\Program Files\Java\jdk1.7.0_13\jre\lib\jsse.jar;D:\Program Files\Java\jdk1.7.0_13\jre\lib\management-agent.jar;D:\Program Files\Java\jdk1.7.0_13\jre\lib\plugin.jar;D:\Program Files\Java\jdk1.7.0_13\jre\lib\resources.jar;D:\Program Files\Java\jdk1.7.0_13\jre\lib\rt.jar;D:\Program Files\Java\jdk1.7.0_13\jre\lib\ext\access-bridge-64.jar;D:\Program Files\Java\jdk1.7.0_13\jre\lib\ext\dnsns.jar;D:\Program Files\Java\jdk1.7.0_13\jre\lib\ext\jaccess.jar;D:\Program Files\Java\jdk1.7.0_13\jre\lib\ext\localedata.jar;D:\Program Files\Java\jdk1.7.0_13\jre\lib\ext\sunec.jar;D:\Program Files\Java\jdk1.7.0_13\jre\lib\ext\sunjce_provider.jar;D:\Program Files\Java\jdk1.7.0_13\jre\lib\ext\sunmscapi.jar;D:\Program Files\Java\jdk1.7.0_13\jre\lib\ext\zipfs.jar;D:\cxlwork\STSworkspace\work-test\target\classes;D:\app\repository\org\springframework\spring-core\4.1.6.RELEASE\spring-core-4.1.6.RELEASE.jar;D:\app\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;D:\app\repository\org\springframework\spring-context\4.1.6.RELEASE\spring-context-4.1.6.RELEASE.jar;D:\app\repository\org\springframework\spring-context-support\4.1.6.RELEASE\spring-context-support-4.1.6.RELEASE.jar;D:\app\repository\org\springframework\spring-beans\4.1.6.RELEASE\spring-beans-4.1.6.RELEASE.jar;D:\app\repository\org\springframework\spring-expression\4.1.6.RELEASE\spring-expression-4.1.6.RELEASE.jar;D:\app\repository\org\springframework\spring-aspects\4.1.6.RELEASE\spring-aspects-4.1.6.RELEASE.jar;D:\app\repository\org\springframework\spring-aop\4.1.6.RELEASE\spring-aop-4.1.6.RELEASE.jar;D:\app\repository\org\springframework\spring-jdbc\4.1.6.RELEASE\spring-jdbc-4.1.6.RELEASE.jar;D:\app\repository\org\springframework\spring-orm\4.1.6.RELEASE\spring-orm-4.1.6.RELEASE.jar;D:\app\repository\org\springframework\spring-tx\4.1.6.RELEASE\spring-tx-4.1.6.RELEASE.jar;D:\app\repository\org\springframework\spring-web\4.1.6.RELEASE\spring-web-4.1.6.RELEASE.jar;D:\app\repository\org\springframework\spring-webmvc\4.1.6.RELEASE\spring-webmvc-4.1.6.RELEASE.jar;D:\app\repository\mysql\mysql-connector-java\5.1.9\mysql-connector-java-5.1.9.jar;D:\app\repository\org\apache\logging\log4j\log4j-api\2.4.1\log4j-api-2.4.1.jar;D:\app\repository\org\apache\logging\log4j\log4j-core\2.4.1\log4j-core-2.4.1.jar;D:\app\repository\org\aspectj\aspectjweaver\1.8.4\aspectjweaver-1.8.4.jar;D:\app\repository\org\aspectj\aspectjrt\1.8.4\aspectjrt-1.8.4.jar;D:\app\repository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;D:\app\repository\cglib\cglib\3.1\cglib-3.1.jar;D:\app\repository\org\ow2\asm\asm\4.2\asm-4.2.jar;D:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.0.3\lib\idea_rt.jar" com.raipeng.work.spring.test.MainTest
Connected to the target VM, address: '127.0.0.1:60621', transport: 'socket'
十一月 21, 2015 3:02:45 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@648ee94b: startup date [Sat Nov 21 15:02:45 CST 2015]; root of context hierarchy
十一月 21, 2015 3:02:45 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
this is around Advice
2015-11-21 15:02:47.479 INFO [main][HelloWorldAspact.java:33] - the method HelloWorldServiceImp.hello() run
this is after Advice
this is afterReturning Advice
Disconnected from the target VM, address: '127.0.0.1:60621', transport: 'socket' Process finished with exit code 0
【spring】aop切面通知,日志处理的更多相关文章
- 利用Spring AOP切面对用户访问进行监控
开发系统时往往需要考虑记录用户访问系统查询了那些数据.进行了什么操作,尤其是访问重要的数据和执行重要的操作的时候将数记录下来尤显的有意义.有了这些用户行为数据,事后可以以用户为条件对用户在系统的访问和 ...
- Spring AOP 切面编程记录日志和接口执行时间
最近客户现在提出系统访问非常慢,需要优化提升访问速度,在排查了nginx.tomcat内存和服务器负载之后,判断是数据库查询速度慢,进一步排查发现是因为部分视图和表查询特别慢导致了整个系统的响应时间特 ...
- Spring笔记07(Spring AOP的通知advice和顾问advisor)
1.Spring AOP的通知advice 01.接口代码: package cn.pb.dao; public interface UserDao { //主业务 String add(); //主 ...
- Spring AOP 四大通知
Spring AOP 四大通知 Spring 3.X 以前 1.前置通知,实现 MethodBeforeAdvice 接口,重写 public void before(Method metho ...
- Spring AOP切面的时候参数的传递
Spring AOP切面的时候参数的传递 Xml: <?xml version="1.0" encoding="UTF-8"?> <beans ...
- spring AOP(切面) 表达式介绍
在 spring AOP(切面) 例子基础上对表达式进行介绍 1.添加接口删除方法 2.接口实现类 UserDaoServer 添加实现接口删除方法 3.测试类调用delUser方法 4. 输出结果截 ...
- 使用Spring AOP切面解决数据库读写分离
http://blog.jobbole.com/103496/ 为了减轻数据库的压力,一般会使用数据库主从(master/slave)的方式,但是这种方式会给应用程序带来一定的麻烦,比如说,应用程序如 ...
- Spring AOP高级——源码实现(2)Spring AOP中通知器(Advisor)与切面(Aspect)
本文例子完整源码地址:https://github.com/yu-linfeng/BlogRepositories/tree/master/repositories/Spring%20AOP%E9%A ...
- Spring AOP 切面实现操作日志
创建接口注解日志类 package com.fh.service.logAop; /** * Created by caozengling on 2018/7/21. */ import java.l ...
随机推荐
- Spring Boot 异常处理
Spring Boot 异常处理 本节介绍一下 Spring Boot 启动时是如何处理异常的?核心类是 SpringBootExceptionReporter 和 SpringBootExcepti ...
- Springboot异常:org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController'
今天本菜鸟编写程序时,遇到了一个异常. org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating ...
- Linux关闭防火墙命令
下面是red hat/CentOs7关闭防火墙的命令! 1:查看防火状态 systemctl status firewalld service iptables status 2:暂时关闭防火墙 s ...
- 关于python的感想和turtle作图
第一次知道有python这个东西是在去年七月份,就是一堆编程语言,当时我想它应该离我很远,也没对它有什么兴趣.直到在三月份的某一天我真正接触了它,其实也不是我主动,环境下学期必须学这东西.我有它的必修 ...
- spring jar包解读(转)
作者:http://www.cnblogs.com/leehongee/archive/2012/10/01/2709541.html spring.jar 是包含有完整发布模块的单个jar 包.但是 ...
- nginx简介与配置
nginx简介 nginx(发音同engine x)是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like协议下发行. nginx由俄罗斯的程序 ...
- springIOplatform
因此Spring IO Platform应运而生,只要项目中引入了它,外部集成时依赖关系无需版本号 <dependency> <groupId>org.springframew ...
- Mac 系统搭建ThinkPHP3.2
PHP3.2完整包目录 拷贝两个文件 index.php 和ThinkPHP目录到服务器目录中,我已经设置服务器目录与eclipse工作空间为同一个 创建TestThinkPHP 项目 Eclipse ...
- 转发:已知rsa的模数和指数 生成pem公钥文件
1.安装cryptographysudo pip3 install cryptography 2.代码 #coding:utf8# pupulate-pub-key-v3.py#from crypto ...
- 第一次OO总结
作业1——多项式加减法 看到这个名字就开始瑟瑟发抖了,毕竟一年前用C语言让我写这么一个程序都很头疼,什么堆栈啊还有结构都稀里糊涂的,更别说用一个完全没接触过的语言来完成最简单的一次作业.像我这样越老心 ...