切面类:
package com.aop;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class AllLogAdvice {
private Logger logger = Logger.getLogger(AllLogAdvice.class);
// @Pointcut("execution(* com.test.spring.aop.pointcutexp..JoinPointObjP2.*(..))")
// @Pointcut("within(com.test.spring.aop.pointcutexp..*)")
// @Pointcut("this(com.test.spring.aop.pointcutexp.Intf)")
// @Pointcut("target(com.test.spring.aop.pointcutexp.Intf)")
// @Pointcut("@within(org.springframework.transaction.annotation.Transactional)")
// @Pointcut("@annotation(org.springframework.transaction.annotation.Transactional)")
// @Pointcut("args(String)")
@Pointcut("execution(* com.service.UserService.add*(..)) || execution(* com.service.UserService.delete*(..))")
public void pointcut1() {
// 定义一个pointcut,下面用Annotation标注的通知方法可以公用这个pointcut
System.out.println("************erery invoke");
}
// 前置通知
// 拦截参数为一个String类型的方法
@Before("pointcut1() && args(temp)")
public void myBeforeAdvice(String temp) {// 如果需要知道拦截的方法的信息,也可以需添加JoinPoint参数
String logInfoText = "这是前置通知" + temp;
// 将日志信息写入配置的文件中
logger.info(logInfoText);
}
// 后置通知
// 拦截 返回类型为String 的方法
@AfterReturning(pointcut = "pointcut1()", returning = "result")
public void myAfterReturnAdvice(String result) {
logger.info("这是后置通知 " + " result: " + result);
}
// 最终置通知
@After("execution(* com.service.**.*(..))")
public void doAfter() {
logger.info("这是最终通知");
}
// 异常通知
@AfterThrowing(pointcut = "pointcut1()", throwing = "e")
public void myThrowingAdvice(JoinPoint jionpoint, Exception e) {
// 获取被调用的类名
String targetClassName = jionpoint.getTarget().getClass().getName();
// 获取被调用的方法名
String targetMethodName = jionpoint.getSignature().getName();
// 日志格式字符串
String logInfoText = "异常通知:执行" + targetClassName + "类的"
+ targetMethodName + "方法时发生异常";
// 将日志信息写入配置的文件中
logger.info(logInfoText);
}
// 环绕通知
// @Around(value="pointcut1()")
@Around("pointcut1()")
public Object myAroundAdvice(ProceedingJoinPoint jionpoint)
throws Throwable {
// 获取被调用的方法名
String targetMethodName = jionpoint.getSignature().getName();
//Object o = jionpoint.proceed();
String logInfoText = "这是环绕通知:" + targetMethodName;
logger.info(logInfoText);
Object o = jionpoint.proceed();//注意写到这儿的话,环绕通知和其它通知的顺序
return o;
}
}
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 开启注解扫描 -->
<context:component-scan base-package="com.aop"/>
<!-- 开启aop注解方式,此步骤s不能少,这样java类中的aop注解才会生效 -->
<aop:aspectj-autoproxy/>
<bean id="myUserService" class="com.service.UserService"></bean>
</beans>
业务接口和实现类:
package com.service;
public class UserService implements IUserService {
public int addUser(String name, int age) {
//省略诸如操作数据库等复杂的逻辑操作
System.out.println("add user "+ name +" successfully");
return 1;
}
public void deleteUser(String name) {
//省略诸如操作数据库等复杂的逻辑操作
System.out.println("deleted one user named " + name);
//throw new RuntimeException("这是特意抛出的异常信息!");
}
}
package com.service;
public interface IUserService {
public int addUser(String name, int age);
public void deleteUser(String name);
}
测试类:
package com.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.service.IUserService;
public class MainTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("aop.xml");
IUserService userService = (IUserService) context
.getBean("myUserService");
userService.addUser("ton", 56);
userService.deleteUser("ton");
}
}
最后附上日志文件:
log4j.rootLogger=info,stdout,info,debug,error
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%-5p] [%d{HH:mm:ss}] %c - %m%n
log4j.logger.info=info
log4j.appender.info=org.apache.log4j.DailyRollingFileAppender
log4j.appender.info.layout=org.apache.log4j.PatternLayout
log4j.appender.info.layout.ConversionPattern=[%-5p] [%d{HH:mm:ss}] %c - %m%n
log4j.appender.info.datePattern='.'yyyy-MM-dd
log4j.appender.info.Threshold = INFO
log4j.appender.info.append=true
log4j.appender.info.File=${webApp.root}/WEB-INF/logs/info.log
log4j.logger.debug=debug
log4j.appender.debug=org.apache.log4j.DailyRollingFileAppender
log4j.appender.debug.layout=org.apache.log4j.PatternLayout
log4j.appender.debug.layout.ConversionPattern=[%-5p] [%d{HH:mm:ss}] %c - %m%n
log4j.appender.debug.datePattern='.'yyyy-MM-dd
log4j.appender.debug.Threshold = DEBUG
log4j.appender.debug.append=true
log4j.appender.debug.File=${webApp.root}/WEB-INF/logs/debug.log
log4j.logger.error=error
log4j.appender.error=org.apache.log4j.DailyRollingFileAppender
log4j.appender.error.layout=org.apache.log4j.PatternLayout
log4j.appender.error.layout.ConversionPattern=[%-5p] [%d{HH:mm:ss}] %c - %m%n
log4j.appender.error.datePattern='.'yyyy-MM-dd
log4j.appender.error.Threshold = ERROR
log4j.appender.error.append=true
log4j.appender.error.File=${webApp.root}/WEB-INF/logs/error.log
相关推荐
spring-aop注解用到的jar包,解压后直接导入即可使用。
commons-logging-1.1.3,spring-test-4.0.0.RELEASE,spring-aop-4.0.0.RELEASE,spring-aspects-4.0.0.RELEASE
spring-aop-4.2.4.RELEASE,spring注解包,代码里面特殊标记,使用注解可以完成功能,相当于语法糖操作
演示了spring对aop的支持,包括注解方式、基于xml方式。
spring-aop-4.0.0.RELEASE工具类,用于spring注解装配bean
这里zip压缩包囊括了学习Spring过程中用到的所有的jar包; 有: ...spring-web-4.1.2.RELEASE.jar //aop注解需要 对于Spring环境的插件,你们可以到Eclipase中的help>Eclipse marketplace...去搜索安装
基于注解实现SpringAop基于注解实现SpringAop基于注解实现SpringAop
代码中有注解实例,spring框架实例,aop切片实例
使用Spring的注解方式实现AOP的细节
Spring注解扫描需要的包。Spring 经过十多年的快速发展和更新,以其独特的创新带来了大量的拥趸者,在选择方面还带来了更多选择!
2、能够清楚的知道如何用spring aop实现自定义注解以及注解的逻辑实现 (需要知道原理的请看spring aop源码,此处不做赘述) 3、可在现有源码上快速进行功能扩展 4、spring boot,mybatis,druid,spring aop的使用
注解方法实现aop
Spring Mvc AOP通过注解方式拦截controller等实现日志管理
spring aop 通过注解实现的项目,这只是一个简单的例子。运行环境也简单。
spring aop 采用注解定义以及xml定义,在java四层结构中无缝实现
关于AOP注解前置通知、后置通知、返回通知、异常通知的注解注释及应用
redis客户端集成spring实现aop方式,方便在程序中使用spring的时候更加方便的使用redis内存数据库, 采用aop的方式主要是为了不去污染业务代码,达到业务逻辑更加清晰易于阅读,在使用过程中可以设置 key的有效时间,...
spring boot AOP注解方式实现多数据源整合(从零开始搭建框架到配置多数据源实践小例子)
SSM整合-用springaop-demo01实现了注解AOP,SSM-MybatisOneForOne-demo01实现了
最近公司要求和别的公司对接,但是不想破坏原有的架构,就只有用切面了,百度了下都不全面,就自己写了个小demo,只要导入spring4.2.3的jar就直接可以用,注解实现spingaop机制,1分意思下