spring MVC --5.11 loc(控制反转)di(依赖注入) aop

DI(依赖注入):
组件之间依赖关系由容器在运行期决定,Spring容器根据javabean之间类与类之间的组合关系,将被组合的实例对象进行自动初始化赋值
LOC(控制反转)
在spring容器中,javabean之间的类与类之间存在组合关系,当一个类组合了另外一个类,这个类将被组合的对象的初始化控制权托管给spring容器进行处理,
aop(面向切面编程)
增强:AOP(切面编程)是用来给某一类特殊的连接点,添加一些特殊的功能
比如:添加日志、管理事务。
连接点:连接点就是程序执行的某个特定的位置
连接点就是被拦截处理的业务逻辑点,就是需要进行业务逻辑处理的功能的时机点;例如方法执行前、方法执行后、方法抛出异常等
织入:织入是将增强添加对目标类具体连接点上的过程;也就是把增强附加给连接点的过程就是织入;增强和连接点有机结合的过程就是织入
切入点:被增强的连接点就是切入点
切面:切面由切点和增强(或引介)组成,或者只由增强(或引介)实现。
目标对象:增强逻辑的织入目标类,也就是需要增强的目标类;(动态代理模式中的被代理的对象)
代理:目标对象(被代理对象)通过AOP机制生成的代理对象;

总结:
依赖注入和控制反转是同一件事情的两个不同角度的理解:
从spring容器角度看:依赖注入是spring容器根据javabean 之间的组合关系,完成被组合的成员变量的自动初始化赋值;
从javabean主体类的角度看:控制反转就是将该类中被组合的实例对象的初始化控制权交给spring容器进行管理,完成自动初始化赋值;
总结:
连接点就是增强(业务逻辑处理)触发的时机点;
增强就是进行的AOP的业务逻辑处理;
织入就是连接点和增强有机结合的过程;
切入点就是连接点和增强的有机结合的结果;
切面就是由所有切入点构成的一个逻辑面;AOP主要是在这个逻辑面上进行增强(业务逻辑处理)的管理;

       把“增强”用“织入”的方式结合在“连接点”上构成“切入点”,所有“切入点”汇集为“切面”;
        AOP采用动态代理的模式对“切面”进行“增强”的管理;

三种实例化方法

1,构造方法实例化
<bean id="exampleBean" class="examples.ExampleBean"/>

<bean name="anotherExample" class="examples.ExampleBeanTwo"/>
2,静态方法实例化
<bean id="clientService"
    class="examples.ClientService"
    factory-method="createInstance"/>
3,实例工厂实例化
<bean id="serviceLocator" class="examples.DefaultServiceLocator">
    <!-- inject any dependencies required by this locator bean -->
</bean>

<!-- the bean to be created via the factory bean -->
<bean id="clientService"
    factory-bean="serviceLocator"
    factory-method="createClientServiceInstance"/>

基于setter的依赖注入

<bean id="exampleBean" class="examples.ExampleBean">
    <!-- setter injection using the nested ref element -->
    <property name="beanOne">
        <ref bean="anotherExampleBean"/>
    </property>

    <!-- setter injection using the neater ref attribute -->
    <property name="beanTwo" ref="yetAnotherBean"/>
    <property name="integerProperty" value="1"/>
</bean>

<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>

基于构造条件的DI
<bean id="exampleBean" class="examples.ExampleBean">
    <!-- constructor injection using the nested ref element -->
    <constructor-arg>
        <ref bean="anotherExampleBean"/>
    </constructor-arg>

    <!-- constructor injection using the neater ref attribute -->
    <constructor-arg ref="yetAnotherBean"/>

    <constructor-arg type="int" value="1"/>
</bean>

<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>

声明一个切入点
 <bean id="SYSlogAdvice" class="com.dz.advice.SYSlogAdvice"></bean>
    <aop:config proxy-target-class="true">
        <aop:aspect id="myAspect" ref="SYSlogAdvice">
            <aop:pointcut id="businessService"
                          expression="execution(* com.dz.service.*.*(..))"/>
            <aop:before pointcut-ref="businessService" method="sysLogAdvice"/>
        </aop:aspect>
    </aop:config>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值