Spring之IOC注入

注入

  spring依赖注入

    set方法:

      <property name="属性名" values ="值">--ref="对象名"

    构造方法:

      <constructor-arg name="属性名" values="值">--ref="对象名"

    p命名空间:

      引入p命名空间的一个约束

      p:属性名=值 p:属性名-ref=“值”

    SPEL表达式

      格式:values=#{值,对象,类.方法 ,类.属性}

复杂数据类型

    数组,list <list><value>值</value></list>

    Set: <set><value>值</value></set>

    MAP:<entry key="值" value="值">

通知类型

  前置类型:before

  后置类型:after-returning

  环绕类型:around

  异常类型:after-throwing

  最终类型:after

AOP注解方式

  1.把切面类和目标类交给spring管理

  2.开启组件(AOP注解):<aop:aspetj-autoproxy>

  3.在切面类的方法上添加注解

SpringJDBC模板:

  jdbcTemplate

实现代码:

<bean id="car" class="com.test.aop.CarInfo"></bean>
<bean id="carProxy" class="com.test.aop.CarInfoProxy"></bean>

<!-- A0P配置 -->

<aop:config>
<!-- 配置本方法需要增强的类里面的哪些方法-->
<aop:pointcut  expression="execution(* com.test.aop.CarInfo.save(..))"
id="carPoincut"/>

<!-- 配置切面 增强类 -->
<aop:aspect ref="carProxy">

<!-- 前置类型 -->
<aop:before method="checkSave" pointcut-ref="carPoincut"/>
<!-- 后置类型 -->
<aop:after-returning method="late" pointcut-ref="carDelete" returning="re"/>
<!-- <aop:after method="late" pointcut-ref="carDelete"/> -->
<!-- aop:after-returning==如果发生异常就不会执行,after可以 -->
<!-- 环绕类型 -->
<aop:around method="around" pointcut-ref="carHuan"/>
<!-- 异常类型 -->
<aop:after-throwing method="exce" pointcut-ref="carex" throwing="exception"/>
<!-- 最终类型 -->
<aop:after method="fin" pointcut-ref="carAf"/>

</aop:aspect>

</aop:config>

<!-- 开启属性注解 -->
<context:annotation-config></context:annotation-config>

spring需要的约束:

<?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:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
"> <!-- bean definitions here --></bean>

猜你喜欢

转载自www.cnblogs.com/alomsc/p/10399512.html
今日推荐