当前位置: 移动技术网 > IT编程>开发语言>Java > Spring核心(IoC) 入门解读

Spring核心(IoC) 入门解读

2019年06月25日  | 移动技术网IT编程  | 我要评论

什么是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>

 

实例化容器:

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

知识小助手 --> 虽说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的详细概述:

spring核心(ioc) 入门解读

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的自动装配

spring核心(ioc) 入门解读

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

bean的范围

spring核心(ioc) 入门解读

  • 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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网