第一章Spring简介和IOC
2018-03-08 22:14
274 查看
一.简介
1.Spring(framework):提供某一领域专业的完整解决方案框架,是一个开源的简化企业级开发的容器框架。或者说Spring 是一个 IOC(DI) 和 AOP 容器框架.
2.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)。
3.Spring的模块
Jar包描述:核心容器模块:
AOP+Aspects模块
数据库访问模块
web应用开发模块
单元测试模块
第三方集成
消息框架
检测代理
二、第一个helloworld程序1.第一个helloworld程序:标准的JavaBean对象: ①权限必须是Public ②必须有一个无参构造器 ③为属性创建getter,setter方法
测试类代码:public void test() { //方式一:通过类路径加载一个配置文件 ApplicationContext context = new ClassPathXmlApplicationContext("hello.xml"); //方式二:文件系统(磁盘路径)加载一个配置文件 //ApplicationContext context = new FileSystemXmlApplicationContext("D:\\MyEclipse\\MySpring\\Spring-day01\\config\\hello.xml"); //方式一:通过id获取 Person person = (Person) context.getBean("person"); //方式二:通过类型获取 //Person person = (Person) context1.getBean(Person.class); //方式三:通过id和类型获取 //Person bean = context.getBean("person",Person.class); System.out.println(person); }
spring 配置文件: <!-- 使用bean 标签定义一个bean(对象) class:要创建bean的全类名 id:bean的唯一标识 --> <bean class="com.nengjie.helloworld.Person" id="person"> <!-- 如果property属性中的值没有内容,可以选择使用单标签 --> <!-- 使用 property标签为对象的属性赋值 name:属性名 value:属性值--> <property name="name" value="小刚"></property> <property name="age" value="19"></property> </bean> <bean class="com.nengjie.helloworld.Person" id="person1"> <property name="name" value="小明" /> <property name="age" value="20" /> </bean>
三、helloworld的实现细节1.ClassPathXmlApplicationContext:根据类路径加载一个配置文件,创建ApplicationContext对象类路径(src路径):源码路径,里面的文件会被存放在:
java工程: 工程目录/bin/
web工程:工程目录/build/classes
FileSystemXmlApplicationContext:根据文件系统(磁盘路径)加载一个配置文件,创建ApplicationContext对象
文件系统(磁盘路径):xml文件的存放路径
2.getBean(): 是从容器中取得创建好的bean,有三种方式:getBean(String id):根据指定id从容器中获取对象
如果找不到指定id:报错,NoSuchBeanDefinitionException
返回object类型,使用的话需要强转
getBean(class T):根据类型获取指定的bean;
找不到,报错:NoSuchBeanDefinitionException
找到多个同一类型:报错NoUniqueBeanDefinitionException
保证容器中有唯一同一类型的bean
getBean(String id,Class T):根据指定的id和类型获取指定的bean
3.容器中对象的创建时机:随着容器的创建而创建; 指定Id的对象,在容器中只会被创建一次(单例模式) 容器中默认创建的 bean是单例的,在容器创建的时候创建
如果容器中的bean是多实例的,那么在调用getbean()方法的时候创建
四、BeanFactory & ApplicationContext1.BeanFactory: 访问spring容器的根接口,面向Spring本身
The root interface for accessing a Spring bean container. 2.ApplicationContext:为应用程序提供配置的核心接口 ;是BeanFactory的子接口,功能更强大,为开发人员使用; Central interface to provide configuration for an application;
1.Spring(framework):提供某一领域专业的完整解决方案框架,是一个开源的简化企业级开发的容器框架。或者说Spring 是一个 IOC(DI) 和 AOP 容器框架.
2.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)。
3.Spring的模块
Jar包描述:核心容器模块:
org.springframework spring-beans | bean支持,包括Groovy |
org.springframework spring-context | 运行时上下文,包括调度和远程调用抽象 |
org.springframework spring-core | 核心库,被许多其它模块使用 |
org.springframework spring-expression | Spring表达式语言 |
org.springframework spring-aop | 基于代理的AOP |
org.springframework spring-aspects | 基于切面的AspectJ |
org.springframework spring-jdbc | JDBC支持包,包括对数据源设置和JDBC访问支持 |
org.springframework spring-orm | 对象关系映射,包括对JPA和Hibernate支持 |
org.springframework spring-oxm | 对象XML映射 |
org.springframework spring-tx | 事务基础,包括对DAO的支持及JCA的集成 |
org.springframework spring-jms | JMS支持包,包括发送和接收JMS消息的帮助类 |
org.springframework spring-web | web支持包,包括客户端及web远程调用 |
org.springframework spring-webmvc | REST web服务及web应用的MVC实现 |
org.springframework spring-webmvc-portlet | 用于Portlet环境的MVC实现 |
org.springframework spring-websocket | WebSocket和SockJS实现,包括对STOMP的支持 |
org.springframework spring-test | 单元测试和集成测试组件 |
org.springframework spring-context-support | 包含用于集成第三方库到Spring上下文的类 |
org.springframework spring-messaging | 消息处理的架构和协议 |
spring-instrument-4.0.0.RELEASE | JVM引导的检测代理 |
org.springframework spring-instrument-tomcat | tomcat的检测代理 |
测试类代码:public void test() { //方式一:通过类路径加载一个配置文件 ApplicationContext context = new ClassPathXmlApplicationContext("hello.xml"); //方式二:文件系统(磁盘路径)加载一个配置文件 //ApplicationContext context = new FileSystemXmlApplicationContext("D:\\MyEclipse\\MySpring\\Spring-day01\\config\\hello.xml"); //方式一:通过id获取 Person person = (Person) context.getBean("person"); //方式二:通过类型获取 //Person person = (Person) context1.getBean(Person.class); //方式三:通过id和类型获取 //Person bean = context.getBean("person",Person.class); System.out.println(person); }
spring 配置文件: <!-- 使用bean 标签定义一个bean(对象) class:要创建bean的全类名 id:bean的唯一标识 --> <bean class="com.nengjie.helloworld.Person" id="person"> <!-- 如果property属性中的值没有内容,可以选择使用单标签 --> <!-- 使用 property标签为对象的属性赋值 name:属性名 value:属性值--> <property name="name" value="小刚"></property> <property name="age" value="19"></property> </bean> <bean class="com.nengjie.helloworld.Person" id="person1"> <property name="name" value="小明" /> <property name="age" value="20" /> </bean>
三、helloworld的实现细节1.ClassPathXmlApplicationContext:根据类路径加载一个配置文件,创建ApplicationContext对象类路径(src路径):源码路径,里面的文件会被存放在:
java工程: 工程目录/bin/
web工程:工程目录/build/classes
FileSystemXmlApplicationContext:根据文件系统(磁盘路径)加载一个配置文件,创建ApplicationContext对象
文件系统(磁盘路径):xml文件的存放路径
2.getBean(): 是从容器中取得创建好的bean,有三种方式:getBean(String id):根据指定id从容器中获取对象
如果找不到指定id:报错,NoSuchBeanDefinitionException
返回object类型,使用的话需要强转
getBean(class T):根据类型获取指定的bean;
找不到,报错:NoSuchBeanDefinitionException
找到多个同一类型:报错NoUniqueBeanDefinitionException
保证容器中有唯一同一类型的bean
getBean(String id,Class T):根据指定的id和类型获取指定的bean
3.容器中对象的创建时机:随着容器的创建而创建; 指定Id的对象,在容器中只会被创建一次(单例模式) 容器中默认创建的 bean是单例的,在容器创建的时候创建
如果容器中的bean是多实例的,那么在调用getbean()方法的时候创建
四、BeanFactory & ApplicationContext1.BeanFactory: 访问spring容器的根接口,面向Spring本身
The root interface for accessing a Spring bean container. 2.ApplicationContext:为应用程序提供配置的核心接口 ;是BeanFactory的子接口,功能更强大,为开发人员使用; Central interface to provide configuration for an application;
相关文章推荐
- Spring学习(一)简介及IOC的小案例
- J2EE进阶学习——Spring框架(一):Spring简介和IOC简介
- 第一章 Shiro简介——跟我学习springmvc shiro mybatis
- 第一章 Spring简介
- Spring教程____Spring简介(核心IOC和AOP)
- spring系列(一)——简介和IOC
- Spring(二):Spring Ioc简介
- 第一章 Shiro简介——跟我学习springmvc shiro mybatis
- Spring 核心IOC, AOP简介
- Spring IOC容器启动简介
- Spring源码之IOC--第一章
- spring3.2.5学习(一)——spring环境配置以及IOC简介
- Spring源码之IOC--第一章
- [spring源码学习]一、IOC简介
- Spring 系列: Spring 框架简介- Spring AOP 和 IOC 容器入门
- Spring IOC简介(代码)
- spring的入门(aop,ioc,bean作用域等)简介
- Spring核心技术(一)——IoC容器和Bean简介
- Spring 系列: Spring 框架简介- Spring AOP 和 IOC 容器入门
- [Spring学习笔记 1 ] Spring 简介,初步知识--Ioc容器详解 基本原理。