Bean作用域的配置以及 Spring各种注入方式实例 list set map props
1.Bean有两种作用域属性,singleton 和 prototype ,默认为前者。对于singleton,当请求的 Bean 相同时,则不再重新生成新的实例化对象,通常应用程序中的组多组件都只需要一个实例就足够了。而 prototype ,用于每次返回 Bean 的一个新的实例,例如需要获取系统实时时间。
<bean id="mydate" class="com.lihui.MyDate" scope="prototype"></bean>
2.各种注入方式
所有的注入方式在Spring.xml 文件中配置如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:p="http://www.springframework.org/schema/p" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 7 8 <!-- 注入基本类型 --> 9 <bean id="person" class="com.lihui.Person"> 10 <property name="name" value="张三"></property> 11 <property name="password" value="hello"></property> 12 <property name="chock" value="true"></property> 13 </bean> 14 15 <!-- 注入list类型和数组类型--> 16 <bean id="beansDemoClass" class="com.lihui.BeansDemoClass"> 17 <property name="fruit"> 18 <list> 19 <value>荔枝</value> 20 <value>桔子</value> 21 <value>樱桃</value> 22 </list> 23 </property> 24 <property name="num"> 25 <list> 26 <value>20</value> 27 <value>45</value> 28 <value>12</value> 29 </list> 30 </property> 31 </bean> 32 33 <!-- 注入引用类型 --> 34 <bean id="mydate" class="com.lihui.MyDate" scope="prototype"> 35 <property name="date" ref="refdate"></property> 36 </bean> 37 <bean id="refdate" class="java.util.Date" scope="prototype"></bean> 38 39 <!-- 注入 set map props 类型 --> 40 <bean id="gatherDemp" class="com.lihui.SetMapProps"> 41 <property name="scoreMap"> 42 <map> 43 <!-- map 必须是 key-value 对应的 --> 44 <entry key="Maths"> 45 <value>98</value> 46 </entry> 47 <entry key="English"> 48 <value>95</value> 49 </entry> 50 </map> 51 </property> 52 <property name="properties"> 53 <!-- 定义 properties 属性 --> 54 <props> 55 <!-- props 必须是 key-value 对应的 --> 56 <prop key="path">C:/MyDocument/MyMusic</prop> 57 <prop key="filename">lihui.txt</prop> 58 </props> 59 </property> 60 <property name="settest"> 61 <set> 62 <!-- set元素,使用value、bean、ref 等指定系列值 --> 63 <value>set值</value> 64 </set> 65 </property> 66 </bean> 67 </beans>
(1)基本注入方式
person.java
1 public class Person { 2 public String name; 3 public String password; 4 public boolean islogin; 5 6 public void setName(String name){ 7 this.name = name; 8 } 9 public void setPassword(String password){ 10 this.password = password; 11 } 12 public void setChock(boolean islogin){ 13 this.islogin = islogin; 14 } 15 }
调用方法:
1 public static void main(String[] args) { 2 XmlBeanFactory bFactory = new XmlBeanFactory(new ClassPathResource( 3 "Spring.xml")); 4 Person person = (Person) bFactory.getBean("person"); 5 System.out.println("姓名:" + person.name + " 密码:" + person.password 6 + " 状态:" + person.islogin); 7 }
(2)注入引用类型
MyDate.java
1 public class MyDate { 2 private Date date; 3 public void setDate(Date date){ 4 this.date = date; 5 } 6 public void getDate(){ 7 System.out.println(date); 8 } 9 }
应用举例:
1 public static void main(String[] args) { 2 XmlBeanFactory bFactory = new XmlBeanFactory(new ClassPathResource("Spring.xml")); 3 MyDate myDate = (MyDate)bFactory.getBean("mydate"); 4 myDate.getDate(); 5 try { 6 Thread.sleep(1000); 7 } catch (InterruptedException e) { 8 // TODO Auto-generated catch block 9 e.printStackTrace(); 10 } 11 ((MyDate)bFactory.getBean("mydate")).getDate(); 12 }
(3)注入list类型和数组类型
BeansDemoClass.java
1 public class BeansDemoClass { 2 public List<String> fruit; 3 public int[] num; 4 public void setFruit(List<String> fruit){ 5 this.fruit = fruit; 6 for(String f : fruit){ 7 System.out.println(f); 8 } 9 } 10 public void setNum(int[] num){ 11 this.num = num; 12 for(int n : num){ 13 System.out.println(n); 14 } 15 } 16 }
应用举例:
1 public static void main(String[] args) { 2 XmlBeanFactory bFactory = new XmlBeanFactory(new ClassPathResource("Spring.xml")); 3 BeansDemoClass bDemoClass = (BeansDemoClass)bFactory.getBean("beansDemoClass"); 4 System.out.println(bDemoClass.fruit + " " + bDemoClass.num); 5 }
(4)注入set、map 及 props 类型
SetMapProps.java
1 public class SetMapProps { 2 public Map scoreMap = new HashMap<String, String>(); 3 public Properties properties = new Properties(); 4 public Set settest = new HashSet<String>(); 5 public void setScoreMap(Map scoreMap){ 6 this.scoreMap = scoreMap; 7 System.out.println(this.scoreMap.get("English")); 8 } 9 public void setProperties(Properties properties){ 10 this.properties = properties; 11 System.out.println(this.properties.get("path")); 12 System.out.println(this.properties.get("filename")); 13 } 14 public void setSettest(Set settest){ 15 this.settest = settest; 16 System.out.println(settest.toString()); 17 } 18 }
应用:
1 public static void main(String[] args) { 2 XmlBeanFactory bFactory = new XmlBeanFactory(new ClassPathResource("Spring.xml")); 3 SetMapProps beans = (SetMapProps) bFactory.getBean("gatherDemp"); 4 5 }