基于XML方式的Bean装配实例

开发者福利!热门AI工具限时免费用 购周边即赠Coding Plan Lite,Claude Code、Cursor等20+工具畅享,效率翻倍! 阅读详情

 

一、首先定义一个用于演示原型模式下Bean实例化的User类,User实例的ID是随机生成的整数:

 

package test.spring.xml;

import java.util.Random;
//用户持久化类
public class User {
	Integer id;        //自然ID号
	String userName;   //用户名
	String userPwd;     //用户密码
	Random rnd =new Random();
	//默认构造方法
	public User(){
		this.id =rnd.nextInt(1000);   //产生一个1000以内的随机ID
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getUserPwd() {
		return userPwd;
	}
	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}
	public Random getRnd() {
		return rnd;
	}
	public void setRnd(Random rnd) {
		this.rnd = rnd;
	}
	
}


 

二、接下来定义一个包含各种需求演示的数据类型属性,自定义构造方法,初始化方法与销毁方法等的业务类XmlIocBean

 

package test.spring.xml;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**用于演示XML方式装备Bean实例的测试类*/
public class XmlIocBean {
	//定义两个通过构造方法注入的属性
	Date now;
	SimpleDateFormat sf;
	//定义集合类型的属性
	int [] intnumber;
	List list;
	Set set;
	Map map;
	Properties props;
	//默认构造方法
	public XmlIocBean (){}
	//自定义构造方法
	public XmlIocBean(SimpleDateFormat sf,Date now){
		this.now=now;
		this.sf=sf;
	}
	//定义初始化方法
	public void myInit(){
		System.out.println("XmlIocBean类的初始化方法myInit()被调用了!");
	}
	//定义一个业务方法输出各属性值
	public void execute(){
		System.out.println("现在的时间是:"+sf.format(now));
		System.out.println("intnumber数组元素内容为:");
		for(int i:intnumber){
			System.out.println("\t"+i);
		}
		System.out.println("\r\n list实例中的元素内容为:");
		for(Object obj:list){
		if(obj instanceof User) System.out.println("\t 用户ID="+((User)obj).getId());	
		else System.out.println("\t"+obj);
		}
		System.out.println("\r\n set实例中的元素内容为:");
		for(Object obj:set){
			System.out.println("\t"+obj+"\t");
		}
		System.out.println("\r\tmap实例中的元素内容为:");
		for(Object key:map.keySet()){
			if(map.get(key) instanceof User){
				System.out.println("\t 用户ID="+((User)map.get(key)).getId());
			}else{
				System.out.println("\t"+key+"="+map.get(key));
			}
		}
		System.out.println("\r\nprops实例中元素内容为:");
		for(Object key:props.keySet()){
			System.out.println("\t"+key+"="+props.get(key));
		}
		System.out.println();
	}
	
	
    //定义销毁方法
	public void myDestory(){
		System.out.println("XmlIocBean类的销毁方法myDestory被调用了!");
	}
	//省略各属性的set、get方法
}


三、下面在Spring的XMl配置文件中配置User与XmlIocBean类的装配信息:

 

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    <!-- 通过构造方法注入属性值装配SimpleDateFormat单实例 -->
	<bean id="simpleDateFormat" class="java.text.SimpleDateFormat">
	<!-- 在注入构造参数时,如果只有一个字符串类型的参数,则可省略index与type属性 -->
	<constructor-arg value="yyyy年MM月dd日 hh时mm分ss秒"/>
	</bean>
	<!-- 使用原型模式装配User实例 -->
	<bean id ="user" class="test.spring.xml.User" scope="prototype">
		<!-- 使用set方式注入属性值 -->
		<property name="userName" value="lilubin"/>
		<property name="userPwd" value="123456"/>
	</bean>
	<!-- 使用单例模式装配XmlIocBean实例,同时制定初始化方法与销毁方法 -->
	<bean id="xmlIocBean" class="test.spring.xml.XmlIocBean" init-method="myInit" destroy-method="myDestory">
	<!-- 使用构造方法注入 -->
	<constructor-arg index="0" type="java.text.SimpleDateFormat" ref="simpleDateFormat"/>
	<constructor-arg index="1" type="java.util.Date">
	<!-- 定义内部Bean实例 -->
	<bean class="java.util.Date"/>
	</constructor-arg>
	<!-- 使用set方法注入数组类型属性 -->
	<property name="intnumber" >
    <list>
    	<value>2</value>
    	<value>16</value>
    	<value>43</value>
    </list>
	</property>
	<!-- 使用set方式注入List类型数据 -->
	<property name="list">
		<list>
			<value>元素1</value>
			<!-- 由于上面的User实例使用原型模式装配,每次引用的将是全新的User实例 -->
			<ref bean="user"/>
			<ref bean="user"/>
	    </list>
	</property>
	<!-- 使用set方式注入Set类型属性 -->
	<property name="set">
	<set>
		<value>set元素1</value>
		<value>set元素2</value>
	</set>
	</property>
	<!-- 使用set方式注入Map类型属性 -->
	<property name="map">
	<map>
		<entry key="key1"><value>map元素1</value></entry>
		<entry key="key2"><ref bean="user"/></entry>
	</map>
	</property>
	<!-- 使用set方式注入Properties类型属性 -->
	<property name="props">
		<props>
		<prop key="prop1">properties元素1</prop>
		<prop key="prop2">properties元素2</prop>
		</props>
	</property>
	</bean>
</beans>


四、最后编写XmlIocBean类的Junit测试用例testXmlIocBean:

 

package test.spring.junit;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import test.spring.xml.XmlIocBean;

public class testXmlIcoBean {
	
	static AbstractApplicationContext cxt;
	static XmlIocBean xmlIocBean;
	//初始化Application容器
	@BeforeClass
	public static void setUpBeforeClass()throws Exception{
		//使用ClassPathXmlApplicationContext方式初始化ApplicationContext容器
		cxt=new ClassPathXmlApplicationContext("xmlIocBean.xml");
		xmlIocBean =(XmlIocBean) cxt.getBean("xmlIocBean");
	}
@Test
public void testExecute(){
	xmlIocBean.execute();
	//手动关闭Spring容器
	cxt.close();
}
	
}


六、测试用例运行的结果如图:

 

XmlIocBean类的初始化方法myInit()被调用了!
现在的时间是:2012年10月09日 10时48分24秒
intnumber数组元素内容为:
	2
	16
	43

 list实例中的元素内容为:
	元素1
	 用户ID=218
	 用户ID=927

 set实例中的元素内容为:
	set元素1	
	set元素2	

	map实例中的元素内容为:
	key1=map元素1
	 用户ID=858

props实例中元素内容为:
	prop2=properties元素2
	prop1=properties元素1

XmlIocBean类的销毁方法myDestory被调用了!


 

 

 

 

 

 

 

 

 

 

 

 

[spring学习]3、基于xml配置bean的常见方式 在这篇文章中,讲解了一些基于xml配置bean的常用方式,当然,方式还有很多,这里只说明了一些,上面的只有配置,就单纯看的化肯定会有点模糊,大家可以自己创建一个测试类来对每种配置进行测试。篇幅有限,其他的配置方法在下一篇文章中进行说明。............ 阅读详情

相关推荐

Bean装配方式-基于XML装配

1.设值注入 使用该方法时Bean类必须要有一个无参构造方法,并且Bean类必须为属性提供setter方法 在配置文件中,使用<property>元素来为每个属性注入值。 Bean类示例代码如下: package com.assemble; import java.util.List; public class User { private String usernam...

YOUNG的博客 679

Bean基于Xml装配

Bean基于xml装配分为:设置注入和构造注入。 设置注入需要满足两点要求: 1。Bean类必须提供一个默认的无参构造方法。 2。Bean类必须为需要注入的属性提供对应的Setter方法。 构造注入满足要求:提供带所有参数的有参构造方法。 下面是实现步骤; 1.创建Bean package com.itheima.assemble; import java.util.List; public c...

Java开发工程师,专注微服务、分布式系统,部署运维,分享实战经验和源码解析。 675

【Spring】Spring中的Bean - 5、Bean装配方式(XML、注解(Annotation)、自动装配)

Bean装配方式 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean 文章目录Bean装配方式基于XML装配基于注解(Annotation)的装配自动装配 IoC是Spring框架的核心内容,使用多种方式完美的实现了IoC,可以使用XML配置,也可以使用注解,新版本的Spring也可以零配置实现IoC。 控制反转(IoC...

渐若窥宏大的博客 1852

第2章—装配Bean—通过XML装配Bean

通过XML装配Bean ​ 尽管我们在生成Bean的过程中可以用到很多方法,但我们依然需要Spring的XML配置来完善更多的需求,下面就来介绍下XML装配Bean的过程是怎样的. 3.1创建XML配置规范 <?xml version="1.0" encoding="UTF-8"?> <...

weixin_34205076的博客 160

Spring基于XML装配bean的三种实例方式

说明 好久没有写博客了,觉得一个星期至少写一篇吧,所以就动手了,而且我Spring的基础不行呀,毕竟看视频自学都是很杂的,有时候听的课都不是很系统的,所以导致有些知识点不全什么的,所以在真正实践起来发现自己的问题之后又要重新学一遍,确实没有人指导你学习的方向走的弯路会比较多。不过不怕,大不了回头重新来,而且有些知识点再听一遍跟你听第一遍的感觉完全是不一样的,会感觉轻松很多,又理解得更深刻了。 实例...

hansmu的博客 2593

第三章、Spring Bean(Bean的配置、Bean实例化(三种方式)、Bean的作用域、Bean的生命周期、Bean装配方式(XML、注解))

文章目录Spring Bean一、Bean的配置二、Bean实例化1、构造方法实例化2、静态工厂实例化3、实例工厂实例化三、Bean的作用域1、singleton作用域2、prototype作用域四、Bean的生命周期五、Bean装配方式1、基于XML配置的装配(也就是构造函数装配和使用setter方法装配)2、基于注解的装配方式(1)Component注意:(2)Repository(3)@...

weixin_38367817的博客 1189

【Spring】--IoC容器装配Bean_基于XML配置方式--实例Bean的四种方式

一、 项目介绍 1、Mybatis的Dao接口动态代理实现CRUD(增、删、改、查)案例 2、项目开发工具:    (1) jdk1.8.0_92    (2) IDEA-2019.3.4    (3) apache-maven-3.5.2 3、项目目录结构 二、源码 1、pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h

NanYII_的博客 598

装配Bean基于XML实例方式

前言 3种bean实例化方法:默认构造、静态工厂、实例工厂 一、默认构造 <bean id="" class=""> //必须提供默认构造 二、静态工厂 2.1工厂 2.2spring配置 3.3目标类 3.4测试 三、实例工厂 实例工厂:必须先有工厂实例对象,通过实例对象创建对象。提供所有的方法都是“非静态”的。 ...

weixin_42310216的博客 211

Spring(四)基于XML装配bean(实例方式)

基于xml装配bean实例方式共有三种 1.默认构造 2.静态工厂 3.实例化工厂 1默认构造1.1 说明 用于生成实例化对象,必须未重写bean的默认构造方法。 1.2 xml配置<bean id="" class=""> 必须提供默认构造方法 id 为bean的别名,用于之后从spring容器获得实例时使用的 class 为需要创建实例的全限定类名

苏苏爱自由 2263

Spring1.1——基于xmlbean装配的三种实例方式

JAVAWEB框架学习文章索引点这里 三种实例方式: 1,默认构造(类中必须有默认构造方法) 2,静态工厂(常用于与spring整合其他框架,所有方法必须是静态的) 3,实例工厂(必须现有工厂实例对象,然后用工厂实例对象创建bean,所有的方法必须是非静态的) 简单默认构造例子: 用于实例化的对象: public class Car { public void run() ...

smallhc的博客 1436

Spring中Bean的配置、实例化、作用域、生命周期和装配方式

1 Bean的配置 1.1 概述   举个生动的例子吧。如果把Spring看作是一个大工厂的话,那么Bean就是这个工厂里的产品。当我们要使用这些产品的时候,就需要工厂为我们生产,工厂还需要管理这些产品,这就需要我们告诉工厂我们需要哪些产品(Bean),以及需要使用何种方式将这些产品(Bean装配到一起。   Bean的本质其实就是Java中的类,而Spring中的Bean其实就是对实体类的引用,来生产Java类对象,从而实现生产和管理Bean。   在Spring容器中,支持两种格式的配置文件:Prop

Haa__的博客 1494

第二章 Bean实例

2.2.1构造器实例化 构造器实例化是指Spring容器通过Bean对应类中默认的无参构造方法来实例Bean。下面通过一个案例来演示Spring容器是如何通过构造器来实例Bean的。 在Eclipse中,创建一个名为chapter02的Web项目,在该项目的lib目录中加入Spring支持和依赖的JAR包。 2.在chapter02项目的src目录下,创建一个 com.ithe...

inexaustible的博客 590

学Spring中的Bean,这一篇就够了

文章目录Spring中的BeanBean的配置Bean实例Bean的作用域Bean的生命周期Bean装配方式基于XML装配基于注解(Annotation)的装配自动装配 Spring中的Bean Bean的本质就是Java中的类,而Spring中的Bean其实就是对实体类的引用,来生产Java类对象,从而实现生产和管理Bean 。 Spring 容器支持 XMLProperties 两种格式的配置文件,在实际开发中,最常使用的就是 XML 格式的配置方式。这种配置方式通过 XML 文件来注册并管理

zag666的博客 1216

Spring学习第二周-揭开Spring中的Bean的面纱

Bean的配置 Spring容器支持XMLProperties两种格式的配置文件,在实际开发中,最常使用的就是XML格式的配置方式。这种配置方式通过XML文件来注册并管理Bean之间的依赖关系。在Spring中,XML配置文件的根元素是, 中包含了多个子元素,每一个子元素定义了一个Bean,并描述了该Bean如何被装配到Spring容器中。元素中同样包含了多个属性以及子元素,在配置文件中,通常一个普通的Bean只需要定义id(或name)和class两个属性即可。如果在Bean中未指定id和name,则S

qq_43515378的博客 538

Spring中Bean实例方式

Spring中的bean的三种实例方式~~

WYSCODER的博客 1335

Spring第一课:基于XML装配bean(四),三种实例方式:默认构造、静态工厂、实例工厂

Spring中基于XML中的装配bean有三种方式: 1.默认构造 2.静态工厂 3.实例工厂   1.默认构造     在我们在Spring的xml文件中直接通过:     &lt;bean id="名字" class="全限定类名" &gt;&lt;/bean&gt;     来写的时候,默认走的是类的默认构造,当我们写了自己的构造方法(就算是无参的)     也是会覆盖默认的...

五彩世界的博客 572

Spring中的Bean

Spring容器会负责控制程序之间的关系,而不是由程序代码直接控制,这样控制权由应用代码转移到了外部容器,控制权发生了反转,也就是Spring的IoC(Inversion of Control, IoC)思想。Spring为我们提供了两种IoC容器,分别为BeanFactory和ApplicationContext。如果把Spring看做一个大型的工厂,而Spring容器中的Bean就是工厂的产品。Spring 容器的核心,用于消减代码耦合问题。

z_y_j229970438的博客 1296

第二章 Spring中的Bean

19、对于prototype作用域的Bean,Spring只负责创建,当容器创建了Bean实例后,Bean实例就交给客户端代码来管理,Spring容器将不再跟踪其生命周期()。16、Spring容器支持多种形式的Bean装配方式,如基于XML装配、基于注解(Annotation)的装配和自动装配(其中最常用的是基于XML装配)()。A、Spring容器支持多种形式的Bean装配方式,如基于XML装配、基于注解(Annotation)的装配和自动装配(其中最常用的是基于XML装配);

小叶的碎碎念 986
上一篇: Spring入门案例
WhatIsNotNull
博客等级 码龄15年 1粉丝 1原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值