日期:2014-05-16  浏览次数:20910 次

Apache XBean 简单介绍
XBean是Apache Geronimo的子项目,设计这个的目的是为了能为Geronimo的插件提供一种方便
快捷的配置方式(具体怎么方便快捷,看完全文便知)。后来,Xbean被更多的开源项目引用。例如:jetty、Activemq等等,同时xbean也提供了对spring的支持。下面具体讲解xbean的配置方式,首先看看平常我们要在spring中配置一个javabean的步骤:
1、先创建个java bean
package com.test.xbean;

import java.io.Serializable;

public class Teacher implements Serializable {

	private static final long serialVersionUID = -8409611806997881614L;

	private int age;
	private String name;

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

2、在xml中配置bean的属性
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

	<bean id="teacher" class="com.test.xbean.Teacher">
		<property name="age" value="25" />
		<property name="name" value="berdy" />
	</bean>
</beans>


从上面bean的配置可以看出,如果要配置多个bean的时候,我们需要写一大堆的标签。其实我想着的就是告诉spring给
我生成javabean,这个javabean的类型是com.test.xbean.Teacher,属性分别是25和berdy。而现在却要写一堆property
标签。下面看看xbean的配置方式:
<beans xmlns="http://xbean.apache.org/schemas/spring/1.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:te="java://com.test.xbean"
	xsi:schemaLocation="http://xbean.apache.org/schemas/spring/1.0 classpath:/org/apache/xbean/spring/spring-beans.xsd">
		
	<te:Teacher id="teacher" age="25" name="berdy"/>
</beans>

值得注意的是,在什么文件中beans标签的默认名字空间已改变为xbean自己的名字空间,指定了自己的schema。同时需要指出的是te名字空间的URI。在这个URL已经包含了Teacher类的包名。这样的配置是不是简洁了很多呢?下面看下实际使用的代码:
package com.test.xbean;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import org.junit.Test;

public class XbeanTest {
	@Test
	public void test() {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:xbean.xml");
		Teacher teacher = (Teacher) context.getBean("teacher");
		assertNotNull(teacher);
		assertEquals(25, teacher.getAge());
		assertEquals("xjacker", teacher.getName());
	}
}

在上面的代码中,注意ClassPathXmlApplicationContext是org.apache.xbean.spring.context.ClassPathXmlApplicationContext而不是org.springframework.context.support.ClassPathXmlApplicationContext。这时,有人提出了,如果我在Teacher类里定义了一个id属性,不是和配置文件的bean的唯一标示id冲突了,这个时候怎么处理呢?当然xbean在设计的时候肯定考虑到这个了嘛。先对Teacher类修改如下
public Class Teacher implements Serializable {
    ...
	private int id;
	
	public void setId(int id){
		this.id = id;
	}
	
	public int getId(){
		return this.id;
	}
	...
}

这个时候就需要在java应用的classpath中添加一个目录树META-INF/services/org/apache/xbean/spring/然后在这个目录下根据你定义的名字空间的url建一个文件。例如:指定URL "http://xbean.test.com/teacher"。那么就需要在根据URL中的schema、contextpath、path分割来创建文件了。在META-INF/services/org/apache/xbean/spring/目录下创建这样的目录树META-INF/services/org/apache/xbean/spring/http/xbean.test.com/teacher.teacher是一个文件,java中的properties风格的文件。然后在teacher文件指定对应关系,解决上面的冲突。
package = com.test.xbean #Teacher类的包名
teacher = com.test.xbean.Teacher #指定xml配置文件中的teacher标签代表的类的全路径   
teacher.alias.teacherId = id #将teacher标签中的productId属性id映射为成员变量

修改xml配置文件如下(注意上面te名字空间的变化):
<beans xmlns="http://xbean.apache.org/schemas/spring/1.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:te="http://xbean.test.com/teacher"
	xsi:schemaLocation="http://xbean.apache.org/schemas/spring/1.0 classpath:/org/apache/xbean/spring/spring-beans.xsd">
		
	<te:Teacher id="teacher" teacherId="10" age="25" name="berdy"/>
<