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;}}
<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>
<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>
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());}}
public Class Teacher implements Serializable { ...private int id;public void setId(int id){this.id = id;}public int getId(){return this.id;}...}
package = com.test.xbean #Teacher类的包名teacher = com.test.xbean.Teacher #指定xml配置文件中的teacher标签代表的类的全路径 teacher.alias.teacherId = id #将teacher标签中的productId属性id映射为成员变量
<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>
<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>