Spring介绍
Spring概述:
- Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架:
- 从大小与开销两方面而言Spring都是轻量级的
- 通过控制反转(IoC)的技术达到松耦合的目的
- 提供了面向切面编程的丰富支持,允许通过分离应用的业务逻辑与系统服进行内聚性的开发
- 包含并管理应用对象的配置和生命周期,这个意义上是一种容器
- 将简单的组件配置、组合成为复杂的应用,这个意义上是框架
Spring作用:
- 容器
- 提供了对多种技术的支持,如(JMS,MQ支持,UnitTest等)
- AOP(事物,日志等)
- 提供了众多方便应用的辅助类(JDBC Template等)
- 对主流应用框架(Hibernate等)提供了良好的支持
Spring适用范围:
- 构建企业应用(SpringMVC+Spring+Hibernate/ibatis)
- 单独使用Bean容器(Bean管理)
- 单独使用AOP进行切面处理
IoC(控制反转):
控制权的转让,应用本身不负责依赖对象的创建和维护,而是由外部容器负责创建和维护,而具体被反转的是获得依赖对象的过程,控制被反转之后,获得依赖对象的过程由自身管理变为了由IoC容器主动注入,简单的来说,就是不需要我们自己去实例化所需要的对象,而是通过Spring的外部容器来帮我们去创建,需要使用的时候,直接从容器中取出来就好了
DI(依赖注入):
所谓依赖注入,就是由IoC容器在运行期间,动态的将某种依赖关系注入到对象之中,也就是说在创建对象的同时,建立起对象与对象之间的依赖关系。
Spring配置文件详解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| <context:component-scan base-package="com.mogon.test" />
<task:annotation-driven />
<aop:aspectj-autoproxy />
<bean id="Bean实例名称" class="Bean类全名" />
<bean id="Bean实例名称" class="Bean类全名" scope="prototype" />
<bean id="Bean实例名称" class="Bean类全名" init-method="方法名" destroy-method="方法名" />
<bean id="Bean实例名称" class="Bean类全名"> <property name="Bean类中的属性名称" value="直接指定属性值" /> <property name="Bean类中的属性名称" ref="要引用的Bean名称" /> <property name="Bean类中的属性名称"> <bean class="Bean类全名" /> </property> <property name="Bean类中的Set类型属性名称"> <set> <value>set中的元素</value> <ref bean="要引用的Bean的名称" /> </set> </property> <property name="Bean类中的List类型属性名称"> <list> <value>List中的元素</value> <ref bean="要引用的Bean的名称" /> </list> </property> <property name="Bean类中的Map类型属性名称"> <map> <entry key="map元素的key"> <value>map元素的value</value> </entry> <entry key="map元素的key"> <ref bean="要引用的Bean名称" /> </entry> </map> </property> <property name="Bean类中的Properties类型属性名称"> <props> <prop key="properties元素的key">properties元素的value</prop> </props> </property> <property name="Bean类中要初始化为null的属性名称"> <null /> </property> </bean>
<bean id="Bean实例名称" class="Bean类全名"> <constructor-arg index="从0开始的序号" type="构造参数的类型" value="构造参数的值" /> <constructor-arg index="从0开始的序号" type="构造参数的类型" ref="要引用的Bean名称" /> </bean> <bean id="目标对象名称" class="目标对象类全名" /> <bean id="切面实例名称" class="切面类全名" /> <aop:config> <aop:aspect id="切面ID" ref="要引用的切面实例名称"> <aop:pointcut id="切入点名称" expression="切入点正则表达式" /> <aop:before pointcut-ref="切入点名称" method="切面类中用作前置通知的方法名" /> <aop:after-returning pointcut-ref="切入点名称" method="切面类中用作后置通知的方法名" /> <aop:after-throwing pointcut-ref="切入点名称" method="切面类中用作异常通知的方法名" /> <aop:after pointcut-ref="切入点名称" method="切面类中用作最终通知的方法名" /> <aop:around pointcut-ref="切入点名称" method="切面类中用作环绕通知的方法名" /> </aop:aspect> </aop:config>
<bean id="事务管理器实例名称" class="事务管理器类全名"> <property name="数据源属性名称" ref="要引用的数据源实例名称" /> </bean>
<tx:advice id="事务通知名称" transaction-manager="事务管理器实例名称"> <tx:attributes> <tx:method name="get" read-only="true" propagation="NOT_SUPPORTED" /> <tx:method name="*" /> </tx:attributes> </tx:advice>
<aop:config> <aop:pointcut id="事务切入点名称" expression="事务切入点正则表达式" /> <aop:advisor advice-ref="事务通知名称" pointcut-ref="事务切入点名称" /> </aop:config> </beans>
|
如何配置Spring配置文件中的命名空间
假如要使用context标签,就要引入context的xsd约束文件到命名空间中,那应该从哪里查找呐,如下图所示:



从spring官网下载相应的spring的jar文件后,里面有一个schema文件夹,spring不同于struts2等框架,他并不是基于dtd文件来约束xml的编写的,而是通过schema来进行xml编写约束的,而我们需要找的内容就在schema文件夹下,如图我们打开最新版本的spring-context-4.3.xsd,如下图所示:

我们找到标黄的那一行,将其复制,然后进行修改,修改为:
1
| xmlns:context="http://www.springframework.org/schema/context"
|
然后将其粘贴到spring配置文件的命名空间中,放在:
1
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
的后面
如下图所示:

然后修改xsi:schemaLocation的值,在后面添加:
1 2
| http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
|
两段网址中间用用空格隔开就好,这样我们就讲context引入到spring的命名空间中了。(注:个人理解这写网址的作用是在联网时,MyEclipse在编写时候可以通过alt+/来进行代码提示,如果断网的话,代码提示就失效了,这点只是个人理解,如有其它定义,还望指出)