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中的各个类中,去掉所有注解标签.然后为 ...
随机推荐
- ElasticSearchserver操作命令
在win7环境,进入elasticsearch安装文件夹的bin文件夹: 1. elasticsearch.bat 就能够启动elasticsearch了.执行这个插件的优点是:elasticsear ...
- Spring Cloud(十一):Spring Cloud Zuul网关 Filter、熔断、重试、高可用的使用方式
上篇文章主要介绍了Zuul网关使用模式,以及自动转发机制,但其实Zuul还有更多的应用场景,比如:鉴权.流量转发.请求统计等等,这些功能都可以使用Zuul来实现. Zuul的核心 Filter是Zuu ...
- Process Autocad by python
一.处理AutoCad模块 -pyautocad 1.安装 pip install pyautocad 注:1.该操作会自动安装 comtypes模块,如果其他方式安装,请自行安装comtypes模块 ...
- caffe2 安装与介绍
http://blog.csdn.net/yan_joy/article/details/70241319 标签: 深度学习 2017-04-19 15:31 5970人阅读 评论(0) 收藏 举报 ...
- MVC准备工作
准备工作 打开VS创建一个ASP.NET MVC空项目,在http://www.asp.net/mvc/overview/getting-started/introduction/getting-st ...
- php 获取当前页面url路径
#测试网址: http://localhost/blog/testurl.php?id=5 //获取域名或主机地址 echo $_SERVER['HTTP_HOST'].""; # ...
- MVC——分页
添加类PageBar.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; ...
- Requires: libstdc++.so.6(GLIBCXX_3.4.15)(64bit)
Error: Package: mysql-community-server-8.0.12-1.el7.x86_64 (mysql80-community) Requires: libstdc++.s ...
- 一款很好的日程安排插件fullcalendar 非常适合OA等系统
1.插件下载 http://arshaw.com/fullcalendar/download/ 2. <!DOCTYPE html> <meta http-equiv="C ...
- ElasticSearch(三)mac安装
1.首先要安装jdk 2.到官网或是用brew下载ElasticSearch 安装包,这边我们选择在官网下载对应的安装包 https://www.elastic.co/cn/downloads/ela ...