目录
Spring
Spring 是软件框架, 是软件的半成品, 封装了很多软件需要的功能, 使用Spring开发软件, 可以快速的开发软件产品. 使用Spring就是使用Spring提供的组件功能, 使用时候必须遵守Spring约定.
Spring 核心功能
1. IOC/DI 控制翻转/依赖注入
2. AOP 面向切面(儿)编程
IOC 控制翻转
相对于主动控制管理对象来说, 将对象的管理控制权交给容器管理的现象称为"控制翻转"。
主动控制: 由程序控制管理对象, 适合简单对象的管理。
控制翻转: 由当前的容器环境控制管理对象, 适合复杂对象的管理。
Spring IOC: 由Spring控制管理对象, 应用程序从Spring获得对象, 应用程序再使用对象.
Spring IOC Hello World
为了测试Spring IOC功能, 由简单对象开始.
原理:
步骤:
1. 创建Maven项目导入包:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.22.RELEASE</version>
</dependency>
2. 创建被测试的类
public class DemoBean {
@Override
public String toString() {
return "Hello World!";
}
}
3. 在 resource文件夹中添加配置文件 beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<bean id="demo" class="day01.DemoBean"/>
</beans>
4. 编写测试案例
public class Demo01 {
public static void main(String[] args) {
/*
* 第一次使用Spring
* ClassPathXmlApplicationContext
* 是Spring提供的对象工厂, 用于管理对象
*/
//ctx对象在创建的过程中, 会自动的读取
//beans.xml 并且根据beans.xml 的文件内容
//创建本Spring IOC容器管理的对象
ClassPathXmlApplicationContext ctx=
new ClassPathXmlApplicationContext(
"beans.xml");
//ctx 提供了 getBean 方法可以获取被
//Spring IOC 创建好的对象
DemoBean bean =
(DemoBean)ctx.getBean("demo");
//测试被创建的对象
System.out.println(bean);
}
}
6. 测试
Spring 提供了重载的getBean方法,可以不用类型转换:
@Test
public void testGetBean() {
/*
* Spring 提供了重载的 getBean
* 可以不用写类型转换了
*/
DemoBean bean = ctx.getBean(
"demo", DemoBean.class);
System.out.println(bean);
}
Spring 提供丰富的Java Bean组件管理功能
1. Spring 默认情况下, 采用单例规则管理对象
- 单例时候, 多次调用getBean获得的都是同一个对象的引用
2. 可以设置属性 scope=prototype 管理多个实例
- 多个实例时候, 每次调用getBean都会创建一个新对象
单例:
1. 配置:
<bean id="demo" class="day01.DemoBean"/>
2. 测试:
@Test
public void testSingleton() {
/*
* 默认情况下 Spring 按照单例规则管理对象
* 多次调用getBean返回的是同一个对象的引用
*/
DemoBean b1 = ctx.getBean("demo",
DemoBean.class);
DemoBean b2 = ctx.getBean("demo",
DemoBean.class);
//如果b1和b2引用的是同一个对象
//则b1==b2结果为true
System.out.println(b1==b2);
}
多个实例:
1. 配置
<!-- 设置属性 scope="prototype" 后Spring 按照多个实例管理对象, 每次调用getBean都会创建新对象 -->
<bean id="demo2" class="day01.DemoBean" scope="prototype"></bean>
2. 测试:
@Test
public void testPrototype() {
/*
* 测试Spring创建了多个bean实例
*/
DemoBean b1 = ctx.getBean("demo2",
DemoBean.class);
DemoBean b2 = ctx.getBean("demo2",