一、Spring概述

1.1 Spring概述:

    1)  Spring是一个开源框架 
    2)  Spring为简化企业级开发而生,使用Spring,JavaBean就可以实现很多以前要靠EJB才能实现的功能。同样的功能,在EJB中要通过繁琐的配置和复杂的代码才能够实现,而在Spring中却非常的优雅和简洁。 
    3)  Spring是一个IOC(DI)和AOP容器框架。
    4)  Spring的优良特性
     ①   非侵入式:基于Spring开发的应用中的对象可以不依赖于Spring的API
     注解:假设大家都想要把用户代码塞到一个框架里。侵入式的做法就是要求用户代码“知道”框架的代码,表现为用户代码需要继承框架提供的类。非侵入式则不需要用户代码引入框架代码的信息,从类的编写者角度来看,察觉不到框架的存在。
     参考:https://blog.csdn.net/xujiangdong1992/article/details/73467922
     ②  依赖注入:DI——Dependency Injection,反转控制(IOC)最经典的实现。
     ③  面向切面编程:Aspect Oriented Programming——AOP
     ④  容器:Spring是一个容器,因为它包含并且管理应用对象的生命周期
     ⑤  组件化:Spring实现了使用简单的组件配置组合成一个复杂的应用。在 Spring 中可以使用XML和Java注解组合这些对象。
    5)  一站式:在IOC和AOP的基础上可以整合各种企业应用的开源框架和优秀的第三方类库(实际上Spring 自身也提供了表述层的SpringMVC和持久层的Spring JDBC)。
    6)  Spring模块

一、Spring概述

1.2 安装Spring插件

1)  插件包:springsource-tool-suite-3.4.0.RELEASE-e4.3.1-updatesite.zip

STS=eclipse+Spring插件  更方便的使用Spring

1.3 搭建Spring运行时环境

1)  加入JAR包
① Spring自身JAR包:spring-framework-4.0.0.RELEASE\libs目录下
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.1.jar (日志被core依赖了)
2)  在Spring Tool Suite工具中通过如下步骤创建Spring的配置文件
① File->New->Spring Bean Configuration File (创建时候带有spring头信息并且使用的默认版本,在xml文件中 左下角NameSpaces默认勾选beans  点击右边会出现版本) 
② 为文件取名字 例如:applicationContext.xml

1.4 HelloWorld

1)  目标:使用Spring创建对象,为属性赋值
2)  创建Student类
        ① 定义一些属性并给set  get方法  toString()方法等
3)  创建Spring配置文件
  <!-- 使用bean元素定义一个由IOC容器创建的对象 -->
    <!-- class属性指定用于创建bean的全类名 -->
    <!-- id属性指定用于引用bean实例的标识 -->
    <bean id="student" class="com.atguigu.helloworld.bean.Student">
        <!-- 使用property子元素为bean的属性赋值 -->
        <property name="studentId" value="1001"/>
        <property name="stuName" value="Tom2015"/>
        <property name="age" value="20"/>
    </bean>
4)  测试:通过Spring的IOC容器创建Student类实例
    //1.创建IOC容器对象
    ApplicationContext iocContainer = 
            new ClassPathXmlApplicationContext("helloworld.xml");
    //2.根据id值获取bean实例对象
    Student student = (Student) iocContainer.getBean("student");
    //3.打印bean
    System.out.println(student);

猜你喜欢

转载自blog.51cto.com/11864647/2306839
今日推荐