首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 服务器 > Apache >

Apache XBean 容易介绍

2012-09-02 
Apache XBean 简单介绍XBean是Apache Geronimo的子项目,设计这个的目的是为了能为Geronimo的插件提供一种

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" 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 {@Testpublic 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"/></beans>

这样teacher实例中的id成员变量的值就是10了。

xbean同样支持constructor注入和对list、map、set等集合属性的注入。具体可查看Geronimo XBean

上面说的设计xbean的初衷为了简单便捷的安装各类插件。只需要将插件的jar包放到classpath中,同时在jar包中包含
META-INF/services/org/apache/xbean/spring/
这个目录,并在这个目录下设定可以自己的标签(根据自己的喜好,可以设置更具语义的标签),然后就可以很方便的在定制自己的插件了。

下面是上面这些代码的依赖:
<dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.8.2</version><scope>test</scope></dependency><dependency><groupId>org.apache.xbean</groupId><artifactId>xbean-spring</artifactId><version>3.7</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>3.0.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>3.0.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>3.0.5.RELEASE</version></dependency></dependencies>


热点排行