Spring XMLDI
来源:互联网 发布:com.cn域名注册 编辑:程序博客网 时间:2024/06/07 23:29
package cn.happy.xmldi;/** * Created by 刘振宇 on 2017/7/26. */public class Car { private String xinghao; private String clolr; public Car() { } public Car(String xinghao, String clolr) { this.xinghao = xinghao; this.clolr = clolr; } @Override public String toString() { return "Car{" + "xinghao='" + xinghao + '\'' + ", clolr='" + clolr + '\'' + '}'; } public String getXinghao() { return xinghao; } public void setXinghao(String xinghao) { this.xinghao = xinghao; } public String getClolr() { return clolr; } public void setClolr(String clolr) { this.clolr = clolr; }}
package cn.happy.xmldi;/** * Created by 刘振宇 on 2017/7/26. */public class Student { private String name; private int age; private Car car; public Student() { } public Student(String name, int age, Car car) { this.name = name; this.age = age; this.car = car; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + ", car=" + car + '}'; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; }}<bean id="car" class="cn.happy.xmldi.Car"> <property name="xinghao" value="兰博基尼"></property> <property name="clolr" value="红色"></property> </bean> <!--构造注入--> <bean id="stu" class="cn.happy.xmldi.Student"> <constructor-arg index="0" value="刘"></constructor-arg> <constructor-arg index="1" value="1"></constructor-arg> <constructor-arg index="2" ref="car"></constructor-arg> </bean>这里还可以使用p命名空间
在头文件中加入:
xmlns:p="http://www.springframework.org/schema/p"<!--p命名空间--><bean id="stu" class="cn.happy.xmldi.Student" p:name="刘" p:age="18" p:car-ref="car"></bean>下面是集合注入:
package cn.happy.xmldi;import java.util.List;import java.util.Map;import java.util.Properties;import java.util.Set;/** * Created by 刘振宇 on 2017/7/26. */public class Prop { private String[] array; private List<String> list; private Set<String> set; Map<String,String> map; Properties properties; public String[] getArray() { return array; } public void setArray(String[] array) { this.array = array; } public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public Set<String> getSet() { return set; } public void setSet(Set<String> set) { this.set = set; } public Map<String, String> getMap() { return map; } public void setMap(Map<String, String> map) { this.map = map; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; }}<!--集合注入array--><bean id="array" class="cn.happy.xmldi.Prop"> <property name="array"> <array> <value>刘</value> <value>振</value> <value>宇</value> </array> </property></bean>测试:
//array@org.junit.Test public void Testarray(){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContextxmldi.xml"); Prop prop =(Prop) context.getBean("array"); System.out.println(prop.getArray()[0]); }结果:<!--list--><bean id="list" class="cn.happy.xmldi.Prop"> <property name="list"> <list> <value>刘</value> <value>振</value> <value>宇</value> </list> </property></bean>测试://list@org.junit.Testpublic void Testlist(){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContextxmldi.xml"); Prop prop =(Prop) context.getBean("list"); System.out.println(prop.getList());}结果:<!--set--><bean id="set" class="cn.happy.xmldi.Prop"> <property name="set"> <set> <value>刘</value> <value>振</value> <value>宇</value> </set> </property></bean>测试://set@org.junit.Testpublic void Testset(){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContextxmldi.xml"); Prop prop =(Prop) context.getBean("set"); System.out.println(prop.getSet());}结果:<!--map--><bean id="map" class="cn.happy.xmldi.Prop"> <property name="map"> <map> <entry key="01" value="刘"></entry> <entry key="02" value="振"></entry> <entry key="03" value="宇"></entry> </map> </property></bean>测试://map@org.junit.Testpublic void Testmap(){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContextxmldi.xml"); Prop prop =(Prop) context.getBean("map"); System.out.println(prop.getMap());}结果:<!--Properties--><bean id="Properties" class="cn.happy.xmldi.Prop"> <property name="properties"> <props> <prop key="01">刘</prop> <prop key="02">振</prop> <prop key="03">宇</prop> </props> </property></bean>测试://Properties@org.junit.Testpublic void TestProperties() { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContextxmldi.xml"); Prop prop = (Prop) context.getBean("Properties"); System.out.println(prop.getProperties());}结果:
阅读全文
0 0
- Spring XMLDI
- Spring XmlDI AnnotationDI
- Spring xmlDI和注解
- xmldi和注解
- Srping的.xmldi和注解 终级版
- Spring
- Spring
- spring
- spring
- spring
- spring
- Spring
- spring
- spring
- Spring
- Spring
- spring
- Spring
- githubPages+jekyll制作博客教程
- CodeForces
- HDU 6040 Hints of sd0061 STL
- nginx 配置项 root与alias区别
- BeanFactory和ApplicationContext的区别
- Spring XMLDI
- Android Intent.FLAG_NEW_TASK详解,包括其他的标记的一些解释
- SharedPreferences的commit和apply
- WERTYU
- 多个panel控件重叠
- Qt Quick核心编程从入门到精通----系列教程
- 耳机
- 第21节-命令别名与历史命令
- 十六进制色值