170826-关于spring的知识点及练习

         1.Spring作用:  

                 1.生态体系庞大,全能型选手!【springmvc是其一个子模块,jdbcTemplate能直接操作数据库!】    

                 2.将其他组件粘合在一起    

                 3.IOC容器和AOP[Aspect Oreinted Programming]:   

                        Spring的Ioc[Inverse of Controller]机制(控制反转和依赖注入)正是用在此处。   

                        Spring的Ioc(控制反转和依赖注入)    

                        控制反转[Ioc]:就是由容器控制程序之间的(依赖)关系,而非传统实现中,由程序代码直接操控。    

                        控制反转是一种思想,其具体实现就是依赖注入!    

                        依赖注入[DI:Dependency Injection]:组件之间的依赖关系由容器在运行期决定,由容器动态的将某种依赖关系注入到组件之中。         

         2.IOC容器细节  

                 1.使用IOC容器创建对象  

                 2.使用IOC容器在创建对象的同时,给对象的属性赋值  

                 3.在创建对象的过程中,实现组件对象之间的自动装配  

         3.搭建Spring IOC容器需要的开发环境   

                 1.导入IOC容器需要的jar包   

                          spring-beans-4.0.0.RELEASE.jar   

                          spring-context-4.0.0.RELEASE.jar   

                          spring-core-4.0.0.RELEASE.jar   

                          spring-expression-4.0.0.RELEASE.jar   

                          commons-logging-1.1.3.jar   

                       

                  2.创建Spring配置文件[Spring bean Configuration File]     

          4.实验

                           实验1:

                                <!--实验1:通过IOC容器创建对象,并为属性赋值  -->  

                                <!-- 需要由IOC容器创建对象的全类名 -->  

                                <!-- 为了便于从IOC容器中获取book对象,声明一个一个唯一的id值 -->  

                                <bean id="book01" class="com.neuedu.spring.bean.Book">   

                                <property name="bookId" value="2001"></property>   

                                <property name="bookName" value="三国演义"></property>   

                                <property name="author" value="罗贯中"></property>   

                                <property name="price" value="22.5"></property>  

                                </bean>       

                                @Test   

                                 public void test01() {    

                                     //1.创建IOC容器自身的对象    

                                     //指定配置文件以类路径为基准的配置文件名    

                                     ApplicationContext ioc= new ClassPathXmlApplicationContext("bean.xml");    

                                     //2.从IOC容器中获取指定的对象    

                                     Object bean = ioc.getBean("book01");    

                                     System.out.println(bean);   

                                      }      

                                  注意:  ①IOC容器本身对象创建时,会将配置文件中配置好的bean先创建出来  

                                               ②默认是单实例的,只创建bean的一个对象  

                                               ③如果设置bean的scope属性为prototype,那么创建bean的对象就是多实例的,在获取的时候创建,每次获取对象都会创建新的  

                                               ④.从IOC容器中获取对象   

                                                       ①根据bean的id获取   

                                                       ②根据bean的类型获取:要求容器中指定类型的bean是唯一的

                              bean的后置处理器:  1.指的是在bean的初始化方法前后执行操作的专门的对象。  

                                                                 2.自定义的后置处理器:   

                                                                         1)需要实现接口:org.springframework.beans.factory.config.BeanPostProcessor .   

                                                                         2) 做相应的配置就好!  

                             

                               

                              

                             

                           

                    实验2:通过p名称空间为bean赋值

                       

                    实验3:测试bean的作用域,分别创建单实例和多实例的bean

                           创建bean

                                            

                          获取bean

                     

                         结果和总结

                           

                     实验4: IOC中bean对象的创建时机  

                           构造函数   

                            

                             test中什么都没写

                           

                               结果:

                                

                     实验5:通过构造器为bean的属性赋值和通过index属性指定参数的位置

                             指定顺序时,按照从0开始的方式指定

                            

                             不指定顺序时,默认按照实体类中的顺序

                            

                            结果都一样:

                                    

                     实验6: 给bean的级联属性赋值

                          

                          

                          

                        配置:

                           

                         测试:

                              

                         结果:

                            

                 实验7.创建带有生命周期方法的bean

                              实体类中写上:

                          

                             配置文件中的:

                           

                            测试:

                          

                            结果:

                          

                 实验8.bean之间的依赖 depends-on=order被依赖的对象会先创建

                           

                             tea依赖于stu:

                          

                              结果:

                             

                   实验9.通过继承实现bean配置信息的重用

                             

                              测试:

                             

                              结果:

                             

                             这个结果说明:继承的时候,没有的属性会从被继承的那里得到

                    实验10.通过abstract属性创建一个模板bean

                             

                    实验11.测试使用null值

                              

                                

                                 double默认是0.0 int 为0

                      实验12.引用其他bean

                                  

                               

                              

                                结果:

                                

                     实验13.引用内部bean

                                                     

                    实验14.使用List类型的集合属性

                                

                                  

                                  

                        实验15.使用Map类型的集合属性

                                    

                                     

                                                                         

                      实验16.使用prop子元素为Properties类型的属性赋值

 

                                     

                                    

                     实验17.配置通过静态工厂方法创建的bean       

                             静态工厂类:       

                            

                          配置静态工厂类:  

                       

                         测试:

                  

                         结果:

                       

                 实验18.配置通过实例工厂方法创建的bean

                         实例工厂类

                        

                         配置

                        

                        测试

                       

                        结果

                       

                实验19.配置FactoryBean

                          创建:

                         

                           

                          配置:

                         

                          测试:

                         

                          结果:

                         

                  实验20.测试bean的后置处理器     

                          创建:

                          

                                  

                          配置:

                         

                          测试:

                         

                          结果: 

                          

                   实验21.数据库连接池的使用

                           创建:

                          

                           配置:

                          

                           测试:

                          

                           结果:

                          

                  实验22.加载外部属性文件

                           创建:

                          

                           配置:

                           

                          

                           测试:

                          

                           结果:

                          

                  实验23.基于XML的属性装配

                       手动装配:

                           创建:

                           

                          

                           配置:

                          

                           测试:

                          

                           结果:

                         

                      自动装配---通过类型:

                           配置:

                          

                           测试:

                          

                           结果:

                          

                       自动装配---通过名字:

                           配置:

                          

                           测试:

                          

                           结果:

                          

                 实验24.使用注解配置bean

                          @Component 将当前类声明为IOC容器中的一个普通的组件
                          @Controller 将当前类声明为IOC容器中的一个控制器组件
                          @Service 将当前类声明为IOC容器中的业务逻辑层组件
                          @Repository 将当前类声明为IOC容器中的一个持久化层组件
                          Spring根据上述注解其实并不能分辨当前类是否真的是一个控制器或Dao,即使标记的类和注解不对应也没有语法错误。但在实际工作中,肯定要将专门的注解标记在对应的类上面。            

                          使用基于注解的bean的配置,需要额外导入一个jar包:spring-aop-4.0.0.RELEASE.jar    

                          需要设置自动扫描的包   < context:component-scan base-package ="com.neuedu.ioc.bean"/>

                          使用注解后,默认按照类名首字母小写作为id的值,也可以使用value属性指定id,value属性名也可以省略注解 

                实验25.指定扫描包时要(不)包含的类

                          配置时体现:

                         

                 实验26.使用注解进行自动装配详细说明

                         

 

                              

 

posted @ 2017-08-26 19:23  12344321hh  阅读(361)  评论(0编辑  收藏  举报