Spring学习(二)------Bean的实例化与作用域

Bean的三种实例化

在面向对象的程序中,想要使用某个对象,就需要先实例化这个对象。同样,在Spring中,要想使用容器中Bean,也需要实例化Bean。实例化Bean有三种方式,分别是构造器实例化,静态工厂实例化,和实例工厂实例化,其中最常用的就是构造器实例化。

XML元素的配置

XML配置文件的根元素是,此中包含多个子。每一个子元素定义一个bean,并描述该Bean如何被装配到spring容器中。关于Bean元素的常用属性如下。
id。bean标识符
class。指定Bean的具体实现类,它必须是一个完整的类名,使用类的全限定名。
scope。用来设定bean的实例域,常用的有singleton(单例,此时容器只实例化一个bean,所有的对象引用的都是该bean)。prototype(原型,容器会在需要bena的时候实例化一个新的bean,后面会具体讲到)。

创建第一个Spring程序

前提:电脑配置好了idea和java环境,以及将maven配置到idea上了。
新建一个maven项目:点击file–new–project。选择maven,然后勾选上Create from archetype。选择以webapp结尾的选项,next。
在这里插入图片描述
接下来就是输入项目名称等内容。创建完毕后等待,maven自动导入依赖完成。

导入依赖

我们开发spring项目的到时候需要在pom文件导入我们所需要的依赖,本次项目所需要的依赖为:

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.3.6.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.6.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.3.6.RELEASE</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>4.3.6.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.2</version>
    </dependency>

项目结构为

在这里插入图片描述
如图,idea文件夹是idea自己创建的配置目录,无需理会。External Libraries下面存放了一些jar包,包括jdk跟我们自己导入的依赖包。Java目录跟resource才是我们需要的关注的目录,两个目录都是我们自己创建,java目录指定为Source Root(右击java目录,mark directory as,选择源文件目录),resources指定为资源文件目录。

构造器实例化

Bean1文件

package com.ss.instance.construcor;

public class Bean1 {
}

  • bean1.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-4.3.xsd">
    <!--将指定类配置给Spring,让Spring调用默认的构造方法来创建其对象的实例-->
    <bean id="beans1" class="com.ss.instance.construcor.Bean1" />

</beans>
  • InstanceTest1文件
package com.ss.instance.construcor;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Clay 8.8
 * bean测试------构造器实例化
 */
public class InstanceTest1 {
    public static void main(String args[]){
        //构造器实例化。
        //定义配置文件路径
        String xmlPath = "beans1.xml";
        //ApplicationContext在加载配置文件时,对bean进行实例化
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        Bean1 bean1 = (Bean1) applicationContext.getBean("beans1");
        System.out.println(bean1);
    }
}

静态工厂实例化

Bean2文件

package com.ss.instance.static_factory;

public class Bean2 {
}

工厂类文件

package com.ss.instance.static_factory;

public class MyBean2Factory {
    //使用自己的工厂创建Bean2实例
    public static Bean2 createBean(){
        return new Bean2();
    }

}

beans2.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-4.3.xsd">
    <!--将指定类配置给Spring,让Spring创建其对象的实例-->
    <!--指定工厂实现类-->
    <!--spring容器配置到哪个是需要实现工厂里面的工厂方法。-->
    <bean id="bean2"

          class="com.ss.instance.static_factory.MyBean2Factory"

    factory-method="createBean" />

</beans>

测试文件

package com.ss.instance.static_factory;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Clay 8.8
 * bean测试------静态工厂实例化
 */
public class InstanceTest2 {
    public static void main(String args[]){

        //定义配置文件路径
        String xmlPath = "beans2.xml";
        //ApplicationContext在加载配置文件时,对bean进行实例化
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        //Bean bean2 = (Bean2) applicationContext.getBean("beans2");
        System.out.println(applicationContext.getBean("bean2"));


    }

}

实例工厂实例化

bean文件

package com.ss.instance.factory;

public class Bean3 {
}

工厂文件

package com.ss.instance.factory;

public class MyBean3Factory {
    public MyBean3Factory(){
        System.out.println("bean3工厂实例化中...");
    }

    public Bean3 createBean(){
        return new Bean3();
    }
}

beans3.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-4.3.xsd">

    <!--实例工厂实例化-->
    <!--配置工厂-->
    <bean id="myBean3Factory"

          class="com.ss.instance.factory.MyBean3Factory"

           />

    <!--使用factory-bean属性指向配置的实例工厂-->
    <!--使用factory-method属性确定使用工厂中哪个方法-->
    <bean id="bean3" factory-bean="myBean3Factory"
    factory-method="createBean"/>

</beans>

Test

package com.ss.instance.factory;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Clay 8.8
 * bean测试------实例工厂实例化
 */
public class InstanceTest3 {
    public static void main(String args[]){

        //定义配置文件路径
        String xmlPath = "beans3.xml";
        //ApplicationContext在加载配置文件时,对bean进行实例化
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);

        System.out.println(applicationContext.getBean("bean3"));


    }
}

测试截图
在这里插入图片描述

Bean的作用域

在Spring容器创建一个Bean实例时,不仅可以完成Bean的实例化,还可以通过scope属性指定待定的作用域。bean的作用域有七种,分别是singleton单例,prototype原型,request,session,globalSession。application,websocket。常用有两种,singleton和prototype。
当不知道scope属性时候,作用域默认为单例。
singleton作用域:Spring容器只会存在一个共享的Bean实例。singleton作用域对于无会话状态的bean(dao,service组件)来说,是最理想的选择。
prototype作用域,Spring容器会为每个bean的请求都创建一个新的实例。
单例运行截图
在这里插入图片描述
原型运行实例
在这里插入图片描述
主要代码

<!--<bean id="scope" class="com.ss.scope.Scope" scope="singleton"/>-->
<bean id="scope" class="com.ss.scope.Scope" scope="prototype"/>

//定义配置文件路径
String xmlPath = “beans4.xml”;
//ApplicationContext在加载配置文件时,对bean进行实例化
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);

    //spring容器默认为单例。通过执行结果可以看出来。此时spring容器只创建了一个实例。
    //当作用域为原型的时候,会创建不同的实例。
    System.out.println(applicationContext.getBean("scope"));
    System.out.println(applicationContext.getBean("scope"));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值