[Spring实战系列] - No.2 Bean基本知识

2024-04-12 14:48

本文主要是介绍[Spring实战系列] - No.2 Bean基本知识,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

    在这一篇文章中,我将会简单地介绍有关Bean的基础知识,这也是Spring实战这本书的第二章节的主要内容。我所使用的代码也是来自于Spring 实战这本书。

  主要内容:

        1.什么是Bean?我们如何使用Bean?

        2.Setter注入和构造注入

        3.Bean的一些属性:内部Bean

        4.Bean的装配和范围化

    1. 什么是Bean?我们如何使用Bean?

        在第一篇文章中,我们曾经提到过Bean。什么是Bean?Bean是一种对象,我们使用Spring容器来控制其生命周期,进行对象的创建和分发。(说白了就是你定义的或者相关的类)Spring提供了多种容器,并分为两种:BeanFactory 和 应用上下文。如果你有兴趣了解具体上述两种容器架构的区别,可以到该博客仔细研究一下,这里不做赘述。

        那么我们如何使用Bean呢?有以下方法:

 BeanFactory factory = new XmlBeanFactory(new FileSystemResource("C:/spring.xml"));MyBean myBean = (MyBean) factory.getBean("nihao");

如果你在intellij中输入上述代码,你可能会得到代码XmlBeanFactory过时的提示。那我们应该怎么办呢?

ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");OneManBand oneManBand = (OneManBand) context.getBean("onemanband");
同时,ApplicationContext提供三种经常使用的实现方式;

1. ClassPathXmlApplicationContext : 从类路径中XML文件载入上下文定义信息。

2. FileSystemXmlApplicationContext : 从文件系统中XML文件载入上下文定义信息,所以需要完整路径。

3. XmlWebApplicationContext:从Web系统中Xml载入上下文定义信息。

2.Setter注入和构造注入

    Setter注入,顾名思义就是使用Bean中的属性设置器来注入对象中的属性,那么构造注入也就是利用含参构造函数来注入属性。Setter注入的行为可以理解为我们使用无参构造函数来构造对象,然后调用属性设置器注入对象。同理,构造注入的行为可以理解为,我们利用含参构造函数直接构造对象。下面我们来看一个例子:

    我们有一个类Juggler:

/*** Created by YanMing on 2017/2/20.*/import org.junit.Test;
public class Juggler implements Performer {private int beanBags;public Juggler(){}public void setBeanBags(int beanBags){this.beanBags = beanBags;}public Juggler(int beanBags){this.beanBags = beanBags;}public void perform() {System.out.println("my beanBags is " + this.beanBags);}
}
    我们分别在spring.xml中写入两种注入方式:

    1.Setter注入:

<bean id="duke" class="Juggler"><property name="beanBags" value="5"></property></bean>
    2.构造注入:

    <bean id="duke" class="Juggler"><constructor-arg value="15"></constructor-arg></bean>
    在intellij中编写测试示例:

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** Created by YanMing on 2017/2/20.*/public class JugglerTest {@Testpublic void setBeanBags() throws Exception {}@Testpublic void perform() throws Exception {ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");Juggler juggler = (Juggler) context.getBean("duke");juggler.perform();}}
    注:我在这里使用了junit4的jar包,有需要的朋友可以去百度上下载然后导入项目。为某个Bean编写测试可以点击类名,然后按住alt+enter,点击创建测试实例,然后就可以像上面一样测试啦。

    可以为Bean注入对象属性么?答案自然是:Yes!在这里我们添加一个Poem接口,让我们的Juggler可以吟诗!

/*** Created by YanMing on 2017/2/20.*/
public interface Poem {void recite();
}/*** Created by YanMing on 2017/2/20.*/
public class Sonnet29 implements Poem{private static String [] Lines ={"When in disgrace with fortune and men's eyes","I all alone beweep my outcast state","And trouble deaf heaven with my bootless cries","And look upon myself, and curse my fate..."};public Sonnet29(){}public void recite(){for(int i = 0 ; i < Lines.length;i++){System.out.println(Lines[i]);}}
}

/*** Created by YanMing on 2017/2/20.*/
public class PoeticJuggler extends Juggler {private Poem poem;public PoeticJuggler(Poem poem){super();this.poem = poem;}public PoeticJuggler(int beanBags,Poem poem){super(beanBags);this.poem = poem;}public void perform(){super.perform();System.out.print("While reciting");poem.recite();}}

<bean id="sonnet29" class="Sonnet29"></bean><bean id="duke" class="PoeticJuggler"><constructor-arg value="15"></constructor-arg><constructor-arg ref="sonnet29"></constructor-arg>
</bean>

public class JugglerTest {@Testpublic void setBeanBags() throws Exception {}@Testpublic void perform() throws Exception {ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");Juggler juggler = (PoeticJuggler) context.getBean("duke");juggler.perform();}}



    3.Bean的一些属性:内部Bean

    内部Bean看上去是一个非常高大上的东西,其实非常简单,只需要在我们的spring.xml中这样声明:

 <bean id="sonnet29" class="Sonnet29"></bean><bean id="duke" class="PoeticJuggler"><constructor-arg value="15"></constructor-arg><constructor-arg><bean class="Sonnet29"></bean></constructor-arg>
</bean>
    如果我们的属性不是单一属性,而是某些集合怎么办呢?在这里我们就用到了装配集合。我们来看一个例子

/*** Created by YanMing on 2017/2/20.*/
public interface Instruments {void play();
}/*** Created by YanMing on 2017/2/20.*/
import java.util.Collection;
public class OneManBand implements Performer {private Collection<Instruments> instruments;public OneManBand(){}public void perform(){for(Instruments ins:instruments){ins.play();}}public void setInstruments(Collection<Instruments> instruments){this.instruments = instruments;}
}
/*** Created by YanMing on 2017/2/20.*/
public class Piano implements Instruments {public void play(){System.out.println("Piano playing");}
}public class Guitar implements Instruments {public void play(){System.out.println("Guitar playing");}
}public class Saxophone implements Instruments {public void play(){System.out.println("Saxophone playing");}
}
/*** Created by YanMing on 2017/2/20.*/
public class OneManBandTest {@Testpublic void perform() throws Exception {ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");OneManBand oneManBand = (OneManBand) context.getBean("onemanband");oneManBand.perform();}@Testpublic void setInstruments() throws Exception {}}
<bean id="guitar" class="Guitar" scope="singleton"></bean><bean id="piano" class="Piano" scope="singleton"></bean><bean id="saxophone" class="Saxophone" scope="singleton"></bean><bean id="onemanband" class="OneManBand"><property name="instruments"><set><ref bean="guitar"></ref><ref bean="piano"></ref><ref bean="saxophone"></ref></set></property></bean>
        运行结果:


       同时,set还可以根据需要换成list,map等。相关的用法在网上也随处可见,这里不再赘述。

     那么,如果一个属性是空值该怎么办呢?我们使用<null/>元素

<property name="somenull"><null/></property>

    4.Bean的装配和范围化:

    在前面,我们已经学会了如何使用<constructor-arg>或<property>来显式地装配Bean。但是在大型的应用中,显式地装配会产生大量的xml。所以我们在这里引入自动装配。


    四种自动装配类型:

    1. byName 试图在容器中寻找和需要自动装配的属性名相同的Bean 

    2. byType 试图在容器中寻找一个与最需要自动装配的属性类型相同的Bean

    3. constructor 试图在容器中寻找与需要自动装配的Bean的构造函数参数一致的一个或多个Bean

    4. autodetect 按照 constructor 和 byType的顺序装配

    使用方式:

 <bean id="sonnet29" class="Sonnet29" autowire="byName"></bean>
    接下来,我们就来着重讲一下Bean的范围化:在默认的时候,我们每次请求一个Bean,都会准确的得到一个 Bean实例,并且这个实例是唯一的。那么我们可以每次获得不同实例么?可以Bean中有五种范围化规则:

singleton,prototype,request,session和global-session。后三种均和HTTP有关,在这里我们不做解释,我们只解释前两种。

    如何才能分辨我们的Bean实例是否为同一个呢?最好的方法是使用对象的hashcode。我们重新创建一个Bean,注册到配置文件中,然后在say()函数中输出this.hashcode(),查看结果

/*** Created by YanMing on 2017/2/21.*/
public class BeanScope {public void say(){System.out.println("my hashcode is " +  this.hashCode());}
}import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import static org.junit.Assert.*;/*** Created by YanMing on 2017/2/21.*/
public class BeanScopeTest {@Testpublic void say() throws Exception {ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");BeanScope beanScope1 = (BeanScope) context.getBean("beanscope");BeanScope beanScope2 = (BeanScope) context.getBean("beanscope");beanScope1.say();beanScope2.say();}}

<bean id="beanscope" class="BeanScope" scope="prototype"></bean>

        运行结果:

            1.Singleton

  2.Prototype

        

Github源码下载点击这里





这篇关于[Spring实战系列] - No.2 Bean基本知识的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/897438

相关文章

springboot集成easypoi导出word换行处理过程

《springboot集成easypoi导出word换行处理过程》SpringBoot集成Easypoi导出Word时,换行符n失效显示为空格,解决方法包括生成段落或替换模板中n为回车,同时需确... 目录项目场景问题描述解决方案第一种:生成段落的方式第二种:替换模板的情况,换行符替换成回车总结项目场景s

SpringBoot集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

SpringBoot中@Value注入静态变量方式

《SpringBoot中@Value注入静态变量方式》SpringBoot中静态变量无法直接用@Value注入,需通过setter方法,@Value(${})从属性文件获取值,@Value(#{})用... 目录项目场景解决方案注解说明1、@Value("${}")使用示例2、@Value("#{}"php

SpringBoot分段处理List集合多线程批量插入数据方式

《SpringBoot分段处理List集合多线程批量插入数据方式》文章介绍如何处理大数据量List批量插入数据库的优化方案:通过拆分List并分配独立线程处理,结合Spring线程池与异步方法提升效率... 目录项目场景解决方案1.实体类2.Mapper3.spring容器注入线程池bejsan对象4.创建

线上Java OOM问题定位与解决方案超详细解析

《线上JavaOOM问题定位与解决方案超详细解析》OOM是JVM抛出的错误,表示内存分配失败,:本文主要介绍线上JavaOOM问题定位与解决方案的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录一、OOM问题核心认知1.1 OOM定义与技术定位1.2 OOM常见类型及技术特征二、OOM问题定位工具

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建

MyBatis分页查询实战案例完整流程

《MyBatis分页查询实战案例完整流程》MyBatis是一个强大的Java持久层框架,支持自定义SQL和高级映射,本案例以员工工资信息管理为例,详细讲解如何在IDEA中使用MyBatis结合Page... 目录1. MyBATis框架简介2. 分页查询原理与应用场景2.1 分页查询的基本原理2.1.1 分

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

springboot中使用okhttp3的小结

《springboot中使用okhttp3的小结》OkHttp3是一个JavaHTTP客户端,可以处理各种请求类型,比如GET、POST、PUT等,并且支持高效的HTTP连接池、请求和响应缓存、以及异... 在 Spring Boot 项目中使用 OkHttp3 进行 HTTP 请求是一个高效且流行的方式。