【从头开始学Spring】了解Spring的机制

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/ca1m0921/article/details/88965475

1. Spring的 IOC/DI  和 AOP ?

 IOC 控制翻转,就是将 之前 new 对象的工作,交给 Spring 去做。将 pojo原型映射给 applicationContext.xml 中 的 bean 。又 Spring 来构建对象。在使用时,只需要 加载 applicationContext.xml 文件获取 context对象,通过 context 获取 响应的bean(“x”)就能获取到对象。

DI 依赖注入,在Spring 构造对对象的时候,将属性值 注入 到对象中,也就是在 构造对象的时候 完成赋值操作。

AOP 面向切面: Spring 在 项目正常逻辑的基础上,将一些辅助功能,例如 日志、性能统计的功能逻辑,提取出来。在需要的时候再 通过切面的形式 加载进去执行。有利于 代码的可读性,更简洁。

以上的技术实现都可以通过在  applicationContext.xml 添加  bean  property  aop:config  来实现。  也可以通过 在代码 中注解 来实现: @Aurowired   @Resource     @Componnet    @Aspect    @Around

AOP :

ApplicationContext.xml


<bean id="loggerAspect" class="top.yancc.aspect.LoggerAspect" />


<aop:config>
    <aop:point-cut id="loggerPoint" expression="execution(* top.yancc.service.ProductService.*(..))" />

    <aop:aspect id="logAspect" ref="loggerAspect">
        <aop:around pointcut-ref="loggerPoint" method="log" />
    </aop:aspect>
</aop:config>

Spring  甚至没有用到 web.xml   只是 使用 applicationContext.xml  加上一些注解,就完成了 控制翻转 和 注入,面向切面编程。

猜你喜欢

转载自blog.csdn.net/ca1m0921/article/details/88965475