spring复习-基本类型的注入,数组,int,string等等注入
List Map Set 的存储的内容可以有基本的比如int类型,string类型,还可以有自己定义的数组。
本文的代码就是基本的一些数据类型的在这么工具类中的使用注入。
下面是例子
1.Bean的创建
?
package com.endual.bean;import java.util.List;import java.util.Map;import java.util.Set;public class People {private int id;private String name;private int[] booksid;private String[] booksname;private List<String> lists;private Map<Integer,String> maps;private Set<String> sets;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int[] getBooksid() {return booksid;}public void setBooksid(int[] booksid) {this.booksid = booksid;}public String[] getBooksname() {return booksname;}public void setBooksname(String[] booksname) {this.booksname = booksname;}public List<String> getLists() {return lists;}public void setLists(List<String> lists) {this.lists = lists;}public Map<Integer, String> getMaps() {return maps;}public void setMaps(Map<Integer, String> maps) {this.maps = maps;}public Set<String> getSets() {return sets;}public void setSets(Set<String> sets) {this.sets = sets;}}
?
2.配置文件的书写
<?xml version="1.0" encoding="UTF-8"?><!-- - Middle tier application context definition for the image database. --><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><bean id="people" value="1" /><property name="name" value="chenwei" /><property name="booksid"><list><value>101</value><value>201</value><value>301</value></list></property><property name="booksname"><list><value>bookname1</value><value>bookname2</value><value>bookname3</value></list></property><property name="lists"><list><value>list1</value><value>list2</value><value>list3</value></list></property><property name="sets"><set><value>set1</value><value>set2</value><value>set3</value></set></property><property name="maps"><map key-type="int"><entry key="1"><value>map1</value></entry><entry key="2"><value>map2</value></entry><entry key="3"><value>map3</value></entry></map></property></bean></beans>?
?
测试:
package com.endual.main;import java.io.FileInputStream;import java.io.InputStream;import java.util.List;import java.util.Map;import java.util.Set;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;import com.endual.bean.People;public class MainRun {public static void main(String[] args) {//ClassPathXmlApplicationContext factory=new ClassPathXmlApplicationContext("bean.xml");ApplicationContext context=new FileSystemXmlApplicationContext("src/applicationContext.xml");People hello = (People)context.getBean("people");int id = hello.getId() ;String name = hello.getName() ;int[] booksid = hello.getBooksid() ;List lists = hello.getLists() ;Map maps = hello.getMaps() ;Set sets = hello.getSets() ;//打印-----System.out.println("int -----> " + id);System.out.println("String ----> " + name);System.out.println("int[]----->" + booksid[0]);System.out.println("list----->" + lists.get(0));System.out.println("map----->" + maps.get(1));System.out.println("set----->" + sets.iterator().next());}}
?
结果是L:
?
int -----> 1String ----> chenweiint[]----->101list----->list1map----->map1set----->set1?
?