spring创建Bean对象的控制

1、spring创建Bean对象的控制

a、控制对象创建方式(使用范围),在<bean>元素中使用scope属性控制,scope可以支持singleton或prototype,默认值是singleton

<bean scope= "singleton"> 该组件在spring容器里只有一个bean对象。每次取出的bean都是同一个bean,相当于单例模式

<bean scope = "prototype">该组件每次使用getBean("")都会返回一个新的对象

例如我们自定义了一个ExampleBean类:

public class ExampleBean {
   public void execute() {
       System.out.println("调用了execute方法");
   }
}

在applicationContext.xml中进行配置,默认使用scope="singleton"

<!-- 创建一个ExampleBean对象 -->
    <bean id ="example" class="com.zlc.test.ExampleBean"></bean>

我们测试一下每次取出的bean是否为同一个bean:

public static void main(String[] args) {
        String conf = "applicationContext.xml";
        //创建容器对象
        ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
        //得到Example对象
        ExampleBean e1 = ac.getBean("example",ExampleBean.class);
        ExampleBean e2 = ac.getBean("example",ExampleBean.class);
        System.out.println(e1 == e2);
    }

运行结果:

由此可见:每次我们从容器中取出的对象都是同一个对象

原文地址:https://www.cnblogs.com/zlingchao/p/9402310.html

时间: 2024-08-01 20:00:52

spring创建Bean对象的控制的相关文章

spring中创建bean对象的三种方式以及作用范围

时间:2020/02/02 一.在spring的xml配置文件中创建bean对象的三种方式: 1.使用默认构造函数创建.在spring的配置文件中使用bean标签,配以id和class属性之后,且没有其他属性和标签时采用的就是默认构造函数创建bean对象,此时如果类中没有默认构造函数函数,则对象无法创建. <bean id="one" class="sdnu.machi.one"></bean> 如果one.class中没有默认构造函数则会报

Spring创建复杂对象

Spring创建复杂对象 何为复杂对象? 在java中,不能通过new关键字创建的对象,都称为复杂对象,如抽象类(abstract,例如Calendar日期类).接口(interface,JDBC中的Connection连接类). 复杂对象(如Calendar日期类)的普通创建方式: Calendar calendar = Calendar.getInstance(); System.out.println(newSimpleDateFormat("yyyy-MM-dd HH:mm:ss&quo

Spring创建Bean的顺序

一直对Spring创建bean的顺序很好奇,现在总算有时间写个代码测试一下.不想看过程的小伙伴可以直接看结论 目录结构: 其中:bean4.bean5包下的class没有注解@Component,测试过程中,这两个包的class会直接通过<bean class="XXXXX"/>的方式创建.bean1.bean2.bean3包下的class注解了@Component,以便component-scan扫描.另外,bean创建之间没有依赖关系,例如bean1的创建不依赖于其他b

Spring中bean对象的生命周期

Spring提供了一些接口来提供一些方法,体现了bean对象在Spring容器中的生命周期 具体的过程可以体现为: 读取权限类名->构建一个类对象->用这个类对象通过无参构造器newInstance()构建对象 ↓ 调用set方法注入依赖 ↓ 如果这个Bean已经实现了BeanNameAware接口 调用它实现的setBeanName(String name)方法 此处传递的就是Spring配置文件中Bean的name值 ↓ 如果这个Bean已经实现了BeanFactoryAware接口 容器

Spring 创建bean的时机

默认在启动spring容器的时候,spring容器配置文件中的类就已经创建完成对象了        在<bean>中添加属性lazy-init,默认值为false.    true  在context.getBean的时候才要创建对象                      *  优点                                    如果该bean中有大数据存在,则什么时候context.getBean,什么时候创建对象                         

Spring 创建Bean的6种方式

前言 本文讲解了在Spring 应用中创建Bean的多种方式,包括自动创建,以及手动创建注入方式,实际开发中可以根据业务场景选择合适的方案. 方式1: 使用Spring XML方式配置,该方式用于在纯Spring 应用中,适用于简单的小应用,当应用变得复杂,将会导致Bean管理很麻烦 <bean id="xxxx" class="xxxx.xxxx"/> 方式2: 使用@Component,@Service,@Controler,@Repository注

获取Spring容器Bean对象工具类

在开发中,总是能碰到用注解注入不了Spring容器里面bean对象的问题.为了解决这个问题,我们需要一个工具类来直接获取Spring容器中的bean.因此就写了这个工具类,在此记录一下,方便后续查阅.废话不多说,直接上代码. 一.代码 package com.zxy.demo.spring; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext;

spring创建bean的三种方式

1.使用构造器创建bean 1.1.使用无参构造器创建 package com.ly.spring; public class Person { private String name; public void say(String name) { System.out.println("你好,我叫"+name); } } <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&

获取Spring管理bean对象

package com.sinosoft.base.util; import org.springframework.beans.BeansException; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.context.ApplicationContext; import org.springframework.context.Applica