45

Spring 核心(IoC)入门解读

 5 years ago
source link: https://www.tuicool.com/articles/vye2MjN
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

点击上方“ 后端技术精选 ”,选择“置顶公众号”

技术文章第一时间送达!

作者:Run

IOC容器

什么是IoC容器和Bean?

说到容器,就是用来装东西,那么这里边装的是什么那?是bean对象。那么你会问什么是bean?这就要从很久很久以前说起,当我们创建对象的时候,我们会new一个对象出来,但是会有这么一个问题,当我们在不同的包下需要this对象的时候,我们都会进行new的操作,这还只是一个对象,那么要创建多个对象呢?

是不是很麻烦。 Spring 为我们解决了这一个难题,我们不在需要自己创建对象,由Sprig的BeanFactory为我们创建。所以说由SpringIoC容器管理的对象是bean对象,反过来说bean是由SpringIoc容器实例化、配置、组装的对象,而BeanFactory是SpringIoC的对象生产工厂。

思路结构:SpringIoC容器 --> BeanFactory工厂 --> bean对象

如何配置容器?

容器是个好东西,可以装bean对象、装水、装妹子……,容器该怎么使用那?有三种方式:

1、基于xml配置元数据

2、基于注解的方式告诉spring这个类要加载到容器当中去

3、基于java代码配置

简单介绍基于xml文件配置元数据(元数据就是实例化bean对象,处理bean对象之间的依赖等等一系列有关bean的配置)

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
https://www.springframework.org/schema/beans/spring-beans.xsd"
>


<bean id="【bean的别名】" class="【bean的完全限定的类名】">
<property name="【bean中对象参数名称】" ref="【所依赖的bean的别名】"/>
</bean>

举个栗子

    <bean id="demo" class="com.taobao.Seller">
<property name="product" ref="productBean"/>
</bean>
<bean id="productBean" class="com.taobao.Product">
<property name="id" value="007"/>
</bean>
</beans>

实例化容器:

实例化容器一般在测试的时候用到:

知识小助手 --> 虽说SpringIoC容器靠BeanFactory工厂来创建对象的,但是更多的是org.springframework.context.ApplciationContext这个BeanFactory的子接口来做的,ApplicationContext是对BeanFactory的完善和补充。

容器的实例化是通过ApplicationContext来实例化的,其两个实现是ClassPathXmlApplicatonContext和FileSystemXmlApplication

举个栗子

//加载我们所配置的xml文件,默认从resource开始加载,可以加载多个xml文件哦
ApplicationContext context = new ClassPathXmlApplicationContext("services.xml");

// getBean这个方法一看就是获取具体的bean,参数(bean的别名,bean的class)
PetStoreService service = context.getBean("petStore", PetStoreService.class);

// 拿到对象调用对象的getUsernameList()方法
List<String> userList = service.getUsernameList();

--> 以上就是容器简单的实例化

bean的详细概述:

NjueyqA.jpg!web

Name: bean的命名

提倡小驼峰命名规则,可以使用id属性和name属性进行命名,在使用内部bean和开启自动装配时也可以不用命名,容器会为该bean生成唯一的名称。

bean的实例化方式:

1、Constructor arguments: 构造函数参数

在容器本身通过反向调用其构造函数直接创建bean的情况下指定要构造的bean类,需要bean的无参构造,稍微等同于使用new运算符的Java代码。

2、静态工厂方法实例化

定义使用静态工厂方法创建的bean时,请使用该class 属性指定包含static工厂方法的类,并使用factory-method名称的属性指定工厂方法本身的名称。您应该能够调用此方法(使用可选参数,如稍后所述)并返回一个活动对象,随后将其视为通过构造函数创建的对象。

举个栗子

//静态工厂类
public class ClientService {
private static ClientService clientService = new ClientService();
private ClientService() {}

public static ClientService createInstance() {
return clientService;
}
}

<bean id="clientService"
class="examples.ClientService"
factory-method="createInstance"/>

3、实例工厂方法实例化

与通过静态工厂方法实例化类似,使用实例工厂方法进行实例化会从容器调用现有bean的非静态方法来创建新bean。

举个栗子

//实例工厂类
public class DefaultServiceLocator {

private static ClientService clientService = new ClientServiceImpl();

public ClientService createClientServiceInstance() {
return clientService;
}
}

<!--现有的bean-->
<bean id="serviceLocator" class="examples.DefaultServiceLocator"/>

<!-- 通过现有的bean的方法返回clientService对象 -->
<bean id="clientService"
factory-bean="serviceLocator"
factory-method="createClientServiceInstance"/>

bean的依赖注入DI

依赖注入(DI)是一个过程,通过这个过程,对象只能通过构造函数参数,工厂方法的参数或在构造对象实例后在对象实例上设置的属性来定义它们的依赖关系(即,它们使用的其他对象)。

DI存在两个主要变体:基于有参构造注入和基于Setter注入。

//有参构造
public class SimpleMovieLister {

private MovieFinder movieFinder;

public SimpleMovieLister(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
<beans>
<bean id = "movieFinder" class = "a.b.MovieFinder"/>
//通过构造函数注入
<bean id="simpleMovieLister" class="a.b.SimpleMovieLister">
<constructor-arg ref="movieFinder"/>
</bean>
</beans>

//基于Setter注入
public class SimpleMovieLister {

private MovieFinder movieFinder;

public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}

}
<beans>
<bean id = "movieFinder" class = "a.b.MovieFinder"/>

<bean id="simpleMovieLister" class="a.b.SimpleMovieLister">
<property name="movieFinder" ref="movieFinder">
</property>
</bean>
</beans>

bean的自动装配

biuIfi7.jpg!web

当然也可以使用@Autowire按照类型装配和@Resource按照名称装配的注解

bean的范围

UFRB3yr.jpg!web

  • singleton:(默认)将单个bean定义范围限定为每个Spring IoC容器的单个对象实例。

  • protpeype: 将单个bean定义范围限定为任意数量的对象实例。

  • request: 将单个bean定义范围限定为单个HTTP请求的生命周期。也就是说,每个HTTP请求都有自己的bean实例,它是在单个bean定义的后面创建的。仅在具有Web感知功能的Spring环境中有效ApplicationContext。

  • session: 将单个bean定义范围限定为HTTP的生命周期Session。仅在具有Web感知功能的Spring环境中有效ApplicationContext。

  • application: 将单个bean定义范围限定为a的生命周期ServletContext。仅在具有Web感知功能的Spring环境中有效ApplicationContext。

  • websocket: 将单个bean定义范围限定为a的生命周期WebSocket。仅在具有Web感知功能的Spring环境中有效ApplicationContext

(点击可跳转)

1.   Web网站架构演变历程

2. Java 编程军规

4.  有助于改善性能的Java代码技巧

看到这里了,就关注一个呗

QZju6ry.jpg!web


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK