文章目录
一、Spring概述
①Spring是一个开源框架
②Spring为简化企业级开发而生,使用Spring,JavaBean就可以实现很多以前要靠EJB才能实现的功能。同样的功能,在EJB中要通过繁琐的配置和复杂的代码才能够实现,而在Spring中却非常的优雅和简洁。
③Spring是一个IOC(DI)和AOP容器框架。
④Spring的优良特性
-
[1]非侵入式:基于Spring开发的应用中的对象可以不依赖于Spring的API
-
[2]依赖注入:DI——Dependency Injection,反转控制(IOC)最经典的实现。
-
[3]面向切面编程:Aspect Oriented Programming——AOP
-
[4]容器:Spring是一个容器,因为它包含并且管理应用对象的生命周期
-
[5]组件化:Spring实现了使用简单的组件配置组合成一个复杂的应用。在 Spring 中可以使用XML和Java注解组合这些对象。
-
[6]一站式:在IOC和AOP的基础上可以整合各种企业应用的开源框架和优秀的第三方类库(实际上Spring 自身也提供了表述层的SpringMVC和持久层的Spring JDBC)。
⑤Spring目前的版本
⑥Spring模块
Test:Spring的单元测试模块;
spring-test-4.0.0.RELEASE
Core Container:核心容器(IOC);黑色代表这部分的功能由哪些jar包组成;要使用这个部分的完整功能,这些jar都需要导入
spring-beans-4.0.0.RELEASE、
spring-core-4.0.0.RELEASE、
spring-context-4.0.0.RELEASE、
spring-expression-4.0.0.RELEASE
AOP+Aspects(面向切面编程模块)
spring-aop-4.0.0.RELEASE、
spring-aop-4.0.0.RELEASE
数据访问/:Spring数据库访问模块
spring-jdbc-4.0.0.RELEASE、spring-orm(Object Relation Mapping)-4.0.0.RELEASE、
spring-ox(xml)m-4.0.0.RELEASE、spring-jms-4.0.0.RELEASE、(Intergration)
spring-tx-4.0.0.RELEASE(事务)
Web:Spring开发web应用的模块;
spring-websocket(新的技术)-4.0.0.RELEASE、
spring-web-4.0.0.RELEASE、和原生的web相关(servlet)
spring-webmvc-4.0.0.RELEASE、开发web项目的(web)
spring-webmvc-portlet-4.0.0.RELEASE(开发web应用的组件集成)
用哪个模块导哪个包(建议)
1.安装Spring插件
开发Spring框架的应用,经常要写框架的配置文件,写起来复杂,我们需要提示;需要给eclipse中安装插件;(提供提示功能);
1)、不想装插件;使用Spring官方提供的sts开发工具
(装好插件的eclipse)
2)、装插件;
1)、查看当前eclispe的版本;
Help->About Eclipse–>点击自己eclipse的图标
②操作步骤:
注意哟,这里只选择带有Spring IDE的就可以了呢
这里的勾勾去掉,Contact all update sites during install to find required software
点这里的YES重启Eclipse使插件生效
2. 搭建Spring运行时环境
①加入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配置文件
3 HelloWorld
①目标:使用Spring创建对象,为属性赋值
②创建Person类
③创建Spring配置文件
<!-- 使用bean元素定义一个由IOC容器创建的对象 -->
<!-- class属性指定用于创建bean的全类名 -->
<!-- id属性指定用于引用bean实例的标识 -->
<bean class="com.welisit.bean.Person" id="person01">
<!-- 使用property子元素为bean的属性赋值 -->
<property name="lastName" value="zhangsan"></property>
<property name="gender" value="man"></property>
<property name="email" value="zhangsan@qq.com"></property>
<property name="age" value="18"></property>
</bean>
④测试:通过Spring的IOC容器创建Student类实例
//1.创建IOC容器对象
ApplicationContext iocContainer =
new ClassPathXmlApplicationContext("ioc.xml");
//2.根据id值获取bean实例对象
Student student = (Student) iocContainer.getBean("student");
//3.打印bean
System.out.println(student);
⑤验证:Spring在创建IOC容器对象时,就已经完成了bean的创建和属性的赋值。
4.框架特性测试流程
1.导包
核心容器
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
Spring运行的时候依赖一个日志包;没有就报错;
2.写配置
spring的配置文件中,集合了spring的ioc容器管理的所有组件(会员清单);
创建一个Spring Bean Configuration File(Spring的bean配置文件);
<?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 class="com.welisit.bean.Person" id="person01">
<property name="lastName" value="zhangsan"></property>
<property name="gender" value="man"></property>
<property name="email" value="zhangsan@qq.com"></property>
<property name="age" value="18"></property>
</bean>
</beans>
3.测试
@Test
public void test() {
//1.创建IOC容器对象
ApplicationContext iocContainer = new ClassPathXmlApplicationContext("ioc.xml");
System.out.println("根据id获取bean 实例");
//2.根据id值获取bean实例对象
Person student = (Person) iocContainer.getBean("person01");
//3.打印bean
System.out.println(student);
}
**new ClassPathXMlApplicationContext(“ioc.xml”);ioc容器的配置文件在类路径下;**FileSystemXmlApplicationContext(“F://ioc.xml”);ioc容器的配置文件在磁盘路径下;
/**
* 存在的几个问题;
* 1)、src,源码包开始的路径,称为类路径的开始;
* 所有源码包里面的东西都会被合并放在类路径里面;
* java:/bin/
* web:/WEB-INF/classes/
* 2)、导包commons-logging-1.1.3.jar(依赖)
* 3)、先导包再创建配置文件;
* 4)、Spring的容器接管了标志了s的类;
*/
/**
* 几个细节:
* 1)、ApplicationContext(IOC容器的接口)
* 2)、给容器中注册一个组件;我们也从容器中按照id拿到了这个组件的对象?
* 组件的创建工作,是容器完成;
* Person对象是什么时候创建好了呢?
* 容器中对象的创建在容器创建完成的时候就已经创建好了;
* 3)、同一个组件在ioc容器中是单实例的;容器启动完成都已经创建准备好的;
* 4)、容器中如果没有这个组件,获取组件?报异常
* org.springframework.beans.factory.NoSuchBeanDefinitionException:
* No bean named 'person03' is defined
* 5)、ioc容器在创建这个组件对象的时候,(property)会利用setter方法为javaBean的属性进行赋值;
* 6)、javaBean的属性名是由什么决定的?getter/setter方法是属性名;set去掉后面那一串首字母小写就是属性名;
* private String lastName;?
* 所有getter/setter都自动生成!
*/
/**
* 从容器中拿到这个组件
*/
@Test
public void test() {
//ApplicationContext:代表ioc容器
//ClassPathXmlApplicationContext:当前应用的xml配置文件在 ClassPath下
//根据spring的配置文件得到ioc容器对象
//ApplicationContext ioc = new ClassPathXmlApplicationContext("com/atguigu/bean/ioc.xml");
ApplicationContext ioc = new ClassPathXmlApplicationContext("ioc.xml");
//容器帮我们创建好对象了;
System.out.println("容器启动完成....");
Person bean = (Person) ioc.getBean("person01");
Object bean2 = ioc.getBean("person01");
System.out.println(bean == bean2);
System.out.println("==================");
Object bean3 = ioc.getBean("person03");
}
二、IOC和Bean的配置
1 IOC和DI
1.1 IOC(Inversion of Control):反转控制。
在应用程序中的组件需要获取资源时,传统的方式是组件主动的从容器中获取所需要的资源,在这样的模式下开发人员往往需要知道在具体容器中特定资源的获取方式,增加了学习成本,同时降低了开发效率。
反转控制的思想完全颠覆了应用程序组件获取资源的传统方式:反转了资源的获取方向——改由容器主动的将资源推送给需要的组件,开发人员不需要知道容器是如何创建资源对象的,只需要提供接收资源的方式即可,极大的降低了学习成本,提高了开发的效率。这种行为也称为查找的被动形式。
1.2 DI(Dependency Injection):依赖注入。
IOC的另一种表述方式:即组件以一些预先定义好的方式(例如:setter 方法)接受来自于容器的资源注入。相对于IOC而言,这种表述更直接。
1.3 IOC容器在Spring中的实现
[1]在通过IOC容器读取Bean的实例之前,需要先将IOC容器本身实例化。
[2]Spring提供了IOC容器的两种实现方式
(1)BeanFactory:IOC容器的基本实现,是Spring内部的基础设施,是面向Spring本身的,不是提供给开发人员使用的。
(2)ApplicationContext:BeanFactory的子接口,提供了更多高级特性。面向Spring的使用者,几乎所有场合都使用ApplicationContext而不是底层的BeanFactory。
1.4 ApplicationContext的主要实现类
[1]ClassPathXmlApplicationContext:对应类路径下的XML格式的配置文件
[2]FileSystemXmlApplicationContext:对应文件系统中的XML格式的配置文件
[3]在初始化时就创建单例的bean,也可以通过配置的方式指定创建的Bean是多实例的。
1.5 ConfigurableApplicationContext
[1]是ApplicationContext的子接口,包含一些扩展方法
[2]refresh()和close()让ApplicationContext具有启动、关闭和刷新上下文的能力。
1.6 WebApplicationContext
专门为WEB应用而准备的,它允许从相对于WEB根目录的路径中完成初始化工作
2 通过类型获取bean
从IOC容器中获取bean时,除了通过id值获取,还可以通过bean的类型获取。但如果同一个类型的bean在XML文件中配置了多个,则获取时会抛出异常,所以同一个类型的bean在容器中必须是唯一
的。
HelloWorld helloWorld = cxt.getBean(HelloWorld.class);
3 给bean的属性赋值
3.1 赋值的途经
①通过bean的setXxx()方法赋值
HelloWorld中使用的就是这种方式
②通过bean的构造器赋值
<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 >
●通过索引index
值指定参数位置
<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 >
●通过类型type
不同区分重载的构造器
<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 >
③给bean的级联属性赋值
<bean id="action" class="com.atguigu.spring.ref.Action">
<property name="service" ref="service"/>
<!-- 设置级联属性(了解) -->
<property name="service.dao.dataSource" value="DBCP"/>
</bean>
④p名称空间
为了简化XML文件的配置,越来越多的XML文件采用属性而非子元素配置信息。
Spring从2.5版本开始引入了一个新的p命名空间,可以通过<bean>
元素属性的方式配置Bean的属性。
使用p命名空间后,基于XML的配置方式将进一步简化。
<bean
id="studentSuper"
class="com.atguigu.helloworld.bean.Student"
p:studentId="2002" p:stuName="Jerry2016" p:age="18" />
3.2可以使用的值
①字面量
[1]可以使用字符串表示的值,可以通过value属性或value子节点的方式指定
[2]基本数据类型及其封装类、String等类型都可以采取字面值注入的方式
[3]若字面值中包含特殊字符,可以使用<![CDATA[]]>
把字面值包裹起来
②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 >
③外部已声明的bean
<bean id="shop" class="com.atguigu.spring.bean.Shop" >
<property name= "book" ref ="book"/>
</bean >
④内部bean
当bean实例仅仅给一个特定的属性使用时,可以将其声明为内部bean。内部bean声明直接包含在<property>
或<constructor-arg>
元素里,不需要设置任何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" />