Spring简介
单体地狱:Servlet JDBC JSP …
替代JDBC的框架: MyBatis;
Spring:春天—>给软件行业带来春天
2002,Rod johnson 首次推出了Spring框架的前身 interface21框架
2003,Spring框架以interface21框架为基础,重新设计,发布1.0正式版。
Spring官网:https://spring.io/
Spring下载:https://spring.io/projects/spring-framework#learn
版本说明:
我们在下载软件会遇到诸如release,stable,alpha,beta,pre,current,eval,rc,snapshot等版本,程序猿下载插件时尤为常见,现在我说明以下版本的意思
1,snapshot(快照),也即开发版,我们创建maven项目时,编辑器会自动给我们填入 1.0-SNAPSHOT版本,也就是1.0开发版,这个版本不能使用,因为该版本处于开发的过程,所以运行时会不时地更新,导致功能变化,正式环境中不得使用snapshot版本的库;
2,alpha,内部测试版,来源于字母α,是比较早的版本,主要是给开发人员和测试人员测试和找BUG用的,不建议使用;
3,beta,公开测试版,来源于字母β,这是比alpha进一步的版本,面向公众测试,但是不建议使用
4,pre,这个和alpha版本类似,有时还会细分为M1,M2版本,不建议使用;
5,RC(Release Candidate) 顾名思义么 ! 用在软件上就是候选版本。系统平台上就是发行候选版本;
6,GA(General Availability)正式发布的版本,在国外都是用GA来说明release版本的;
7,release,发行版,这是release的中文意思,也就是官方推荐使用的版本;
8,stable,稳定版,这个版本相比于测试版更加稳定,去除了测试版许多的bug,完善了一些功能,建议使用;
9,current,最新版,但是不一定是稳定版本,需要看一下是否还有release或者stable等版本;
10,eval,评估版。可能会有一个月或者固定时间的使用期限;
SpringGithub地址:https://github.com/spring-projects/spring-framework
Spring官网下载地址:https://repo.spring.io/release/org/springframework/spring/
Spirng优点
- Spring是开源,免费的。
- Spring是一个轻量级的框架,非入侵式的框架。
- 没有任何其他的依赖,使用他,不需要导入其他的包。
- IOC AOP
- 控制反转
- 面向切面编程
- 对事务的支持
- 对框架的支持,不需要任何成本就可以引入其他框架。MyBatis、Hibernate,Strut1/2,SpringMVC
- SSH、SSM
重要:一句话来概括:Spring是一个轻量级的控制反转【IOC】和面向切面【AOP】的(容器)框架。
OOP: 面向对象编程
Spring想做粘合剂。—> 发展出来一套自己的生态;
Spring组成
架构 : mvc三层架构
Spring 框架是一个分层架构,由 7 个定义良好的模块组成。Spring 模块构建在核心容器之上,核心容器定义了创建、配置和管理 bean 的方式。
组成 Spring 框架的每个模块(或组件)都可以单独存在,或者与其他一个或多个模块联合实现。每个模块的功能如下:
- 核心容器:核心容器提供 Spring 框架的基本功能。核心容器的主要组件是
BeanFactory
,它是工厂模式的实现。BeanFactory
使用控制反转 (IOC) 模式将应用程序的配置和依赖性规范与实际的应用程序代码分开。 - Spring 上下文:Spring 上下文是一个配置文件,向 Spring 框架提供上下文信息。Spring 上下文包括企业服务,例如 JNDI、EJB、电子邮件、国际化、校验和调度功能。
- Spring AOP:通过配置管理特性,Spring AOP 模块直接将面向方面的编程功能集成到了 Spring 框架中。所以,可以很容易地使 Spring 框架管理的任何对象支持 AOP。Spring AOP 模块为基于 Spring 的应用程序中的对象提供了事务管理服务。通过使用 Spring AOP,不用依赖 EJB 组件,就可以将声明性事务管理集成到应用程序中。
- Spring DAO:JDBC DAO 抽象层提供了有意义的异常层次结构,可用该结构来管理异常处理和不同数据库供应商抛出的错误消息。异常层次结构简化了错误处理,并且极大地降低了需要编写的异常代码数量(例如打开和关闭连接)。Spring DAO 的面向 JDBC 的异常遵从通用的 DAO 异常层次结构。
- Spring ORM:Spring 框架插入了若干个 ORM 框架,从而提供了 ORM 的对象关系工具,其中包括 JDO、Hibernate 和 iBatis SQL Map。所有这些都遵从 Spring 的通用事务和 DAO 异常层次结构。
- Spring Web 模块:Web 上下文模块建立在应用程序上下文模块之上,为基于 Web 的应用程序提供了上下文。所以,Spring 框架支持与 Jakarta Struts 的集成。Web 模块还简化了处理多部分请求以及将请求参数绑定到域对象的工作。
- Spring MVC 框架:MVC 框架是一个全功能的构建 Web 应用程序的 MVC 实现。通过策略接口,MVC 框架变成为高度可配置的,MVC 容纳了大量视图技术,其中包括 JSP、Velocity、Tiles、iText 和 POI。
Spring 框架的功能可以用在任何 J2EE 服务器中,大多数功能也适用于不受管理的环境。Spring 的核心要点是:支持不绑定到特定 J2EE 服务的可重用业务和数据访问对象。毫无疑问,这样的对象可以在不同 J2EE 环境 (Web 或 EJB)、独立应用程序、测试环境之间重用。
Spring的组件:
SpringBoot 和 Spring Cloud
Spring Boot :一款可以快速构建Spring应用的脚手架; 约定大于配置 , 承上启下。
Spring Cloud:基于SpringBoot实现的;他是一个生态圈;
IOC推导
◆目的:解决企业应用开发的复杂性
◆功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能
◆范围:任何Java应用
Spring是一个轻量级控制反转(IoC)和面向切面(AOP)的容器框架。
分析实现
我们使用原来的方式写一段代码测试:dao–service—前端
思考:现在前端传递或者调用不会变,所有操作都是我们程序猿来实现;
解决方案:前端操作,后台不变;留一个调用的接口
IOC的原型。
IOC的本质
控制反转 (inversion of Control):IOC
他是一种设计思想。 根据这个思想有实现的方式,DI : 依赖注入。
本质就是设置接口 , set方法,是一种抽象思维
反转: 从被动到主动 , 程序猿主动控制 到 被动接受 , 由编写代码,编写接口,程序具有高度配置性和动态性;
让程序的耦合性大大降低。方便模块独立:----> 微服务。解耦 。
HelloSpring
1.导入jar包
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
2.创建实体类
package com.kuang.pojo;
public class Hello {
private String name;
public Hello() {
}
public Hello(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void show(){
System.out.println("Hello,"+name);
}
@Override
public String toString() {
return "Hello{" +
"name='" + name + '\'' +
'}';
}
}
3.编写Spring的配置文件
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--bean就是一个java对象,由Spring管理和创建-->
<bean id="hello" class="com.kuang.pojo.Hello">
<property name="name" value="Spring"/>
</bean>
<!--bean就是一个java对象,由Spring管理和创建-->
<bean id="hello2" class="com.kuang.pojo.Hello">
<property name="name" value="WestOS"/>
</bean>
<!--// Hello hello2 = new Hello();-->
<!--// hello2.setName (WestOS)-->
</beans>
4.测试类
package com.kuang.pojo;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloTest {
@Test
public void test(){
//解析beans.xml配置文件,生产管理相应的Bean对象;
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//通过Bean得到这个对象的实体
Hello hello = (Hello)context.getBean("hello2");
hello.show();
}
}
思考问题?
- Hello对象是由谁创建的?
- hello对象是由Spring创建的。
- beans.xml ----> 管理bean,Java对象 , Spring就像一个容器,存放了许多的对象;
- 解析配置文件
- 通过反射创建对象并且设置值
- Hello对象的属性是怎么设置值的?
- hello对象的属性是由Spring容器创建的。
这个过程就叫控制反转:IOC
控制:谁来控制对象的创建? 原来 :程序猿来控制, 使用Spring后,Spring来创建对象的
反转:程序本身不创建对象了,变成被动接受对象。
依赖注入:DI ,本质就是利用set方式来注入的。
IOC是一种编程思想。由主动的编程到被动的接受;
修改测试Demo
使用spring完成之前 Oracle 和 Mysql 获取用户信息的小Demo;
项目结构:
pom.xml 文件配置:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.kuang</groupId>
<artifactId>spring-study</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>Spring01</module>
<module>Spring02</module>
<module>Spring03</module>
<module>Spring04</module>
<module>Spring05</module>
<module>Spring06</module>
<module>Spring07</module>
</modules>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
</dependencies>
</project>
pojo实体类:
package com.kuang.pojo;
public class Hello {
private String name;
public Hello() {
}
public Hello(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void show(){
System.out.println("Hello,"+name);
}
@Override
public String toString() {
return "Hello{" +
"name='" + name + '\'' +
'}';
}
}
resources资源包配置:
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--bean就是一个java对象,由Spring管理和创建-->
<bean id="hello" class="com.kuang.pojo.Hello">
<property name="name" value="Spring"/>
</bean>
<!--bean就是一个java对象,由Spring管理和创建-->
<bean id="hello2" class="com.kuang.pojo.Hello">
<property name="name" value="WestOS"/>
</bean>
<!--// Hello hello2 = new Hello();-->
<!--// hello2.setName (WestOS)-->
</beans>
Test测试类:
package com.kuang.pojo;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloTest {
@Test
public void test(){
//解析beans.xml配置文件,生产管理相应的Bean对象;
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//通过Bean得到这个对象的实体
Hello hello = (Hello)context.getBean("hello2");
hello.show();
}
}
测试结果:
如果把测试类中hello2改为hello则输出结果为:对应得为bean.xml文件value值;
OK , 到了现在 , 我们彻底不用再程序中去改动了 , 要实现不同的操作 , 只需要在xml配置文件中进行修改 , 所谓的IoC,一句话搞定 : 对象由Spring 来创建 , 管理 , 装配 !
IOC创建对象的方式
我们平时创建对象的方式:
有参构造 、无参构造
我们来看看在Spring中怎么处理这两种情况
项目建立:
项目结构:
** 创建实体类**
User:
package com.kuang.pojo;
public class User {
private String name;
private String sex;
private int age;
public User() {
System.out.println("User的无参构造");
}
public User(String name) {
System.out.println("User的有参构造");
this.name = name;
}
public User(String name, int age) {
System.out.println("User的有参构造2");
this.name = name;
this.age = age;
}
public User(String name, String sex, int age) {
System.out.println("User的有参构造3");
this.name = name;
this.sex = sex;
this.age = age;
}
public void setName(String name) {
System.out.println(name+":"+System.currentTimeMillis());
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
Spring配置文件
beans.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!--suppress SpringFacetInspection -->
<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">
<!--
Spring的配置文件
bean 代表一个对象
alias 别名
import 导入一个其他资源
-->
<bean id="user" class="com.kuang.pojo.User">
<property name="name" value="qinjiang"/>
</bean>
<!--使用构造器的参数下标进行赋值-->
<bean id="user2" class="com.kuang.pojo.User">
<constructor-arg index="0" value="kuangshen"/>
<constructor-arg index="1" value="18"/>
</bean>
<!--通过名字进行赋值-->
<bean id="user3" class="com.kuang.pojo.User">
<constructor-arg name="name" value="kuangshen3"/>
<constructor-arg name="age" value="3"/>
</bean>
<!--通过类型进行赋值-->
<bean id="user4" class="com.kuang.pojo.User">
<constructor-arg type="java.lang.String" value="kuangshen4"/>
</bean>
</beans>
测试类
package com.kuang.pojo;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserTest {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
User user = (User)context.getBean("user");
/*
User user = new User();
user.setName("qinjiang");
*/
System.out.println(user.toString());
}
@Test
public void test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
User user = (User) context.getBean("user4");
System.out.println(user);
}
}
测试结果:
小结:
- 通过有参构造
- 通过下标
- 通过参数名 【推荐】
- 通过参数类型
- 通过无参构造
- 默认会用无参构造
注意点:一定要有一个无参构造方法
想多了解一点的可以查查:通过工厂模式创建
Spring配置文件
bean
<!--bean讲解:
1.没有id,没有name,我们依旧可以获取这个对象,但是不建议使用、需要使用类的class对象获取;
User bean = context.getBean(User.class);
2.id 就是对象的名字
3.class 就是要实例化的类
4.name就是别名
有id的时候,name是别名 , 没有id的时候,name就是对象的名字
别名可以起多个
-->
<bean id="user" name="userTwo,user3" class="com.kuang.pojo.User">
<property name="name" value="Spring"/>
</bean>
alias
<!--alias讲解:
1. name : 就是bean对象的id
2,alias : 对象的别名
-->
<alias name="user" alias="user4"/>
import
<!--import讲解
作用:导入另外一个资源,把另外配置文件装进来
classpath*: 他会去所有地方寻找你的文件。【效率低】
classpath: 只会在classpath中寻找文件,找不到就报错;【建议使用】
file:填写文件路径url【不建议使用】
http:填写网络路径url【不建议使用】
-->
<import resource="classpath*:userBeans.xml"/>
<import resource="classpath:userBeans.xml"/>
<import resource="file:"/>
<import resource="http:"/>
import一般在团队项目中会使用,每个人开发各自的beans.xml,最后用一个总的文件拼装起来。
依赖注入DI
依赖注入
DI (Dependency Injection)
依赖:指bean对象的创建依赖于Spring容器。bean对象依赖的资源
注入:指Bean对象所依赖的资源,由容器来设置和装配。
构造器注入
- 有参
- 无参
setter注入
要求被注入的属性,必须有set方法。set方法的名字需要规范
set+属性名(属性名字母大写);
1.1 常量注入
<!--普通字段-->
<property name="name" value="小明"/>
1.2 Bean注入
<!--引用其他bean使用ref-->
<property name="address" ref="addr"/>
1.3 数组注入
<!--数组的注入-->
<property name="books">
<array>
<value>西游记</value>
<value>水浒传</value>
<value>红楼梦</value>
<value>三国演义</value>
</array>
</property>
1.4 List注入
<!--List注入-->
<property name="hobbys">
<list>
<value>女孩</value>
<value>代码</value>
<value>电影</value>
<value>音乐</value>
</list>
</property>
1.5 Map注入
标签:entry
键:使用key
值: 使用value
<!--Map的注入-->
<property name="card">
<map>
<entry key="IdCard" value="666666888888884444"/>
<entry key="银行卡" value="111122223333444"/>
</map>
</property>
1.6 Set注入
<!--Set注入-->
<property name="games">
<set>
<value>王者荣耀</value>
<value>贪玩蓝月</value>
<value>绝地求生</value>
<value>LOL</value>
</set>
</property>
1.7 空值注入
<!--Null空值注入-->
<property name="girlFriend">
<null/>
</property>
1.8 Properties注入
props标签
键使用key
值,在标签中间;
<!--Properties注入-->
<property name="info">
<props>
<prop key="学号">201932301</prop>
<prop key="性别">男</prop>
<prop key="姓名">小明</prop>
</props>
</property>
拓展1:p命名空间注入
<!--p:property属性,命名空间注入-->
<bean id="user1" class="com.kuang.pojo.User" p:name="狂神" p:age="18"/>
拓展2:c命名空间注入
<!--c:constructor构造器:命名空间注入-->
<bean id="user2" class="com.kuang.pojo.User" c:name="狂神" c:age="19"/>
Spring就是一个粘合剂,托管所有的对象;
案例:
项目结构:
pom.xml依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.kuang</groupId>
<artifactId>spring-study</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>Spring01</module>
<module>Spring02</module>
<module>Spring03</module>
<module>Spring04</module>
<module>Spring05</module>
<module>Spring06</module>
<module>Spring07</module>
</modules>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
</dependencies>
</project>
pojo 实体类:
Address:
package com.kuang.pojo;
public class Address {
private String address;
//默认存在无参构造
public void setAddress(String address) {
this.address = address;
}
public String getAddress() {
return address;
}
}
Student:
package com.kuang.pojo;
import java.util.*;
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> games;
private String girlFriend; //null
private Properties info;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public List<String> getHobbys() {
return hobbys;
}
public void setHobbys(List<String> hobbys) {
this.hobbys = hobbys;
}
public Map<String, String> getCard() {
return card;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public Set<String> getGames() {
return games;
}
public void setGames(Set<String> games) {
this.games = games;
}
public String getGirlFriend() {
return girlFriend;
}
public void setGirlFriend(String girlFriend) {
this.girlFriend = girlFriend;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address.toString() +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", card=" + card +
", games=" + games +
", girlFriend='" + girlFriend + '\'' +
", info=" + info +
'}';
}
}
User:
package com.kuang.pojo;
public class User {
private String name;
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--Address-->
<!--
作用域scope:
Web相关
request
session
singleton:单例模式
prototype: 原型
bean中不填写作用域默认, scope="singleton",单例
单例:在内存中只有一个备份
-->
<bean id="addr" class="com.kuang.pojo.Address">
<property name="address" value="西安"/>
</bean>
<!--Student-->
<bean id="student" class="com.kuang.pojo.Student">
<!--普通字段-->
<property name="name" value="小明"/>
<!--引用其他bean使用ref-->
<property name="address" ref="addr"/>
<!--数组的注入-->
<property name="books">
<array>
<value>西游记</value>
<value>水浒传</value>
<value>红楼梦</value>
<value>三国演义</value>
</array>
</property>
<!--List注入-->
<property name="hobbys">
<list>
<value>女孩</value>
<value>代码</value>
<value>电影</value>
<value>音乐</value>
</list>
</property>
<!--Map的注入-->
<property name="card">
<map>
<entry key="IdCard" value="666666888888884444"/>
<entry key="银行卡" value="111122223333444"/>
</map>
</property>
<!--Set注入-->
<property name="games">
<set>
<value>王者荣耀</value>
<value>贪玩蓝月</value>
<value>绝地求生</value>
<value>LOL</value>
</set>
</property>
<!--Null空值注入-->
<property name="girlFriend">
<null/>
</property>
<!--Properties注入-->
<property name="info">
<props>
<prop key="学号">201932301</prop>
<prop key="性别">男</prop>
<prop key="姓名">小明</prop>
</props>
</property>
</bean>
<!--p:property属性,命名空间注入-->
<bean id="user1" class="com.kuang.pojo.User" p:name="狂神" p:age="18"/>
<!--c:constructor构造器:命名空间注入-->
<bean id="user2" class="com.kuang.pojo.User" c:name="狂神" c:age="19" />
</beans>
测试类:
package com.kuang.pojo;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpring {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
}
@Test
public void test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) context.getBean("user1");
User user1 = (User) context.getBean("user1");
User user2 = (User) context.getBean("user1");
User user3 = (User) context.getBean("user1");
System.out.println(user.hashCode());
System.out.println(user1.hashCode());
System.out.println(user2.hashCode());
System.out.println(user3.hashCode());
}
}
输出结果:
Bean作用域及自动装配
在Spring中,那些组成应用程序的主体及由SpringIOC容器所管理的对象,被称之为bean。简单地讲,bean就是由IOC容器初始化、装配及管理的对象。
配置文件中定义 Bean 时,我们不但可以配置 Bean 的属性值以及相互之间的依赖关系,还可以定义 Bean 的作用域 。作用域会对 Bean 的生命周期和创建方式产生影响 。
Bean 的作用域类型:
类型 | 说明 |
---|---|
singleton | 在 Spring 容器中仅存在一个 Bean 实例, Bean 以单例的形式存在,默认值。 |
prototype | 每次从容器中调用 Bean 时,都会返回一个新的实例,即相当于执行 new XxxBean() 的实例化操作。 |
request | 每次 http 请求都会创建一个新的 Bean , 仅用于 WebApplicationContext 环境。request.setAttribute("","") |
session | 同一个 http Session 共享一个 Bean ,不同的 http Session 使用不同的 Bean,仅用于 WebApplicationContext 环境。session.setAttribute("","") |
globalSession | 同一个全局 Session 共享一个 bean, 用于 Porlet, 仅用于 WebApplication 环境。application.setAttribute("","") |
Spring 以容器的方式,使得我们仅需配置,即可得到天然的单例模式。
在五种作用域中,request、session和globalSession三种作用域仅在web的应用中使用。
单例模式
【核心:对象在内存中只有一份】
饿汉式,懒汉式。—> static
区别:加载时机不同.