基于XML配置

Beans.xml

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans" 1.默认命名空间
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2.xsi标准命名空间,用于指定自定义命名空间的Schema文件
xmlns:p="http://www.springframework.org/schema/p" 3.声明p命名空间
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<bean id="car" class="com.smart.ditype.Car" 4.id为bean的名称,可以通过getbean("car")获取容器中的Bean,class为Bean的类名
p:brand="红旗123&amp;CA72" 5.采用p命名空间配置Bean属性值
p:maxSpeed="100"
p:price="20000.00"/>
</beans>

Car.class

package com.smart.ditype;

public class Car {
public String brand;
private String corp;
private double price;
private int maxSpeed; public Car() {}
public Car(String brand, double price) {
this.brand = brand;
this.price = price;
} public Car(String brand, String corp, double price) {
this.brand = brand;
this.corp = corp;
this.price = price;
}
public Car(String brand, String corp, int maxSpeed) {
this.brand = brand;
this.corp = corp;
this.maxSpeed = maxSpeed;
} public String getBrand() {
return brand;
} public void setBrand(String brand) {
this.brand = brand;
} public int getMaxSpeed() {
return maxSpeed;
} public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
} public double getPrice() {
return price;
} public void setPrice(double price) {
this.price = price;
} public String toString(){
return "brand:"+brand+"/maxSpeed:"+maxSpeed+"/price:"+price;
} }

Test

package com.smart.ditype;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.annotations.*;
import static org.testng.Assert.*; public class DiTypeTest {
public ApplicationContext factory = null; private static String[] CONFIG_FILES = { "com/smart/ditype/beans.xml" }; @BeforeClass
public void setUp() throws Exception {
factory = new ClassPathXmlApplicationContext(CONFIG_FILES);
} @Test
public void testCar(){
Car car = (Car)factory.getBean("car");
assertNotNull(car);
System.out.println(car);
}
}

Bean的基于XML配置方式的更多相关文章

  1. Spring3.2 中 Bean 定义之基于 XML 配置方式的源码解析

    Spring3.2 中 Bean 定义之基于 XML 配置方式的源码解析 本文简要介绍了基于 Spring 的 web project 的启动流程,详细分析了 Spring 框架将开发人员基于 XML ...

  2. struts_20_对Action中所有方法、某一个方法进行输入校验(基于XML配置方式实现输入校验)

    第01步:导包 第02步:配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app ...

  3. struts2视频学习笔记 22-23(基于XML配置方式实现对action的所有方法及部分方法进行校验)

    课时22 基于XML配置方式实现对action的所有方法进行校验   使用基于XML配置方式实现输入校验时,Action也需要继承ActionSupport,并且提供校验文件,校验文件和action类 ...

  4. 转载 - Struts2基于XML配置方式实现对action的所有方法进行输入校验

    出处:http://www.cnblogs.com/Laupaul/archive/2012/03/15/2398360.html http://www.blogjava.net/focusJ/arc ...

  5. ssm整合(基于xml配置方式)

    本文是基于xml配置的方式来整合SpringMVC.Spring和Mybatis(基于注解的方式会再写一篇文章),步骤如下: (1)首先自然是依赖包的配置文件 pom.xml <project ...

  6. 02_Spring Bean的装配模式_基于XML配置方式

    一.三种实例化Bean的方式 1.使用类构造器实例化(默认无参数) <bean id="bean1" class="com.demo1.Bean1"> ...

  7. Spring 基于xml配置方式的事务

    参考前面的声明式事务的例子:http://www.cnblogs.com/caoyc/p/5632198.html 我们做了相应的修改.在dao中和service中的各个类中,去掉所有注解标签.然后为 ...

  8. Spring IOC容器装配Bean_基于XML配置方式

    开发所需jar包 实例化Bean的四种方式 1.无参数构造器 (最常用) <?xml version="1.0" encoding="UTF-8"?> ...

  9. Spring 基于xml配置方式的事务(14)

    参考前面的声明式事务的例子:http://www.cnblogs.com/caoyc/p/5632198.html 我们做了相应的修改.在dao中和service中的各个类中,去掉所有注解标签.然后为 ...

随机推荐

  1. eclipse从svn检出项目

    在eclipse的project explorer 右键->import->svn->从svn检出项目,然后填写资源库的位置,完成,然后一直next. 直到项目检出完成后,选择项目, ...

  2. A20 Gate信号

    https://doc.docsou.com/ba8e6b0612d6a989b7cebeaae-5.html Gate A20 Option 功能:设置A20 地址线的控制模式 设定值:Fast 或 ...

  3. import caffe时出错:can not find module skimage.io

    import caffe时出错:can not find module skimage.io  //以下内容在ubuntu16.4上实际验证过.注意大小写的.----20170605 在命令行输入Py ...

  4. Android API Guides---NFC Basics

    本文档介绍了Android中运行基本任务NFC. 它说明了怎样在NDEF消息的形式发送和接收数据的NFC并介绍了支持这些功能的Andr​​oid框架的API. 对于更高级的主题.包含与非NDEF数据工 ...

  5. java线程中断[interrupt()函数] (转载)

    一个正常的线程中断: 从运行到真正的结束,应该有三个阶段: 正常运行. 处理结束前的工作,也就是准备结束. 结束退出. Java曾经提供过抢占式限制中断,但问题多多,例如的Thread.stop.另一 ...

  6. SQL 游标示例

    DECLARE @i INT ) --给初始值 CREATE TABLE #temp_test --创建临时表 ( num ) ) ) BEGIN INSERT INTO #temp_test ( n ...

  7. 红黑树深入剖析及Java实现(转自知乎美团点评技术团队)

    作者:美团点评技术团队 链接:https://zhuanlan.zhihu.com/p/24367771 来源:知乎 著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 红黑树是平衡 ...

  8. 九度OJ 1113:二叉树 (完全二叉树)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5079 解决:1499 题目描述: 如上所示,由正整数1,2,3--组成了一颗特殊二叉树.我们已知这个二叉树的最后一个结点是n.现在的问题是 ...

  9. redis事务和乐观锁

    1 MULTI/EXEC 执行本事务. MULTI set foo bar get foo set foo hello EXEC 在EXEC执行前,三条命令都放入队列中,然后EXEC触发执行.没有回滚 ...

  10. ETF到底是什么?

    ETF(交易所交易基金)是一种证券产品,它可以跟踪一些相关的资产,不论是股票.债券.商品,还是数字货币. ETF基金会负责跟踪指定的资产.然后放出部分股份,这些股份代表着对资产的拥有权. 交易ETF股 ...