[04] Bean的实例化和多个配置文件
1、实例化方式
1.1 构造器方式
public class Coder {
private String name;
private int age;
public Coder() {
System.out.println("This is the constructor with none-parameter");
}
public void print(){
System.out.println("Hello World");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class Coder {
private String name;
private int age;
public Coder() {
System.out.println("This is the constructor with none-parameter");
}
public void print(){
System.out.println("Hello World");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

- 增加构造函数
- 增加xml中对应bean标签中的<constructor-arg>
public Coder(String name, int age) {
this.name = name;
this.age = age;
}
public Coder(String name, int age) {
this.name = name;
this.age = age;
}
<bean id="coder" class="dulk.learn.spring.Coder">
<constructor-arg index="0" value="Dulk"></constructor-arg>
<constructor-arg index="1" value="27"></constructor-arg>
</bean>
<bean id="coder" class="dulk.learn.spring.Coder">
<constructor-arg index="0" value="Dulk"></constructor-arg>
<constructor-arg index="1" value="27"></constructor-arg>
</bean>

1.2 静态工厂方式
public class StaticFactory {
public static Coder produceCoder() {
System.out.println("invoke the produceCode() of StaticFactory");
return new Coder();
}
}
public class StaticFactory {
public static Coder produceCoder() {
System.out.println("invoke the produceCode() of StaticFactory");
return new Coder();
}
}
<bean id="coder" class="dulk.learn.spring.StaticFactory" factory-method="produceCoder"></bean>
<bean id="coder" class="dulk.learn.spring.StaticFactory" factory-method="produceCoder"></bean>

1.3 普通工厂方式
public class CustomFactory {
public Coder produceCoder() {
System.out.println("invoke the produceCode() of CustomFactory");
return new Coder();
}
}
public class CustomFactory {
public Coder produceCoder() {
System.out.println("invoke the produceCode() of CustomFactory");
return new Coder();
}
}
<bean id="customFactory" class="dulk.learn.spring.CustomFactory"></bean>
<bean id="coder" factory-bean="customFactory" factory-method="produceCoder"></bean>
<bean id="customFactory" class="dulk.learn.spring.CustomFactory"></bean>
<bean id="coder" factory-bean="customFactory" factory-method="produceCoder"></bean>

2、配置文件
<import resource="{path}" />
<import resource="{path}" />

[04] Bean的实例化和多个配置文件的更多相关文章
- Spring中Bean的实例化
Spring中Bean的实例化 在介绍Bean的三种实例化的方式之前,我们首先需要介绍一下什么是Bean,以及Bean的配置方式. 如果 ...
- 【Spring】Spring bean的实例化
Spring实现HelloWord 前提: 1.已经在工程中定义了Spring配置文件beans.xml 2.写好了一个测试类HelloWorld,里面有方法getMessage()用于输出" ...
- 【spring源码】bean的实例化(转载)
首先来看一段代码,看过上一节的朋友肯定对这段代码并不陌生.这一段代码诠释了Spring加载bean的完整过程,包括读取配置文件,扫描包,加载类,实例化bean,注入bean属性依赖. 上一节介绍了Sp ...
- 【初识Spring】对象(Bean)实例化及属性注入(xml方式)
title: [初识Spring]对象(Bean)实例化及属性注入(xml方式) date: 2018-08-29 17:35:15 tags: [Java,Web,Spring] --- #初识S ...
- Spring中Bean的实例化与DI的过程
引言 前文我们介绍了关于如何学习Spring的源码以及解析了spring中加载配置文件注册Beandefinition的过程.今天我们继续学习DI的过程. 创建实例和DI过程 IOC和DI都是对spr ...
- 【spring源码系列】之【Bean的实例化】
人生需要探索的热情.坚持的勇气以及热爱生活热爱自己的力量. 1. Bean的实例化 上一篇讲述了bean的生命周期,其中第一步就涉及到了bean的实例化,本文重点分析bean实例化,先进入源码中的Ab ...
- Spring学习笔记之Bean的实例化
一.bean的实例化方法有3种, 1.构造器实例化 2.静态工厂方法实例化 3.实例工厂方法实例化 二.用构造器来实例化 <bean id="ShunDao" class=& ...
- Spring Ioc介绍和Bean的实例化
一.IoC:Inverse of Control 控制反转 // 依赖注入 Dependency Injection 控制:某一接口具体实现类的选择权 反转:从调用者中移除控制权,转交第三方 ...
- bean的实例化方式
spring中bean的实例化方式有三种,1.构造器实例化,2.实例工厂实例化,3.静态工厂实例化 1.构造器实例化方式 public class bean1 { public bean1() { } ...
随机推荐
- csharp: FTP Client Library using System.Net.FtpWebRequest
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- CoreCRM 开发实录 —— 基于 AntDesign 的新 UI
上一篇说到,因为有新朋友加入,对前端开发有了新的要求.原来基于 Bootstrap 的 UI 就不要了.在网上(其实是 GitHub 上)逛了几圈,最后使用了 antd-admin 这个框架做为基础模 ...
- Loadrunner 脚本开发-soap_request函数介绍及WebService接口测试
脚本开发- soap_request函数介绍及WebService接口测试 by:授客 QQ:1033553122 函数介绍 soap_request 函数执行一个SOAP请求 函数原型 int so ...
- Google Chrome 下载&绿化&增强
Chrome下载 Google Chrome 已经可以在线更新,虽然比较慢! 国内常用的更新地址有两处:chromedownloads 和 shuax(耍下): https://www.chromed ...
- spring ApplicationContext中Bean的生命周期
AbstractApplicationContext Spring的AbstractApplicationContext是ApplicationContext的抽象实现类,该抽象类的refresh方法 ...
- [20170927]hugepages与内核参数nr_overcommit_hugepages.txt
[20170927]hugepages与内核参数nr_overcommit_hugepages.txt /proc/sys/vm/nr_overcommit_hugepages specifies h ...
- tkinter中button按钮控件(三)
button控件 简单的实现: import tkinter wuya = tkinter.Tk() wuya.title("wuya") wuya.geometry(" ...
- C#语言————第三章 使用属性升级MyBank
********常见的访问修饰符*********: public :公共的,可以在其他类中访问 private:私有的,只有在本类里可以使用,其他的类无权访问 类的默认访问修饰符 internal( ...
- ssh无法访问服务器报“ssh-dss”认证错误
故障描述: 在windows下的ssh客户端直接报错,内容为: Unable to negotiate with legacyhost: no matching host key type found ...
- Django框架的简介
Django框架的背景 Django是一款基于Python开发的全栈式一体化Web 应用框架.2003 年问世之初,它只是 美国一家报社的内部工具,2005 年 7 月使用 BSD 许可证完成了开源. ...