注意:xml配置中bean节点下scope属性默认值为singleton(单例),在需要多例的情况下需要配置成prototype

spring提供三种实例化方式:默认构造、静态工厂、实例工厂

一、默认(无参)构造:就是经常使用的方式,xml-><bean id="" class=""></bean>

二、静态工厂:工厂工具类,提供的方法都是static静态的

1、沿用上一个工程,基本结构如下:

2、新建CategoryService类

package hjp.spring.staticinstance;

public class CategoryService {
public void addCategory() {
System.out.println("add category");
}
}

3、新建工厂类MyFactory

package hjp.spring.staticinstance;

public class MyFactory {
public static CategoryService createService() {
return new CategoryService();
}
}

4、新建beans.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.xsd">
<!-- 将工厂交予spring,自定义工厂创建对象由spring管理
class 指定工厂实现类
factory-method 确定静态方法
-->
<bean id="categoryServiceId" class="hjp.spring.staticinstance.MyFactory" factory-method="createService"></bean>
</beans>

5、新建测试类

package hjp.spring.staticinstance;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestApp {
@Test
public void demo1() {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("hjp/spring/staticinstance/beans.xml");
CategoryService categoryService=applicationContext.getBean("categoryServiceId",CategoryService.class);
categoryService.addCategory();
}
}

三、实例工厂:

1、沿用上一个工程,基本结构如下:

2、新建OrderService类

package hjp.spring.commeninstance;

public class OrderService {
public void addOrder() {
System.out.println("add Order");
}
}

3、新建工厂类MyFactory

package hjp.spring.commeninstance;

public class MyFactory {
public OrderService createService() {
return new OrderService();
}
}

4、新建beans.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.xsd">
<!-- 1、创建工厂 -->
<bean id="myFactoryId" class="hjp.spring.commeninstance.MyFactory"></bean>
<!-- 2、实例工厂创建对象,交予spring管理
factory-bean 确定实例工厂
factory-method 确定方法
-->
<bean id="orderServiceId" factory-bean="myFactoryId" factory-method="createService"></bean>
</beans>

5、新建测试类

package hjp.spring.commeninstance;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestApp {
@Test
public void demo1() {
//获得容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("hjp/spring/commeninstance/beans.xml");
//获得对象
OrderService orderService = applicationContext.getBean("orderServiceId", OrderService.class);
orderService.addOrder();
}
}

spring bean实例化方式的更多相关文章

  1. Spring学习笔记之 Spring IOC容器(二) 之注入参数值,自动组件扫描方式,控制Bean实例化方式,使用注解方式

     本节主要内容:    1. 给MessageBean注入参数值    2. 测试Spring自动组件扫描方式    3. 如何控制ExampleBean实例化方式    4. 使用注解方式重构Jdb ...

  2. Spring bean注入方式

    版权声明:本文为博主原创文章,如需转载请标注转载地址. 博客地址:http://www.cnblogs.com/caoyc/p/5619525.html  Spring bean提供了3中注入方式:属 ...

  3. Spring bean实例化的方式

    实例化过程如图,方式如图. 甩代码. 方式一:构造方法 搞一个bean,修改一下xml配置 package com.itheima.instance.constructor; public class ...

  4. spring bean实例化的三种方式

    一.使用类的无参构造创建 配置文件 java代码 注意若类里面没有无参的构造,则会出现异常 二.使用静态工厂创建 配置文件 java代码 Factory类 测试类 结果 三.使用实例工厂 配置文件 1 ...

  5. Spring Bean装配方式

    Spring装配机制 在xml中进行显示配置 在Java中进行显示配置 隐式bean发现机制和自动装配 自动化装配bean 组件扫描(component scanning),Spring会自动发现应用 ...

  6. Bean实例化方式

    https://blog.csdn.net/diaosinixiheixiu/article/details/78919395 https://www.cnblogs.com/deng-cc/p/89 ...

  7. Spring Bean 生命周期之destroy——终极信仰

    上一篇文章 Spring Bean 生命周期之我从哪里来 说明了我是谁? 和 我从哪里来? 的两大哲学问题,今天我们要讨论一下终极哲学我要到哪里去? 初始化 Spring Bean 有三种方式: @P ...

  8. spring三种实例化bean的方式

    1构造函数实例化 2静态工厂方法实例化 3实例工厂方法实例化 service接口: package service; public interface PersonService { public v ...

  9. Spring学习笔记之 Spring IOC容器(一)之 实例化容器,创建JavaBean对象,控制Bean实例化,setter方式注入,依赖属性的注入,自动装配功能实现自动属性注入

    本节主要内容:       1.实例化Spring容器示例    2.利用Spring容器创建JavaBean对象    3.如何控制Bean实例化    4.利用Spring实现bean属性sett ...

随机推荐

  1. css3 线性渐变和径向渐变

    线性渐变:ie6以下不兼容 径向渐变:只支持firefox.Chrome和Safari <!DOCTYPE html> <html> <head> <meta ...

  2. 《Linux及安全》实验安排

    SEED(SEcurity EDucation)项目由雪城大学杜文亮教授2002年创立,为计算机教学提供一套信息安全实验环境,目前已开发超过30个实验,涵盖广泛的安全原理,被全世界数百个高校采用. 实 ...

  3. c# 模拟get和post

    private string HttpPost(string Url, string postDataStr)         {             HttpWebRequest request ...

  4. C#Json序列化和反序列化

    1.动态决定数据是否要序列化 我的需求是这样的,我用了一款数据库的组件叫Dos.ORM,确实方便了不少,但是在用的时候,我发现一个问题,比如我定义的表中有一个字段添加时间,修改时间,这些都是默认的,在 ...

  5. web压测工具http_load原理分析

    一.前言 http_load是一款测试web服务器性能的开源工具,从下面的网址可以下载到最新版本的http_load: http://www.acme.com/software/http_load/ ...

  6. 那些不好的Socket服务器设计

    基础Socket 自强的程序猿们都喜欢搞Socket,而且觉得最好自己来封装个组件出来,如果再往上,加入某种数据协议,让上层服务器开发照着此协议走,就是一个小小的框架了.于是,从头开始,最开始的服务器 ...

  7. 用Wireshark抓包分析超过70秒的请求

    超过70秒的请求是通过分析IIS日志发现的: 10.159.63.104是SLB的内网IP. 通过Wireshark抓包分析请求是9:22:21收到的(tcp.stream eq 23080): 09 ...

  8. 04.C#类型系统、值类型和引用类型(二章2.2-2.3)

    今天要写的东西都是书中一些概念性的东西,就当抄笔记,以提问对话的方式将其写出来吧,说不定以后面试能有点谈资~~~ Q1.C#1系统类型包含哪三点特性? A1.C#1类型系统是静态的.显式的和安全的. ...

  9. [工具类]文件或文件夹xx已存在,则重命名为xx(n)

    写在前面 最近在弄一个文件传输的一个东东,在接收文件的时候,如果文件已经存在,该如何处理?提示?删除?感觉直接删除实在不太合适,万一这个文件对用户来说很重要,你给他删除了肯定不行.然后就想到了,win ...

  10. 第十四课:js操作节点的插入,复制,移除

    节点插入 appendChild方法,insertBefore方法是常用的两个节点插入方法,具体实现,请看js高级程序设计,或者自行百度. 这里提一下面试时经常会问到的问题,插入多个节点时,你是怎么插 ...