日期:2014-05-16 浏览次数:20937 次
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" class="com.test.xbean.Teacher"> <property name="age" 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 { @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()); } }
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"/> <