Sping是一个开源的控制反转(ioc)和面向切面(aop)的容器框架
控制反转:负责对象的创建。
面向切面:可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术。
依赖注入(di)在运行期,由外部容器动态的将依赖对象注入到组件中。
Spring的结构组成
Spring的好处:1.降低组件之间的耦合度
2.可以使用容器提供众多的功能。如:事务管理服务。不需要处理复杂的事务。
3.提供单例模式,开发不需要自己写实现代码
4.提供的aop技术,利用它很容易实现权限拦截,运行期监控等功能
5.容器提供了众多的辅助类。
6.spring支持主流的应用框架。
Spring如果使用少量的服务我们可以认为他是轻量级的容器。如果spring使用大量的服务,我们就可以认为是重量级的容器。
对spring 的使用:
首先对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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions go here -->
</beans>
实例化spring 的两种方式:
1在类路径下寻找配置文件来实例化容器
2在文件系统路径下寻找配置文件来实例化容器。
一般建议使用第一种。