spring相关知识点

 Spring概述

Spring是一个开源框架

Spring为简化企业级开发而生,使用SpringJavaBean就可以实现很多以前要靠EJB才能实现的功能。同样的功能,在EJB中要通过繁琐的配置和复杂的代码才能够实现,而在Spring中却非常的优雅和简洁。

Spring是一个IOC(DI)AOP容器框架。

Spring的优良特性

[1]非侵入式:基于Spring开发的应用中的对象可以不依赖于SpringAPI(non-instrusive)

[2]依赖注入DI——Dependency Injection,反转控制(IOC)最经典的实现。

[3]面向切面编程Aspect Oriented Programming——AOP

[4]容器Spring是一个容器,因为它包含并且管理应用对象的生命周期

        [5]组件化Spring实现了使用简单的组件配置组合成一个复杂的应用。在 Spring 中可以使用XMLJava注解组合这些对象。

        [6]一站式:在IOCAOP的基础上可以整合各种企业应用的开源框架和优秀的第三方类库(实际上Spring 自身也提供了表述层的SpringMVC和持久层的Spring JDBC)。

Spring目前的版本

 

Spring模块

 

安装Spring插件

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

②操作步骤:参照《参考资料:Spring插件安装图解.pptx

 

搭建Spring运行时环境(单IOC)

①加入JAR

[1]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

[2]commons-logging-1.1.1.jar

②根据需要创建Spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    
    <!-- 通过p名称空间 为bean赋值 --><!-- Namespaces中添加p -->
    <bean id="student" class="com.neuedu.spring.entity.Student" 
        p:name="张三" 
        p:gender="1"
        p:address="河北">
    </bean>
    
    <!-- 实验14:给bean的级联属性赋值 -->
    <bean id="studentDao" class="com.neuedu.spring.entity.StudentDao"></bean>
    <bean id="studentService" class="com.neuedu.spring.entity.StudentService"></bean>
    <bean id="studentController" class="com.neuedu.spring.entity.StudentController">
        <property name="stuService" ref="studentService"></property>
        <property name="stuService.stuDao" ref="studentDao"></property>
        <property name="stuService.stuDao.username" value="张三 "></property>
    </bean>
    
    <!-- 实验3:通过构造器为bean的属性赋值 (构造器按顺序给属性赋值,若要指定顺序可再添加index属性(从0开始),设置对应属性对象)-->
    <!-- 在Book对象中再添加一个String类型属性isdu,按下面赋值方法执行时不管有参构造器顺序(分别不含price和idsu),第三个属性值均都在idsu处显示(可能均是String类型)
        若要在price(double类型)处显示,可在第三个赋值处添加限制类型
     -->
     <!-- 对象类中无参构造器一般不可少,否则可能报错 -->
    <bean id="book01" class="com.neuedu.spring.entity.Book">
        <constructor-arg value="作责" index="1"></constructor-arg>
        <constructor-arg value="甄嬛传" index="0"></constructor-arg>
        <constructor-arg value="33" type="double"></constructor-arg>
    </bean>
    <!-- class指定类对象的全类名,交给服务器创建一个对象,id是一个唯一标识,在IOC只能出现一个id值为book的bean对象 -->
    <!-- 创建类对象时,是通过全类名,再调用它的无参构造器,通过反射的机制构建出来的 -->
    <bean id="book" class="com.neuedu.spring.entity.Book">
        <property name="bookname" value="Thinking in Java"></property>
        <property name="author" value="Thomas"></property>
        <property name="price" value="33.6"></property>
        
    </bean>
    <!-- applicationContext.xml本身就相当于一个IOC容器 -->
</beans>
applicationContext.xml

applicationContext.xml本身就相当于一个IOC容器,哪个自己需要使用就为哪个创建一个bean。

4 HelloWorld

①目标:使用Spring创建对象,为属性赋值

②创建Student

 

③创建Spring配置文件

 

<!-- 使用bean元素定义一个由IOC容器创建的对象 -->

<!-- class属性指定用于创建bean的全类名 -->

<!-- id属性指定用于引用bean实例的标识 -->

<!-- 使用property子元素为bean的属性赋值 -->

<bean id="student" class="com.atguigu.helloworld.bean.Student">

<property name="studentId" value="1001"/>

<property name="stuName" value="Tom2015"/>

<property name="age" value="20"/>

</bean>

 

④测试:通过SpringIOC容器创建Student类实例

//1.创建IOC容器对象

ApplicationContext iocContainer =

new ClassPathXmlApplicationContext("helloworld.xml");

 

//2.根据id值获取bean实例对象

Student student = (Student) iocContainer.getBean("student");

 

//3.打印bean

System.out.println(student);

 

⑤验证:Spring在创建IOC容器对象时,就已经完成了bean的创建和属性的赋值。

posted @ 2017-08-27 21:07  康星悦  阅读(127)  评论(0编辑  收藏  举报