spring基本用法总结

使用两年spring后,发现对spring依旧陌生,遂重新总结spring基础用法

特性:1,依赖注入的核心机制;2,Aop的声明式事务管理;3与多种持久层结合使用;4,web MVC的优秀使用;

一、下载安装

       1,http://repo.springsource.org/libs-release-local/下下载springframework下的spring找到spring-framework-4.0.4.release-dist.zip下载解压

         libs文件夹下有所需所有jar包;需要额外下载common-logging的jar包

         schemas下有所需要各种配置xml文件的xml schema文档

        2,使用eclipse的user liberary选项加载jars,具体方法不解释,方便实用。

二、核心机制

        1.依赖注入

         名称定义:控制反转(将以前主动创建对象的权利交给spring),依赖注入(spring给调用者注入实例,供其使用)

         <1.设值注入:使用成员变量的setter方法注入;

                  <property  name="***"    ref="">-----------------成员变量为类(具体实现类不能是接口)

                 <property name="*** "     value="">--------------成员变量为基本数据类型

                   <property name=" ">

                                 <list>

                                          <value>小学</value>

                                   </list> 

                    </property>

             <property name=" ">

                                 <map>

                                          <entry key="数学" value="32">

                                   </map> 

                    </property>

   <property name=" person.name" value="孙悟空"/>最后一个菜用setter前面用get(所以不能为空) 

          <2.构造注入:使用构造方法注入;

                   <constructor-arg name=""  ref=“ ”>参数是一个类


                   <constructor-arg  name=""  value="  ">参数是一个值可以加上type="int"来区别多构造方法index="0"代表位置

                  <constructor-arg name=""  value="  ">参数是一个值

           2.spring容器

          <1  BeanFactory:功能不强,没用下边的功能

         <2  ApplicationContext;功能更为强大,初始化所用单例类,创建bean;为了使它不初始化可以在,bean指定lazy-init="true";可以及时发现初始化时错误

                   支持国际化使用ResourceBundleMesageSource类

             事件机制:(1)监听器

                                   《1》定义一个使用一个继承了ApplicationEvent的类,它为容器事件

                                   《2》实现一个ApplicationListener接口的实现onApplicationEvent方法

                                 (2)其他内置事件(不做分析)

              <3 特殊情况下在bean中获取spring容器:

                          类继承ApplicationContAware,注入ApplicationContext属性就可以使用了(特殊功能如:国际化)

               <4 容器中bean设置(前面加上default-就可以放在beans中了)

                              《1》为bean定义特殊字符的别名

                                  《2》lazy-init延迟初始化

                             《3》atuo-autowire自动装配行为

                                  no:

                                  byName:

                                byType:

                              constructor:

                               autodetect


                                《4》auto-autowire-candidates排除自动装配

                                《5》destroy-method默认回收方法

                            例如<bean id="            " class="    ">(alias为已有bean添加别名)

                              bean作用域:

                                          scope="         "

                                  《1》singleton:单例(默认)

                                   《2》prototype:每一次容器getbean都是一个新的

                                    《3》request:

                                   《4》session:

                                   《5》globeSession:

               3,使用注释管理

                          annotation

                      @configuration:修饰一个Java配置类

                       @Bean(name="chinese"):修饰一个方法返回值定义一个bean

                       @value("孙悟空"):修饰一个field;配置一个变量

                      《1》:以xml文件为主:先创建spring容器

                        《2》:以java配置为主:使用@ImportResource("classpath:/beans.xml")

待续。。。。。。。。。。。。。。。。。



猜你喜欢

转载自blog.csdn.net/u010446936/article/details/54409564