概述

bean说白了就是一个普通的java类的实例,我们在bean中写一些我们的业务逻辑,这些实例由Sping IoC容器管理着。在web工程中的spring配置文件中,我们用<bean/>标签来配置一个bean。

Bean ID

没个bean都有至少一个ID,而且区别于其他bean的ID。在配置文件中,我们可以用 id 或者 name 来指定bean的ID。id属性只能设置一个值,如果项目中需要给bean指定多个ID,可以在name属性中设置多个,表示这个bean的别名。如果 id 和 name 属性都没有指定,Spring会在初始化bean的时候自动给bean赋一个唯一的ID(格式为:类的全路径@数字串)。但如果你的bean需要引用另外一个bean,那么被引用的那个bean就必须设置id或者name属性。当然我们还可以以内置bean的方式配置而不需要给内部bean指定ID。

除了在bean配置内部指定bean的ID,还可以用<alias/>标签来指定bean的别名:
<span style="font-size:14px;"><alias name="oldName" alias="newName1"/>
<alias name="oldName" alias="newName2"/></span>

Bean实例化的方式

Spring提供多种配置方式以实现不同方式来实例化一个bean

1. 通过默认构造函数
配置方式如下:
<span style="font-size:14px;"><bean id="exampleBean" class="examples.ExampleBean"/>

<bean name="anotherExample" class="examples.ExampleBeanTwo"/></span>
 
注意class中必须是一个类名,不能是接口。因为Spring是通过Class.newInstance方法来实例化的。

2. 通过类内部静态工厂方法
配置方式如下:
<span style="font-size:14px;"><bean id="clientService"
class="examples.ClientService"
factory-method="createInstance"/>
</span>
<span style="font-size:14px;">public class ClientService {
private static ClientService clientService = new ClientService();
private ClientService() {} public static ClientService createInstance() {
return clientService;
}
}</span>
这种方式在Spring是通过调用bean中factory-method中指定的静态方法来实例化这个bean。

3. 通过实例工厂类方法
配置方式如下:
<span style="font-size:14px;"><bean id="serviceLocator" class="examples.DefaultServiceLocator">
<!-- inject any dependencies required by this locator bean --></bean> <bean id="clientService"
factory-bean="serviceLocator"
factory-method="createClientServiceInstance"/> <bean id="accountService"
factory-bean="serviceLocator"
factory-method="createAccountServiceInstance"/></span>
 
<span style="font-size:14px;">public class DefaultServiceLocator {
private static ClientService clientService = new ClientServiceImpl();
private static AccountService accountService = new AccountServiceImpl(); private DefaultServiceLocator() {} public ClientService createClientServiceInstance() {
return clientService;
} public AccountService createAccountServiceInstance() {
return accountService;
}
}
</span>

这种方式Spring先加载实例工厂类DefaultServiceLocator,然后后面需要从这个类获取指定类实例的,只需要通过factory-bean和factory-method配置指定工厂类和调用方法即可。

Spring - Bean的概念及其基础配置的更多相关文章

  1. [spring]Bean注入——在XML中配置

    Bean注入的方式有两种: 一.在XML中配置 属性注入 构造函数注入 工厂方法注入 二.使用注解的方式注入@Autowired,@Resource,@Required 本文首先讲解在XML中配置的注 ...

  2. Spring系列2:Spring容器基本概念和使用

    本文内容 简单回顾IoC和DI概念 Spring容器的概念 的xml配置和初始化 容器的基本使用 bean的定义和初始化配置 简单理解IoC和DI概念 什么是IoC控制反转? 通俗地但不严谨地讲,以前 ...

  3. Java进阶知识15 Spring的基础配置详解

    1.SSH各个的职责 Struts2:是web框架(管理jsp.action.actionform等).Hibernate:是ORM框架,处于持久层.Spring:是一个容器框架,用于配置bean,并 ...

  4. 使用 Java 配置进行 Spring bean 管理--转

    概述 众所周知,Spring 框架是控制反转 (IOC) 或依赖性注入 (DI) 模式的推动因素,而这种推动是通过基于容器的配置实现的.过去,Spring 允许开发人员使用基于 XML 的配置,通过利 ...

  5. Spring基础配置

    从毕业到现在我一直从事Android开发,但是对JavaEE一直念念不忘,毕业校招的时候,一个礼拜拿了三个offer,岗位分别是Android.JavaEE和JavaSE,后来觉得Android比较简 ...

  6. SpringCloud系列九:SpringCloudConfig 基础配置(SpringCloudConfig 的基本概念、配置 SpringCloudConfig 服务端、抓取配置文件信息、客户端使用 SpringCloudConfig 进行配置、单仓库目录匹配、应用仓库自动选择、仓库匹配模式)

    1.概念:SpringCloudConfig 基础配置 2.具体内容 通过名词就可以发现,SpringCloudConfig 核心作用一定就在于进行配置文件的管理上.也就是说为了更好的进行所有微服务的 ...

  7. Spring Boot 基础配置

    之前简单接触了一些Spring Boot ,并且写了一个简单的 Demo .本文就来简单学习一下 Spring Boot 的基础配置. 一.Spring Boot 项目入口 上文中有写到,Spring ...

  8. Spring学习(九)-----Spring bean配置继承

    在 Spring,继承是用为支持bean设置一个 bean 来分享共同的值,属性或配置. 一个子 bean 或继承的bean可以继承其父 bean 的配置,属性和一些属性.另外,子 Bean 允许覆盖 ...

  9. Spring bean配置继承

    在 Spring,继承是用为支持bean设置一个 bean 来分享共同的值,属性或配置. 一个子 bean 或继承的bean可以继承其父 bean 的配置,属性和一些属性.另外,子 Bean 允许覆盖 ...

随机推荐

  1. Java中几种常量池的区分

    转载自:https://tangxman.github.io/2015/07/27/the-difference-of-java-string-pool/ 在java的内存分配中,经常听到很多关于常量 ...

  2. Big Data Solution in Azure: Azure Data Lake

    https://blogs.technet.microsoft.com/dataplatforminsider/2015/09/28/microsoft-expands-azure-data-lake ...

  3. 使用TenforFlow 搭建BP神经网络拟合二次函数

    使用简单BP神经网络拟合二次函数 当拥有两层神经元时候,拟合程度明显比一层好 并出现如下警告: C:\Program Files\Python36\lib\site-packages\matplotl ...

  4. LaTeX的图片插入及排版

    LaTeX中一般只直接支持插入eps(Encapsulated PostScript)格式的图形文件, 因此在图片插入latex文档之前应先设法得到图片的eps格式的文件. UNIX下的各种应用软件都 ...

  5. 中篇: php 微信支付 基于Thinkphp3.2开发

    ⑤ 微信支付接口的使用 a.微信公众平台文档:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115 b.微信支付开 ...

  6. STL中的二分查找———lower_bound,upper_bound,binary_search

    关于STL中的排序和检索,排序一般用sort函数即可,今天来整理一下检索中常用的函数——lower_bound , upper_bound 和 binary_search . STL中关于二分查找的函 ...

  7. 求最长公共前缀和后缀—基于KMP的next数组

    KMP算法最主要的就是计算next[]算法,但是我们知道next[]求的是当前字符串之前的子字符串的最大前后缀数,但是有的时候我们需要比较字符串中前后缀最大数,比如 LeetCode的shortest ...

  8. 高新技术---枚举及部分JDK1.5新特性

    第一讲     枚举 一.概述 这里说的枚举,不是集合vector的特有枚举迭代器,而是JDK1.5的一个新特性.之所以单独拿它开刷,是这个知识点比较重要,同时相对来说比较难理解一些. 为什么要有枚举 ...

  9. 递归的二叉查找树Java实现

    package practice; public class TestMain { public static void main(String[] args) { int[] ao = {50,18 ...

  10. 融会贯通——最常用的“合成复用原则”技能点Get

    复用一个类的时候,多使用对象的组合/聚合的关联关系,而不是继承. 之前提到的"依赖倒转原则",是以里氏代换原则为基础的实现开闭原则目标的手段,这一条路线涉及到的是类的继承(包括单继 ...