spring框架环境搭建

来源:互联网 发布:海量数据为什么大跌 编辑:程序博客网 时间:2024/06/05 16:47
1、添加Spring框架的依赖jar    这个到maven里查查就好     网址:https://mvnrepository.com/artifact/org.springframework
    
            <dependencies>
            <!-- spring的 jar -->
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-context</artifactId>
                    <version>4.3.10.RELEASE</version>
                </dependency>
            </dependencies>
        
    2、 添加的Spring的配置文件 spring-context.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的配置 -->
                       <bean id=""  class="" />
                        
                 </beans>    
            简单解释下:
                xmlns 命名空间
                xmlns:xsi 表示xml的官方规范
                xsi:schemaLocation Spring配置的约束规范
                
                
    3、使用的spring
         第一步、 创建测试的 class
         第二步、 在Spring-context.xm 中配置的bean
               
                  <!-- 实现bean的装配 -->     
                   <bean id="userService"  class="com.shsxt.demo.UserService" />
                    
              在  《Spring 实战》   一书中:  称上述的bean的装配过程 为  显式装配
              注意: bean  的id 默认是使用的类名  且  首字母小写
             
             
    4、 测试Spring环境
         1、加载spring的配置文件 (启动框架)
                    ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-context.xml");
        
         2、获取Spring的bean池的中的示例化 bean
        
                 UserService bean = ctx.getBean(UserService.class);
                System.out.println(bean);
                UserService bean2 = (UserService) ctx.getBean("userService");
                System.out.println(bean2);
                
         说明 Spring的bean池的中的示例化 bean--->    默认,是个单例模式
                     
                     
-------------------------------------------------------------------------------------------------------

     Spring框架 对bean是怎样进行维护?

         Spring对bean的维护是通过 BeanFactory  进行维护。
         
         1、 Spring IOC 技术
             
             第一步、 Spring框架在启动时候, 会加载我们的Spring的配置文件(Spring-context.xml)
                 通过的Spring的xml的读取的, 获取到  用户显示配置的bean 信息。
             第二步、根据的用户的配置的bean要求, 框架使用反射等 去创建实例化bean,   将创建好的放置我们Spring bean池
             第三、 根据的用户的自身的需要的,取出bean池中的示例化的bean
             小结: 这样的过程中, 实现将的我们对bean的控制权 ,移交给了 Spring的框架 ---------> IOC 控制反转---> 对bean的控制权反转给了Spring
             上述为:Spring对bean的维护过程。
原创粉丝点击