注意: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. 【转】【C#】【Thread】Interlocked 轻量级锁

    为什么说它是轻量级呢?因为它仅对整形数据(即int类型,long也行)进行同步. 具体使用如下表: Interlocked.Increment(ref value) 数值加一(原子性操作) Inter ...

  2. C#操作JSON

    http://www.cnblogs.com/LiZhiW/p/3624729.html C#操作JSON 1. .NET对JSON的支持介绍............................. ...

  3. 2015某编程网易语言vip课堂全套教程 包含post,hook入门到精通等

    2015某编程网易语言vip课堂全套教程 包含post,hook入门到精通等  官方论坛弄来的  如果在官方下载需要权限的  挺不错教程 想学习易语言入门到精通 post hook  js改写的可以看 ...

  4. [CareerCup] 3.2 Min Stack 最小栈

    3.2 How would you design a stack which, in addition to push and pop, also has a function min which r ...

  5. LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...

  6. 20145215实验四 Android开发基础

    20145215实验四 Android开发基础 实验内容 基于Android Studio开发简单的Android应用并部署测试; 了解Android组件.布局管理器的使用: 掌握Android中事件 ...

  7. JavaScript事件详解

    1.事件传播机制:事件冒泡,事件捕获.      2.注册事件处理程序方式: 设置html标签属性为事件处理程序,文档元素的事件处理程序属性,名字由“on”后面跟着事件名组成,例如:onclick,o ...

  8. js方式清空表单数据的两种方式

    方法1:遍历页面元素 /* 清空FORM表单内容  id:表单ID*/  function ClearForm(id) {     var objId = document.getElementByI ...

  9. Dandelion - Distributed Computing on GPU Clusters

    linq on GPUs 非常期待中 看起来很cool,期望早点面世

  10. php并发请求

    一般在php进行请求url的时候,直接用 fopen 函数就可以搞定了,比如像这样: $file=fopen("http://www.cnblogs.com","r&qu ...