Spring框架
第一章 Spring概述
1.1Spring概述
1)Spring是一个开源框架
2)Spring为简化企业级开发而生,使用Spring,JavaBean就可以实现很多以前要靠EJB(Enterprise JavaBean)才能实现的功能,同样的功能,在EJB中要通过繁琐的配置和复杂的代码才能够实现,而在Spring中却非常的优雅和简洁。
3)Spring是一个IOC(控制反转)(DI)和AOP(面向切面编程)容器框架。
4)Spring的优良特性
①非侵入式:基于Spring开发的应用中的对象可以不依赖Spring的API(实现接口或继承类,又叫做轻量级)
②依赖注入:DI——Dependency Injection.反转控制(IOC)最经典的实现。
③面向切面编程:Aspect Oriented Programming——AOP
④容器:Spring是一个容器,因为它包含并且管理应用对象的生命周期,可以使用XML和Java注解组合这些对象
5)一站式:在IOC和AOP的基础上可以整合各种企业应用的开源框架和优秀的第三方类库(实际上Spring自身也提供了表述层的springMVC和持久层的SpringJDBC).
6)Spring模块
1.2安装Spring插件【了解】
1)插件包:springsource-tool-suite-3.4.0.RELEASE-e4.3.1-updatesite.zip
2)操作步骤:参照《[尚硅谷]_参考资料:安装Springtools插件.doc》
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
2 ) 在Spring Tool Suite工具中通过如下步骤创建Spring的配置文件 (STS可以自动创建)(STS就是eclips+Springtools)
① File->New->Spring Bean Configuration File
② 为文件取名字 例如:A
1.4Hello World
- 目标 :使用Spring创建对象,为属性赋值
2)创建Student类
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);
第二章 IOC容器和Bean的配置
2.1IOC和DI
2.1.1 IOC(Inversion of Control):反转控制
在应用程序中的组件需要获取资源时,传统的方式是组件主动从容器中获取所需要的资源,在这样的模式下开发人员往往需要知道在具体容器中特定资源的获取方式,增加了学习成本,同时降低了开发效率。
反转控制的思想完全颠覆了应用程序组件获取资源的传统方式:反转了资源的获取方向——改由容器主动的将资源推送给需要的组件,开发人员不需要知道容器是如何创建资源对象的,只需要提供接受资源的方式即可,极大的降低了学习成本,提高了开发的效率,这种行为已成为查找的被动形式。
2.1.2 DI(Dependency Injection):依赖注入
IOC的另一种表述方式:即组件以一些预先定义好的方式(例如:setter 方法)接受来自于容器的资源注入,相对于IOC而言,这种表述更直接。
IOC描述的是一种思想,而DI是对IOC思想的具体实现。
2.1.3 IOC容器在Spring中的实现(也就是创建IOC本身的对象)
1)在通过IOC容器读取Bean的实例之前,需要先将IOC容器本身实例化。
2)Spring提供了IOC容器的两种实现方式
①BeanFactory:IOC容器的基本实现,是Spring内部的基础设施,是面向Spring本身的,不是提供给开发人员使用的。
②ApplictionContext:BeanFactory的子接口,提供了更多高级特性,面向Spring的使用者,几乎所有场合都使用ApplicationContext而不是底层的BeanFactory
2.1.4 ApplicationContext的主要实现类
1)ClassPathXmlApplicationContext:对应类路径下的XML格式的配置文件
2)FileSystemXmlApplicationContext:对应文件系统中的XML格式的配置文件
3)在初始化时就创建单例bean,也可以通过配置的方式指定创建的Bean是多实例的。
2.1.5 ConfigurableApplicationContext
1)是ApplicationContext的子接口,包含一些扩展方法
2)refresh()和close()让ApplicationContext具有启动、关闭和刷新上下文能力。
2.1.6 WebApplicationContext
1)专门为WEB应用而准备的,他允许从相对的WEB根目录的路径中完成初始化换工作
2.2 通过类型获取bean(三种,优缺点自己总结上)
1)通过id值获取,需要类型的强制转换
2)从IOC容器中获取bean时,除了通过id值获取,还可以通过bean的类型获取。但如果同一个类型的bean在XML文件中配置了多个,则获取时抛出异常(NoUniqueBeanDefinitionException),所以同一个类型的bean在容器中必须是唯一的。
HelloWorld helloWorld = cxt.getBean(HelloWorld. class);
3)或者可以使用另一个重载的方法,同时制定dean的id值和类型
HelloWorld helloWorld = cxt.getBean(“helloWorld”,HelloWorld. class);
2.3给bean的属性赋值
2.3.1 依赖注入的方式
1.通过bean的setXXX()方法赋值
2.通过bean的构造器赋值
1)Spring自动匹配合适的构造器
<bean id="book" class="com.atguigu.spring.bean.Book" >
<constructor-arg value= "10010"/>
<constructor-arg value= "Book01"/>
<constructor-arg value= "Author01"/>
<constructor-arg value= "20.2"/>
</bean >
2)通过索引指定参数位置
<bean id="book" class="com.atguigu.spring.bean.Book" >
<constructor-arg value= "10010" index ="0"/>
<constructor-arg value= "Book01" index ="1"/>
<constructor-arg value= "Author01" index ="2"/>
<constructor-arg value= "20.2" index ="3"/>
</bean >
3)通过类型区分重载的构造器
<bean id="book" class="com.atguigu.spring.bean.Book" >
<constructor-arg value= "10010" index ="0" type="java.lang.Integer" />
<constructor-arg value= "Book01" index ="1" type="java.lang.String" />
<constructor-arg value= "Author01" index ="2" type="java.lang.String" />
<constructor-arg value= "20.2" index ="3" type="java.lang.Double" />
</bean >
2.3.2 p名称空间
为了简化XML文件的配置,越来越多的XML文件采用属性而非子元素配置信息。Spring从2.5版本开始引入了一个新的p命名空间,可以通过元素属性的方式配置Bean的属性
使用p命名空间后,基于XML的配置方式将进一步简化
<bean
id="studentSuper"
class="com.atguigu.helloworld.bean.Student"
p:studentId="2002" p:stuName="Jerry2016" p:age="18" />
2.3.3 可以使用的值
1.字面量(与其相对的应该是非字面量吧?)
1)可以使用字符串表示的值,可以通过value属性或value子节点的方式指定
2)基于数据类型及其封装类、String等类型都可以采取字面值注入的方式
3)若字面值中包含特殊字符,可以使用<![CDATA[]]>把字面包裹起来
2.null值
<bean class="com.atguigu.spring.bean.Book" id="bookNull" >
<property name= "bookId" value ="2000"/>
<property name= "bookName">
<null/>
</property>
<property name= "author" value ="nullAuthor"/>
<property name= "price" value ="50"/>
</bean >
*推荐上图这种用标签的方式,如果是引用类对象有多个成员变量的,value就不能对应多个值了。不推荐下图这种用元素属性的方式。在这里插入代码片
<bean class="com.atguigu.spring.bean.Book" id="bookNull" >
<property name= "bookId" value ="2000"/>
<property name= "bookName" value=”null”></property>
<property name= "author" value ="nullAuthor"/>
<property name= "price" value ="50"/>
</bean >
*用构造器给参数赋值的形式,也可以用null值
<bean id="student8" class="spring.Student">
<constructor-arg value="1008" type="Integer" />
<constructor-arg type="String"><null/></constructor-arg>
<constructor-arg value="33" type="int"/>
<constructor-arg value="男" type="char"></constructor-arg>
<constructor-arg ref="car" />
</bean>
3.给bean的级联属性赋值
<bean id="action" class="com.atguigu.spring.ref.Action">
<property name="service" ref="service"/>
<!-- 设置级联属性(了解) -->
<property name="service.dao.dataSource" value="DBCP"/>
</bean>
4.(引用)外部已声明的bean
<bean id="shop" class="com.atguigu.spring.bean.Shop" >
<property name= "book" ref ="book"/>
</bean >
5.内部bean
当bean实例仅仅给一个特定的属性使用时,可以将其声明为内部bean,内部bean声明直接包含或元素里,不需要任何id或name属性
内部bean不能使用在任何其他地方
<bean id="shop2" class="com.atguigu.spring.bean.Shop" >
<property name= "book">
<bean class= "com.atguigu.spring.bean.Book" >
<property name= "bookId" value ="1000"/>
<property name= "bookName" value="innerBook" />
<property name= "author" value="innerAuthor" />
<property name= "price" value ="50"/>
</bean>
</property>
</bean >