基于xml配置spring

本文介绍了一个使用Spring框架通过XML进行Bean配置的例子。包括定义了三个类:Me、Height及Weight,并展示了如何在XML配置文件中注入这些Bean的属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

XML使用有意义的标记来描述数据。

XML文件是由一个个称为元素的部件构成的。只要满足XML标记的命名规则,我们便可以自由地定义一系列独具匠心的标记。

在MyEclipse中新建一个Java project。

右键项目添加Spring支持

project中共有三个类

Me有两个属性Height 、 Weight。

Height有两个属性m、cm。

Weight有一个属性weight。

Me.java

package chenkeyu;
public class Me {
private Height height;
private Weight weight;

//    get/set方法
public Height getHeight() {
return height;
}
public void setHeight(Height height) {
this.height = height;
}
public Weight getWeight() {
return weight;
}
public void setWeight(Weight weight) {
this.weight = weight;
}
public String toString(){
return "身高:"+height+"\n"+"体重:"+weight;
}
}


Height.java

package chenkeyu;
public class Height {
private String m;
private String cm;

//    get/set方法
public String getM() {
return m;
}
public void setM(String m) {
this.m = m;
}
public String getCm() {
return cm;
}
public void setCm(String cm) {
this.cm = cm;
}
public String toString(){
return m+"米"+cm;
}
}

Weight.java

package chenkeyu;
public class Weight {
private String weight="65";

//    get/set方法
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
public String toString(){
return weight+"公斤";
}
}


测试类

Test.java

package chenkeyu;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test{
public static void main(String[] args) {

//使用ClassPathXmlApplicationContext方式初始化ApplicationContext容器

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

//从bean工厂容器中获取名为me的Me实例

Me me = (Me) ctx.getBean("me");

System.out.println(me);
}
}


配置文件

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- beans标记:是整个配置文件的根节点,包含一个或者多个bean元素。在该标记中可配置命名空间与schema的装载路径,还可以通过default-init-method属性为该配置文件中的所有bean实例统一指定初始化方法,通过default-destroy-method属性为该配置文件中的所有bean实例统一指定销毁方法,通过default-lazy-init属性为该配置文件中的所有bean实例统一指定是否进行延迟加载-->
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<!--  bean标记:使用该标记顶一个bean的实例化信息,用以指导bean工厂正确地进行bean的生产与装配。有class属性指定类全名, 由id(推荐使用)或name属性指定生成的bean实例名称。init-method属性指定初始化时要调用的方法,destroy-method属性实例销毁时要调用的方法。bean实例的依赖关系可通过property子标记(set方式注入)或者constructor-arg子标记(构造方式注入)来定义。bean标记中的scope属性用以设定bean实例的生成方式,当scope设置为singleton或者默认时以单例模式生成,当scope设置为prototype时则以原型(多例)模式生成-->
<bean id="height" class="chenkeyu.Height">

<!--  property标记:是bean标记的子标记,用以调用bean实例中相关set方法完成属性值得赋值,从而完成依赖关系的注入。name属性指定bean实例中的相应属性名称,属性值可通过ref属性或者value属性直接指定。也可以通过ref或者value子标记指定-->
<property name="m" value="一"></property>

<!--  value标记:通常作为constructor-arg、property、list、set、entry等标记的子标记,用一直接指定一个常量值-->
<property name="cm" value="七六"></property>
</bean>
<bean id="weight" class="chenkeyu.Weight">
<property name="weight" value="65"></property>
</bean>
<bean id="me" class="chenkeyu.Me">

<!--  ref标记:通常作为constructor-arg、property、list、set、entry等标记的子标记,-->
<property name="height" ref="height"></property>
<property name="weight" ref="weight"></property>
</bean>
</beans>


程序运行结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值