基于XMLSchema的简化配置方式也就是Spring中的命名空间以及国际化
来源:互联网 发布:虚拟机快照限时软件 编辑:程序博客网 时间:2024/06/10 14:23
Spring2.0开始,spring允许使用基于XML Schema的配置方式来简化Spring配置文件,这种方式更加简洁,可以对Spring配置文件进行“减肥”。
util Schema下提供了如下几个元素:
constant:该元素用于获取指定类的静态Field的值。它是FieldRetrievingFactoryBean的简化配置
property-path:该元素用于获取指定对象的getter方法的返回值。它是PropertyPathFactory的简化配置
List:该元素用于定义一个List Bean,支持使用<value.../>、<ref.../>、<bean.../>等子元素来定义List集合元素。使用该标签支持如下三个属性:
–id:该属性指定定义一个名为id的List Bean实例
–list-class:该属性指定Spring使用哪个List实现类来创建Bean实例。默认使用ArrayList作为实现类
–scope:指定该List Bean实例的作用域
Set:该元素用于定义一个Set Bean,支持使用<value.../>、<ref.../>、<bean.../>等子元素来定义Set集合元素。使用该标签支持如下三个属性:
–id:该属性指定定义一个名为id的Set Bean实例
–set-class:该属性指定Spring使用哪个Set 实现类来创建Bean实例。默认使用HashSet作为实现类
–scope:指定该Set Bean实例的作用域
Map:该元素用于定义一个Map Bean,支持使用<entry.../>来定义Map的key-value对。使用该标签支持如下三个属性:
–id:该属性指定定义一个名为id的Map Bean实例
–map-class:该属性指定Spring使用哪个Map实现类来创建Bean实例。默认使用HashMap作为实现类
–scope:指定该Map Bean实例的作用域
properties:该元素用于加载一份资源文件,并根据加载的资源文件创建一个Properties Bean实例。使用该标签可指定如下几个属性:
–id:该属性指定定义一个名为id的Properties Bean实例
–location:指定资源文件的位置
–scope:指定该Properties Bean的作用域
实现代码如下:
public interface Axe {
public String chop();
}
public interface Person {
public void useAxe();
}
public class Chinese implements Person{
private Axe axe;
private List schools;
private Map scores;
private Set axes;
public Axe getAxe() {
return axe;
}
public void setAxe(Axe axe) {
this.axe = axe;
}
public List getSchools() {
return schools;
}
public void setSchools(List schools) {
this.schools = schools;
}
public Map getScores() {
return scores;
}
public void setScores(Map scores) {
this.scores = scores;
}
public Set getAxes() {
return axes;
}
public void setAxes(Set axes) {
this.axes = axes;
}
@Override
public void useAxe() {
System.out.println(axe.chop());
System.out.println(schools);
System.out.println(scores);
System.out.println(axes);
}
}
public class SteelAxe implements Axe{
@Override
public String chop() {
// TODO Auto-generated method stub
return "钢斧头砍树真快";
}
}
public class StoneAxe implements Axe {
@Override
public String chop() {
// TODO Auto-generated method stub
return "石斧头砍树真慢";
}
}
public class BeanText {
public static void main(String[] args) {
ApplicationContext acContext=new ClassPathXmlApplicationContext("bean.xml");
Person p=acContext.getBean("chinese",Person.class);
p.useAxe();
System.out.println(acContext.getBean("mt"));
System.out.println(acContext.getBean("mt1"));
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd
">
<bean id="steelAxe" class="com.cn.service.imp.SteelAxe"/>
<bean id="stoneAxe" class="com.cn.service.imp.StoneAxe"/>
<bean id="chinese" class="com.cn.service.imp.Chinese"
p:axe-ref="stoneAxe"
p:schools-ref="chin.schools"
p:scores-ref="chin.scores"
p:axes-ref="chin.axes"/>
<util:list id="chin.schools" list-class="java.util.LinkedList" scope="singleton">
<value>帝国大学</value>
<value>三一大学</value>
</util:list>
<util:map id="chin.scores" map-class="java.util.TreeMap">
<entry key="地理" value="100"></entry>
</util:map>
<util:properties id="mt" location="message_zh_CN.properties"></util:properties>
<util:properties id="mt1" location="message_en_US.properties"></util:properties>
<util:set id="chin.axes" set-class="java.util.HashSet">
<value>字符串</value>
<bean class="com.cn.service.imp.Chinese"></bean>
<ref bean="steelAxe"/>
</util:set>
</beans>
message_en_US.properties:DNF=The king of martial arts
bns=Good 3d online game
总结:
如果需要注入的参数不需要引用容器中另一个已存在的bean实例,则直接使用该需要注入的字段的名称,作为属性的key;否则,则需要在注入字段的名称后面加上“-ref”,然后作为属性的key。如上文中写到 p:axe-ref="stoneAxe"这是因为需要引用已经存在的,所以需要在后面加上-ref。
+配置方式是在c:后使用构造器参数名来指定构造器参数,Spring还支持一种通过索引来配置构造器参数的方式。上面的Bean也可改写为如下形式:
<bean id="chinese" class=“com.cn.service.imp.Chinese" c:_0-ref="steelAxe" c:_1="29"/>
c:_0-ref指定使用容器中已有的steelAxe Bean作为第一个构造器参数,c:_1="29"则指定使用29作为第二个构造器参数。在这种方式下,c:_N代表第几个构造器参数。
阅读全文
0 0
- 基于XMLSchema的简化配置方式也就是Spring中的命名空间以及国际化
- spring命名空间的配置方式
- spring 基于XML Schema的简化配置方式
- Spring-----11、基于XML Schema的简化配置方式
- spring 常用的一些命名空间配置方式及作用
- XMLSchema 模式与命名空间
- XMLSchema 模式与命名空间
- XMLSchema 模式与命名空间
- Spring-注入参数详解-[通过util命名空间简化集合类型的配置]
- spring中的命名空间以及xml详解
- Spring使用p空间配置属性——基于XML Schema的简化配置
- 基于XML schema的简化配置方式
- 基于XML Schema的简化配置方式
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(四):使用 命名空间 简化配置
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(四):使用 命名空间 简化配置
- Spring 命名空间配置
- 基于session的spring国际化配置
- 基于Spring的服务端国际化配置
- 将excel导入mysql(使用navicat)
- Pandas 对象储存
- 修改UITableViewCell左滑删除按钮的样式和自定义
- android 中如何模拟back键
- Node下的数据库操作——Sequelize
- 基于XMLSchema的简化配置方式也就是Spring中的命名空间以及国际化
- 一张图,一分钟,读懂什么是资源服务总线!
- SET NOCOUNT ON 作用
- Linux环境下 UDP服务端/客户端代码
- JVM高级特性与实践(六):Class类文件的结构(访问标志,索引、字段表、方法表、属性表集合)
- Android反编译工具的使用-Android Killer
- SQL 之 concat_ws 和 concat
- SQL 关于apply的两种形式cross apply 和 outer apply
- 科普—精原细胞分裂