1. Spring
- 框架(可以理解为一个半成品的软件)是高度抽取可重用代码的一种设计
- 具有高度的通用性、事务控制的功能,形成一个某个领域的整体解决方案
1.1 Spring模块划分
jar包三个一组:
开发中需导入的jar包
spring-aop-4.0.0.RELEASE.jar
文档
spring-aop-4.0.0.RELEASE-javadoc.jar
源码包
spring-aop-4.0.0.RELEASE-sources.jar
1.2 Spring模块划分图:
① Test——Spring的单元测试模块
spring-test-4.0.0.RELEASE
② Core Container——核心容器(IOC);要使用这个部分的完整功能,需要导入Beans、Core、Context、SpEL的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
④ Data Access/Integration——Spring数据库访问模块
Data Access模块:
spring-jdbc-4.0.0.RELEASE
spring-orm(Object Relation Mapping)-4.0.0.RELEASE
spring-tx-4.0.0.RELEASE(事务)
Integration模块:
spring-ox(xml)m-4.0.0.RELEASE
spring-jms-4.0.0.RELEASE、(Intergration)
⑤ Web——Spring开发web应用的模块
spring-websocket(新技术)-4.0.0.RELEASE
spring-web-4.0.0.RELEASE(和原生的web相关——servlet)
spring-webmvc-4.0.0.RELEASE(开发web项目)
spring-webmvc-portlet-4.0.0.RELEASE(开发web应用的组件集成)
此外,spring运行的时候依赖一个日志包:
commons-logging-1.1.3.jar
2. IOC(Inversion Of Control)
- IOC(Inversion Of Control),即控制反转。控制指的是资源的获取方式,分为主动式和被动式;
- 应用程序中的组件要获取资源时,传统的方式是组件主动的从容器中获取所需要的资源,在这样的模式下开发人员往往需要知道在具体容器中特定资源的获取方式,同时降低了开发效率。
- 反转控制的颠覆了应用程序组件获取资源的传统方式:反转了资源的获取方向——改由容器主动的将资源推送给需要的组件,开发人员不需要知道容器是如何创建资源对象的,只需要提供接收资源的方式即可提高了开发的效率。这种行为也称为查找的被动形式。
使主动的new资源变为被动的接受资源
DI(Dependency Injection)依赖注入
IOC的具体实现的描述:组件以一些预先定义好的方式(如setter
方法)接受来自于容器的资源注入。相对于IOC而言,这种表述更直接。
流程
①导包
核心容器
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运行的时候依赖一个日志包;没有就报错;
②写配置
创建一个Spring Bean Configuration File(Spring的bean配置文件),集合了spring的ioc容器管理的所有组件
<!-- 注册一个Person对象,Spring会自动创建这个Person对象 -->
<!--
一个Bean标签可以注册一个组件(对象、类)
class:写要注册的组件的全类名
id:这个对象的唯一标示;
-->
<bean id="person01" class="com.atguigu.bean.Person">
<!--使用property标签为Person对象的属性赋值
name="lastName":指定属性名
value="张三":为这个属性赋值
-->
<property name="lastName" value="张三"></property>
<property name="age" value="18"></property>
<property name="email" value="zhangsan@atguigu.com"></property>
<property name="gender" value="男"></property>
</bean>
③测试
package com.atguigu.test;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.atguigu.bean.Person;
public class IOCTest {
/**
* 从容器中拿到这个组件
*/
@Test
public void test() {
//ApplicationContext:代表ioc容器
//ClassPathXmlApplicationContext:当前应用的xml配置文件在 ClassPath下
//根据spring的配置文件得到ioc容器对象
ApplicationContext ioc = new ClassPathXmlApplicationContext("ioc.xml");
//容器帮我们创建好对象了;
Person bean = (Person) ioc.getBean("person01");
System.out.println(bean);
}
}
解释:
(1)ApplicationContext是IOC容器的接口
new ClassPathXMlApplicationContext("ioc.xml")
——ioc容器的配置文件在类路径下FileSystemXmlApplicationContext("F://ioc.xml")
——ioc容器的配置文件在磁盘路径下
(2)给容器中注册一个组件,这个组件的创建工作是由容器完成; Person对象容器中对象的创建在ioc容器创建完成的时候就已经创建好了;
ApplicationContext ioc = new ClassPathXmlApplicationContext("ioc.xml");
(3)同一个组件在ioc容器中是单实例的;容器启动完成都已经创建准备好;
(4)若容器中如果没有某个组件,则获取组件会报异常:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean
named 'person03' is defined
(5)ioc容器在创建组件对象时,<property>
标签会利用setter
方法为javaBean的属性赋值;
(6)javaBean的属性名是由什么决定的?getter/setter
方法是属性名;set去掉后面一串首字母小写就是属性名;
注意事项:
(1)src是源码包开始的路径,称为类路径的开始;所有源码包里面的东西都会被合并放在类路径里面;
- 普通java项目的类路径:/bin/
- web项目的类路径:/WEB-INF/classes/
(2)先导包再创建配置文件;