Ioc与Aop

    Spring Ioc  控制反转 也成为依赖注入,是面向对象编程中的一种设计理念。      好处:降低程序代码中的耦合。

    所谓依赖 例如:在代码中通过局部变量。方法参数。返回值等建立的对于其他对象的调用关系,:例如   A类方法中 实例化B类的对象并调用其方法完成特定功能。 也就是A类依赖B类。

    若使用依赖注入来实现则 不需要实例化 new 对象。 在配置中完成即可。

    

<?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:p="http://www.springframework.org/schema/p"
	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/tx
     http://www.springframework.org/schema/tx/spring-tx.xsd
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-3.0.xsd">
		
		<bean id="userDao" class="cn.bdqn.dao.impl.UserDaoImpl"></bean>
		<bean id="userService" class="cn.bdqn.service.impl.UserDaoServiceImpl" p:dao-ref="userDao"></bean>
		
		
			<bean id="person" class="cn.bdqn.dome1.domes">
			<constructor-arg index="0" value="张嘎说:"></constructor-arg>
			<constructor-arg index="1" value="三台你不打鬼子蛋疼"></constructor-arg>
		</bean>
			<bean id="persons" class="cn.bdqn.dome1.domes" p:name="ROM说:" p:shuo="我来劲不">
		
		</bean>
</beans>

       之后 通过 ClassPathXmlApplicationContext 实例化Spring 的上下文

    

1     //  实例化spring 上下文        获取  配置文件信息
2         ApplicationContext cotext=new ClassPathXmlApplicationContext("applicationContext.xml");    
3         
4         // 获取委托
5         logins login=(logins)cotext.getBean("lo");
6         login.shu();

    Spring Aop 面向切面编程:是对面向对象编程的有益补充, 适用于具有 横切逻辑的场合, 如访问控制, 事物管理  性能检测。

    在业务流程中切入新代码  增加新功能  这就是所谓的面向切面编程  

    面向切面编程基本概念:

      1   切面 :一个模块化的横切逻辑  可能会横切多个对象

      2  连接点: 程序执行中 的 某个具体的执行点

      3 增强处理 : 切面 在某个特定连接点上执行的代码逻辑

      4 切入点 :对连接点 的特征描述 可使用正则表达式 增强处理和 切入点相关联

      5 目标对象 :被一个或多个 切面增强的对象

      6 织入:将增强处理链接到应用程序中的类型或对象上的过程

      与Aop 相关配置 都放在 <aop:config> 标签中    

      切入点<aop:poincut>的expression属性可以配置切入点  可使用 :  * com.service.*.*(..)  表示 com.service 包下的所有类的所有方法 

      * com.service ..*.*(..) 表示 com.service 包下及其子包下的所有类的所有方法 

      

<aop:config>
    //获取 切点
	<aop:pointcut expression="execution(* cn.bdqn.service..*.*(..))" id="pointcut1"/>
//织入
	<aop:aspect ref="se">
		<aop:before method="before" pointcut-ref="pointcut1" arg-names="joinpoint"></aop:before>
		
		<aop:after-throwing method="afterthrowing" pointcut-ref="pointcut1" throwing="e"/>
		<aop:after method="after"  pointcut-ref="pointcut1"></aop:after>
		<aop:around method="around"  pointcut-ref="pointcut1"/>
	</aop:aspect>


	</aop:config>    

  

  同时Spring 支持多种增强 :

  1. 前置增强

  2.后置增强

  3.  异常抛出增强

  4. 环绕增强

   5.最终增强  

后置增强:
如果目标方法 抛出了一个异常。无论是否使用try-catch捕获,都不会执行
最终增强:
如果目标方法 抛出了一个异常。无论是否使用try-catch捕获,都会执行

  

猜你喜欢

转载自www.cnblogs.com/basaio/p/9223758.html