SSH复习笔记---Spring IOC&AOP

一、原理

        控制反转——Spring通过一种称作控制反转(IoC)的技术促进了松耦合。当应用了IoC,一个对象依赖的其它对象会通过被动的方式传递进来,而不是这个对象自己创建或者查找依赖对象。你可以认为IoC与JNDI相反——不是对象从容器中查找依赖,而是容器在对象初始化时不等对象请求就主动将依赖传递给它。
        面向切面——Spring提供了面向切面编程的丰富支持,允许通过分离应用的业务逻辑与系统级服务(例如审计(auditing)和事务(transaction)管理)进行内聚性的开发。应用对象只实现它们应该做的——完成业务逻辑——仅此而已。它们并不负责(甚至是意识)其它的系统级关注点,例如日志或事务支持。

二、IOC&AOP 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"
	xsi:schemaLocation="http://springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans3.0.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
	
	<!-- 声明bean-->
	<bean id="knight" class="com.springinaction.knights.BraveKnight">
		<constructor-arg ref="quest" />
	</bean>
	<bean id="quest"
		  class="com.springinaction.knights.SlayDragonQuest" />
	
	<bean id="minstrel"
		  class="com.springinaction.knights.Minstrel" />
		  
	<aop:config>
		<aop:aspect ref="minstrel"> 
			
			<aop:pointcut ref="embark"
					expression="execution(* *.embarkOnQuest(..))" <!-- 定义切面-->
			
			<aop:before pointcut-ref="embark"
						methord="singBeforeQuest"/><!-- 声明前置通知-->
			
			<aop:after pointcut-ref="embark"
						methord="singAfterQuest" /><!-- 声明后置通知-->
			
		</aop:aspect>
	</aop:config>

三、IOC&AOP 注解配置

AOP

/**
 * 测试after,before,around,throwing,returning Advice.
 * @author Admin
 *
 */
@Aspect
public class AspceJAdvice {
 
    /**
     * Pointcut
     * 定义Pointcut,Pointcut的名称为aspectjMethod(),此方法没有返回值和参数
     * 该方法就是一个标识,不进行调用
     */
    @Pointcut(execution(* find*(..)))
    private void aspectjMethod(){};
     
    /** 
     * Before
     * 在核心业务执行前执行,不能阻止核心业务的调用。
     * @param joinPoint 
     */ 
    @Before(aspectjMethod())  
    public void beforeAdvice(JoinPoint joinPoint) {  
        System.out.println(-----beforeAdvice().invoke-----);
        System.out.println( 此处意在执行核心业务逻辑前,做一些安全性的判断等等);
        System.out.println( 可通过joinPoint来获取所需要的内容);
        System.out.println(-----End of beforeAdvice()------);
    }
     
    /** 
     * After 
     * 核心业务逻辑退出后(包括正常执行结束和异常退出),执行此Advice
     * @param joinPoint
     */
    @After(value = aspectjMethod())  
    public void afterAdvice(JoinPoint joinPoint) {  
        System.out.println(-----afterAdvice().invoke-----);
        System.out.println( 此处意在执行核心业务逻辑之后,做一些日志记录操作等等);
        System.out.println( 可通过joinPoint来获取所需要的内容);
        System.out.println(-----End of afterAdvice()------);
    }  
 
    /** 
     * Around 
     * 手动控制调用核心业务逻辑,以及调用前和调用后的处理,
     * 
     * 注意:当核心业务抛异常后,立即退出,转向AfterAdvice
     * 执行完AfterAdvice,再转到ThrowingAdvice
     * @param pjp
     * @return
     * @throws Throwable
     */
    @Around(value = aspectjMethod())  
    public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable {  
        System.out.println(-----aroundAdvice().invoke-----);
        System.out.println( 此处可以做类似于Before Advice的事情);
         
        //调用核心逻辑
        Object retVal = pjp.proceed();
        System.out.println( 此处可以做类似于After Advice的事情);
        System.out.println(-----End of aroundAdvice()------);
        return retVal;
    }  
     
    /** 
     * AfterReturning 
     * 核心业务逻辑调用正常退出后,不管是否有返回值,正常退出后,均执行此Advice
     * @param joinPoint
     */
    @AfterReturning(value = aspectjMethod(), returning = retVal)  
    public void afterReturningAdvice(JoinPoint joinPoint, String retVal) {  
        System.out.println(-----afterReturningAdvice().invoke-----);
        System.out.println(Return Value:  + retVal); 
        System.out.println( 此处可以对返回值做进一步处理);
        System.out.println( 可通过joinPoint来获取所需要的内容);
        System.out.println(-----End of afterReturningAdvice()------);
    }
     
    /**
     * 核心业务逻辑调用异常退出后,执行此Advice,处理错误信息
     * 
     * 注意:执行顺序在Around Advice之后
     * @param joinPoint
     * @param ex
     */
    @AfterThrowing(value = aspectjMethod(), throwing = ex)  
    public void afterThrowingAdvice(JoinPoint joinPoint, Exception ex) {  
        System.out.println(-----afterThrowingAdvice().invoke-----);
        System.out.println( 错误信息:+ex.getMessage());
        System.out.println( 此处意在执行核心业务逻辑出错时,捕获异常,并可做一些日志记录操作等等);
        System.out.println( 可通过joinPoint来获取所需要的内容);
        System.out.println(-----End of afterThrowingAdvice()------);  
    }  
}

IOC

http://www.cnblogs.com/xdp-gacl/p/3495887.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值