[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() { } ...
随机推荐
- JS之iscroll.js的使用详解
入门 Scroll是一个类,每个需要使用滚动功能的区域均要进行初始化.每个页面上的iScroll实例数目在设备的CPU和内存能承受的范围内是没有限制的. 尽可能保持DOM结构的简洁.iScroll使用 ...
- 2018-02-06 编程猫IDE体验:对Scratch的改进
前两天偶遇编程猫推介(为什么没有中文的编程?), 第一眼感觉像Scratch, 求证之下确实, 并且据说有改良. 今天非常粗浅地尝试一下, 限于水平没有做出很炫的效果, 不过颇有些发现. 首先上最终效 ...
- 一些关于Viewport与device-width的东西~(转)
内容转自 http://www.cnblogs.com/koukouyifan/p/4066567.html 非常感谢 口口一凡 为我们提供的这篇文章,受益匪浅,特地转到自己的博客收藏起来. 以下是原 ...
- 纯小白入手 vue3.0 CLI - 2.7 - 组件之间的数据流
vue3.0 CLI 真小白一步一步入手全教程系列:https://www.cnblogs.com/ndos/category/1295752.html 尽量把纷繁的知识,肢解重组成为可以堆砌的知识. ...
- Vue父组件接收不到子组件$emit事件的原因分析
通常有两种情况: 事件名称不全是小写.事件名称要求全小写. 不是父子关系.这里的父子关系是严格的父子关系,祖孙关系也不行.只能一层一层触发,这在写树形组件时,很容易掉坑里.
- 转 fiddler常见的应用场景
fiddler常见的应用场景 在移动互联网时代,作为软件测试工程师,fiddler绝对是值得掌握并添加进技术栈里的工具之一. 那么,fiddler在日常的测试工作中,一般都有哪些常见的应用场景呢? ...
- JDBC-Statement,prepareStatement,CallableStatement的比较
参考:https://www.cnblogs.com/Lxiaojiang/p/6708570.html JDBC核心API提供了三种向数据库发送SQL语句的类: Statement:使用create ...
- scrapy之spider模块
scrapy中的spider的用法 : 1.scrapy命令行可以传参数给构造器 scrapy crawl myspider -a category=electronics 构造器接收传入的参数 im ...
- https://www.testingcircus.com/tell-me-about-yourself-6-sample-answers-software-testers/
https://www.testingcircus.com/tell-me-about-yourself-6-sample-answers-software-testers/ Tell Me Abou ...
- windows 2003 IIS 设置 FTP被动模式
IIS FTP 将21端口更改为xx123端口: 更改数据端口: cd c:/Inetpub/AdminScripts cscript.exe adsutil.vbs set /MSFTPSVC/Pa ...