Bean的基于XML配置方式
基于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&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配置方式的更多相关文章
- Spring3.2 中 Bean 定义之基于 XML 配置方式的源码解析
Spring3.2 中 Bean 定义之基于 XML 配置方式的源码解析 本文简要介绍了基于 Spring 的 web project 的启动流程,详细分析了 Spring 框架将开发人员基于 XML ...
- struts_20_对Action中所有方法、某一个方法进行输入校验(基于XML配置方式实现输入校验)
第01步:导包 第02步:配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app ...
- struts2视频学习笔记 22-23(基于XML配置方式实现对action的所有方法及部分方法进行校验)
课时22 基于XML配置方式实现对action的所有方法进行校验 使用基于XML配置方式实现输入校验时,Action也需要继承ActionSupport,并且提供校验文件,校验文件和action类 ...
- 转载 - Struts2基于XML配置方式实现对action的所有方法进行输入校验
出处:http://www.cnblogs.com/Laupaul/archive/2012/03/15/2398360.html http://www.blogjava.net/focusJ/arc ...
- ssm整合(基于xml配置方式)
本文是基于xml配置的方式来整合SpringMVC.Spring和Mybatis(基于注解的方式会再写一篇文章),步骤如下: (1)首先自然是依赖包的配置文件 pom.xml <project ...
- 02_Spring Bean的装配模式_基于XML配置方式
一.三种实例化Bean的方式 1.使用类构造器实例化(默认无参数) <bean id="bean1" class="com.demo1.Bean1"> ...
- Spring 基于xml配置方式的事务
参考前面的声明式事务的例子:http://www.cnblogs.com/caoyc/p/5632198.html 我们做了相应的修改.在dao中和service中的各个类中,去掉所有注解标签.然后为 ...
- Spring IOC容器装配Bean_基于XML配置方式
开发所需jar包 实例化Bean的四种方式 1.无参数构造器 (最常用) <?xml version="1.0" encoding="UTF-8"?> ...
- Spring 基于xml配置方式的事务(14)
参考前面的声明式事务的例子:http://www.cnblogs.com/caoyc/p/5632198.html 我们做了相应的修改.在dao中和service中的各个类中,去掉所有注解标签.然后为 ...
随机推荐
- leetcode第一刷_Permutation Sequence
这道题还挺好的,假设你的思路是每次生成一个全排列,然后累计到k次,那么停下来吧.肯定超时了亲.. 微软今年的笔试题里有一道类似的,我之前已经提到过了.是唯独0和1的字符串,求第k个排列是什么样子的.这 ...
- 如何与强势的人相处zz
要和强势的人相处良好,须知道强势的人有两个很显著的特点:一.以自我观点为中心.二.怕别人否定自己.强势的主要作用也有两个:一.支配别人.二.掩盖自卑. 首先,要区分一下强势的人和特立独行的人,这两类人 ...
- ETL Automation完整安装方法_(元数据存放在mysql数据库)
安装前介质准备: DBI-1.636.tar.gz DBD-mysql-4.037.tar.gz ETL.tar mysql-5.6.12-linux-glibc2.5-x86_64.tar.gz P ...
- ContentPresenter理解
这是2年前写了一篇文章 http://www.cnblogs.com/Clingingboy/archive/2008/07/03/wpfcustomcontrolpart-1.html 我们先来看M ...
- 【BZOJ4561】[JLoi2016]圆的异或并 扫描线
[BZOJ4561][JLoi2016]圆的异或并 Description 在平面直角坐标系中给定N个圆.已知这些圆两两没有交点,即两圆的关系只存在相离和包含.求这些圆的异或面积并.异或面积并为:当一 ...
- jquery 备忘笔记
1.选择器 a.查询所有以某字符串开头的元素 $("input[id^='dgItem_txt']") b.获取一组单选按钮中选中的值 $("input[name='it ...
- Hibernate表关系映射之多对多映射
一.多对多的实现原理 在数据库中实现多对多的关系,必须使用连接表.也就是用一个独立的表来存入两个表的主键字段,通过遍历这张表来获取两表的关联关系. 而在我们的对象中,多对多是通过两者对象类中互相建立对 ...
- 九度OJ 1102:最小面积子矩阵 (DP、缓存、剪枝)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1666 解决:504 题目描述: 一个N*M的矩阵,找出这个矩阵中所有元素的和不小于K的面积最小的子矩阵(矩阵中元素个数为矩阵面积) 输入: ...
- memcached和一致性hash算法
1 一致性hash算法的一致性 这里的一致性指的是该算法可以保持memcached和数据库中的数据的一致性. 2 什么是一致性hash算法 2.1 为什么需要一致性hash算法 现在有大量的key v ...
- 阿里云Redis开发规范(转)
一.键值设计 1. key名设计 (1)[建议]: 可读性和可管理性 以业务名(或数据库名)为前缀(防止key冲突),用冒号分隔,比如业务名:表名:id ugc:video: (2)[建议]:简洁性 ...