Java框架 Sping AOP配置

AOP即 Aspect Oriental Program 面向切面编程。

在面向切面编程的思想里面,把功能分为核心业务功能和辅助功能。所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务。所谓的辅助功能,比如性能统计,日志,事务管理等等。辅助功能在Spring的面向切面编程AOP思想里,即被定义为切面功能。
在面向切面编程AOP的思想里面,核心业务功能和切面功能分别独立进行开发,然后把切面功能和核心业务功能 "编织" 在一起,这就叫AOP。

一、XML方式配置AOP

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
		http://www.springframework.org/schema/context     
		http://www.springframework.org/schema/context/spring-context-3.0.xsd">
		
    <!-- 声明业务对象 -->
	<bean name="s" class="com.how2java.service.ProductService" />
    <!-- 声明日志切面 -->
    <bean id="loggerAspect" class="com.how2java.aspect.LoggerAspect" />	
    
    <aop:config>
    	<!-- 声明切入点,切入点的 id叫loggerCutPoint,用来标记这个切入点 -->
        <!-- expression表示满足expression中的方法调用之后,就会进行切面操作,即触发了切面 -->
        <aop:pointcut id="loggerCutpoint"
            expression = "execution(* com.how2java.service.ProductService.*(..))"/>

        <!-- 定义一个切面,所谓切面,就是一个类中的方法而已。 --> 
        <!-- id指切面的名字,ref指方法所在的类,method代表的是方法的名字 -->
        <!-- pointcut-ref="loggerCutpoint" 表示这个切面是和上面的切点关联起来的 -->
        <!-- 只要上面的切点被触发,就会到这里来执行一些辅助功能。 -->
        <!-- Around:在方法执行之前与之后执行-->     
        <aop:aspect id="logAspect" ref="loggerAspect">
            <aop:around pointcut-ref="loggerCutpoint" method="log"/>
        </aop:aspect>
    </aop:config>
  
</beans>

一个切点可以关联多个切面,一个切面只能关联一个方法。

若有多个切面,使用xml配置的时候,在定义切面时候,可以加上order表示先后顺序,越小越先执行 <aop:aspect id="aspect1" ref="Handler1" order="1"> <aop:aspect id="aspect2" ref=“Handler2" order="2"> 要是不指定order,就按定义的先后顺序来执行。

二、注解方式配置AOP

1. 业务类配置

在业务类前注明@Component,将业务类交由Spring管理。

@Component("s"),表示这个业务类的Bean名字为 s。

2. 切面配置

package com.how2java.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LoggerAspect {
	@Around(value = "execution(* com.how2java.service.ProductService.*(..))")
	public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("start log:" + joinPoint.getSignature().getName());
        Object object = joinPoint.proceed();
        System.out.println("end log:" + joinPoint.getSignature().getName());
        return object;
    }
}

@Aspect 注解表示这是一个切面。
@Component 表示这是一个bean,由Spring进行管理。
@Around(value = "execution(* com.how2java.service.ProductService.*(..))") 表示对com.how2java.service.ProductService 这个类中的所有方法进行切面操作。

3. XML文件配置

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
		http://www.springframework.org/schema/context     
		http://www.springframework.org/schema/context/spring-context-3.0.xsd">
		
	<context:component-scan base-package="com.how2java.aspect"/>
    <context:component-scan base-package="com.how2java.service"/>
    <aop:aspectj-autoproxy />
    
</beans>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值