Spring AOP 配置一般有两种
一、注解式的
1.在spring的配置文件中加入
<!-- Annotation aop -->
<!--需要的代理对象-- >
<bean id="logInfoAspect" class="com.cctv.aspect.LogInfoAspect"></bean>
<!--注解方式必要的配置-->
<aop:aspectj-autoproxy proxy-target-class="true"/>
2.在代理对象LogInfoAspect类中
@Aspect
public class LogInfoAspect {
@Autowired
private HttpServletRequest request;
@Resource
private BspDalylogDao dao;
//日志内容模板
private final String CONTENT_TEMPLATE = "@@agentId@@@@date@@调用 @@object@@@@method@@方法";
@Pointcut("execution(* com.cctv.service..*.del*(..))||execution(* com.cctv.service..*.save*(..))||execution(* com.cctv.service..*.add*(..))||execution(* com.cctv.service..*.update*(..))")
private void myMethod(){};
@Pointcut("execution(* com.hwacreate.service..*.del*(..))||execution(* com.cctv.service..*.save*(..))||execution(* com.cctv.service..*.add*(..))||execution(* com.cctv.service..*.update*(..))")
private void throwMethod(){};
@After("myMethod()")
public void addLogInfo(JoinPoint joinPoint){
BspDailyLogState dialLog = dao.findDialLog();
String state = dialLog.getState();
if(state.equals("1")){
//Object[] args = joinPoint.getArgs();
Object target = joinPoint.getTarget();
String method = joinPoint.getSignature().getName();
String userName = "zyc";
String states = "2";
String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
String remark = CONTENT_TEMPLATE.replace("@@agentId@@", userName+"于")
.replace("@@date@@", date)
.replace("@@object@@", (target == null ? "" : target.getClass().getName()+"的"))
.replace("@@method@@", method);
this.insertlog(date, remark, userName, states);
}
}
/**
* 目标方法抛出异常后
* @param jp
* @param ex 异常信息
*/
@AfterThrowing(pointcut="throwMethod()", throwing="ex")
public void afterThrowing(JoinPoint jp,Exception ex) {
BspDailyLogState dialLog = dao.findDialLog();
String state = dialLog.getState();
if(state.equals("1")){
Object[] args = jp.getArgs();
Object target = jp.getTarget();
String method = jp.getSignature().getName();
String userName = "zyc";
String states="2";
String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
String remark = CONTENT_TEMPLATE.replace("@@agentId@@", userName + "于").replace("@@date@@", date)
.replace("@@object@@", (target == null ? "" : target.getClass().getName() + "的"))
.replace("@@method@@", method);
remark += ",错误:"+ex.getMessage();
this.insertlog(date, remark, userName, states);
}
}
//插入数据
public void insertlog(String date,String remark,String userName,String state){
BspDailylog dia = new BspDailylog();
dia.setCreateTime(date);
dia.setRemark(remark);
dia.setStatus(state);
dia.setUserName(userName);
dao.save(dia);
}
二、xml方式的配置,比较常用,也是在spring中配置
<!-- 日志监控AOP配置xml
<bean id="logInfoAspect" class="com.hwacreate.aspect.LogInfoAspect"></bean>
<aop:config proxy-target-class="true">
<aop:pointcut expression="execution(* com.cctv.service..*.del*(..))||execution(* com.cctv.service..*.save*(..))||execution(* com.cctv.service..*.add*(..))||execution(* com.cctv.service..*.update*(..))" id="logPointCut"/>
<aop:aspect id="logAspect" ref="logInfoAspect">
<aop:after method="addLogInfo" pointcut-ref="logPointCut" />
</aop:aspect>
</aop:config>-->
分享到:
相关推荐
在Spring XML配置文件中,我们可以定义以下元素来实现AOP配置: - `<aop:config>`:声明AOP配置。 - `<aop:pointcut>`:定义切入点表达式,例如`execution(* com.example.service.*.*(..))`表示匹配...
Spring AOP 配置事务方法 Spring AOP(Aspect-Oriented Programming,面向方面编程)是一种编程范式,它允许开发者在不修改源代码的情况下,增强和修改应用程序的行为。 Spring AOP 提供了一种灵活的方式来实现事务...
以下是一个简单的Spring AOP配置文件示例: ```xml <aop:config> <aop:aspect id="loggingAspect" ref="loggingAdvice"> <aop:before method="beforeMethod" pointcut-ref="businessMethods"/> <aop:after-...
**Spring AOP配置实例** Spring AOP(Aspect Oriented Programming,面向切面编程)是Spring框架的核心组件之一,它提供了一种在不修改源代码的情况下,对程序进行功能增强的技术。AOP允许开发者定义“切面”,这些...
### Spring之AOP配置文件详解 #### 一、前言 在Java开发中,Spring框架因其强大的功能和灵活的配置而被广泛应用于企业级应用的开发。其中,面向切面编程(Aspect Oriented Programming,简称AOP)是Spring框架的...
本例中,我们关注的是如何利用Spring AOP配置一个简单的切面,该切面在`DukePerformer`类的`perform`方法执行前后,插入观众的行为,如找座位、关手机、鼓掌以及不满意的反应。通过这种方式,我们将这些行为抽象为...
### Spring AOP配置详解 #### 一、Spring AOP配置概览 面向切面编程(Aspect-Oriented Programming,简称AOP)是Spring框架的重要特性之一,它通过将业务逻辑中的横切关注点(Cross-cutting Concerns)与核心业务...
2. **注解配置**:Spring 2.5引入了基于注解的AOP配置,可以在切面类上使用@Aspect注解,@Before、@After、@AfterReturning、@AfterThrowing和@Around定义通知,@Pointcut定义切点。例如: ```java @Aspect ...
**Spring AOP配置与管理** 在Java开发中,Spring框架以其强大的功能和灵活性深受开发者喜爱。其中,AOP(Aspect-Oriented Programming,面向切面编程)是Spring框架的一个重要特性,它允许开发者定义“切面”,即...
**Spring AOP 配置小节** 在Java开发中,Spring框架因其强大的功能和灵活性而备受推崇,其中AOP(面向切面编程)是其重要特性之一。AOP允许我们把关注点分离到非核心业务逻辑之外,如日志、事务管理等,使得代码...
Spring AOP 配置 Spring AOP(Aspect-Oriented Programming)是一种面向方面编程技术,通过将关注点模块化,提高了系统的可维护性和可扩展性。下面将详细介绍 Spring AOP 的配置和实现。 一、基本概念 * 切面...
Spring aop 配置 Spring aspect 配置 Spring advisor 配置 Spring pointcut 配置
Spring AOP配置 Spring AOP的配置可以通过XML或注解方式进行: - **XML配置**: - 在`<aop:config>`标签内定义切面,`<aop:pointcut>`定义切入点,`<aop:advisor>`定义通知。 - `<aop:aspect>`标签用于定义完整...
2. **配置Spring容器**:在Spring的XML配置文件中启用AOP支持,通过`<aop:config>`标签开启,并可以定义切点(pointcut)和通知(advice)。 3. **定义切点**:使用`<aop:pointcut>`定义要拦截的方法或类。切点...
在使用Spring AOP时,我们可以通过XML配置或注解的方式来定义切面。例如,可以使用`@Aspect`注解定义一个切面类,`@Before`、`@After`等注解来声明通知,`@Pointcut`定义切点表达式。 在实际开发中,Spring AOP广泛...
Spring AOP配置 在Spring XML配置文件中,可以声明切面和通知,如下: ```xml <aop:config> <aop:aspect id="loggingAspect" ref="loggingService"> <aop:before method="logBefore" pointcut="execution(* ...
在提供的压缩包文件"springAOP"中,可能包含了以下内容: - **切面类(Aspect Class)**:包含切点和通知的Java类,可能使用了`@Aspect`注解。 - **目标类(Target Class)**:被AOP代理的对象,通常包含业务逻辑。...