一、Spring概述
图解
1、Spring 有两个核心部分:IOC 和 Aop
(1)IOC(Inversion od Control):控制反转,把创建对象过程交给 Spring 进行管理,又叫依赖注入(DI:Dependency Injection)
(2)Aop(Aspect Oriented Programming):面向切面,不修改源代码进行功能增强
2、Spring 特点
(1)方便解耦,简化开发
(2)Aop 编程支持
(3)方便程序测试
(4)方便和其他框架进行整合
(5)方便进行事务操作
(6)降低 API 开发难度
(7)Java源码是经典学习范例
Spring核心组件有三个 :Core、Context、Beans他们构建了整个Spring骨架。
二、 Spring入门程序
1.在pom.xml中添加spring相关依赖
<!-- https://mvnrepository.com/artifact/org.springframework/spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>5.2.7.RELEASE</version>
<type>pom</type>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
2.在main/resources创建beans.xml文件(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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
3.编写组件完成操作
public class HelloWorld {
public String sayHello(String name){
return "Hello"+name+"!";
}
}
4.在spring的配置文件中声明定义这个bean
<!--如下节点相当于调用无参构造方法完成对象的实例化:
Hello hello = new HelloWorld()
id:唯一表示
class:类全名
-->
<bean id="hello" class="com.acoffee.HelloWorld"></bean>
5.测试
public class AppTest {
private Logger logger = Logger.getLogger(this.getClass());
@Test
public void testHelloWorld() {
//获取容器对象
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Object hello = context.getBean("hello");//获取beans.xml中的对象
if (hello instanceof HelloWorld){
HelloWorld hw = (HelloWorld) hello;
logger.info(hw.sayHello("World"));
}
}
}
执行结果:
三、Spring核心容器
Spring 提供了两种核心容器,分别是BeanFactory 和 AppectionContext
1.BeanFactory
BeanFactory 是基础类型的IOC容器,它提供了完整的IOC服务支持,简单来说,BeanFactory就是一个管理Bean的工厂,它主要负责初始化各种Bean,并调用它们的生命周期方法。它有多个实现类,最常见的是XMLBeanFactory 但是已经过时了.
@Test
public void testBeanFactory(){
BeanFactory BeanFactory = new XmlBeanFactory(new FileSystemResource("D:\\code\\Acoffee-Spring\\spring-01-quickstart\\src\\main\\resources\\beans.xml"));//文件系统中寻找指定的XML配置文件
HelloWorld hw = (HelloWorld) BeanFactory.getBean("hello");
String world = hw.sayHello("World");
logger.info(world);
}
2.ApplicationContext
ApplicationContext是BeanFactory 的子接口,ApplicationContext不仅包含了BeanFactory的所有功能,还添加了对国际化,资源访问,事件传播等方面的支持。
两个常用类:
类 | 作用 |
---|---|
ClassPathXmlApplicationContext | 该类从类路径中寻找指定的XML配置文件 |
FileSystemXmlApplicationContext | 该类从制定的文件系统中寻找指定的XML配置文件 |
在WEB项目中,ApplicationContext容器的实例化工作会交由WEB服务器完成,WEB服务器实例化ApplicationContext容器通常使用基于ContextLoaderListener实现的方式.
3.BeanFactory和ApplicationContext的主要区别
1.BeanFactory和ApplicationContext接口,创建对象的时机不同!
2.BeanFactory是在getBean的时候创建对象!
3.ApplicationContext是在获取配置文件的时候创建的对象!|
如果Bean的某一个属性没有注入,则使用BeanFactory加载后,在第一次调用getBean()方法时会抛出异常,而ApplicationContext则在初始化自检,这样有利于检查所依赖的属性是否注入。
因此,在实际开发过程中,通常选择使用ApplicationContext,而只有在资源较少的情况下,才考虑使用BeanFactory。