package model;
import java.util.Date;
import java.util.List;
import java.util.Map;
public class DemoBean {
private String name = "xxx";
private java.util.Date year;
private List<String> list1;
private Map<String, String> map;
private List<DemoBean> list2;
public DemoBean() {
}
public DemoBean(String name) {// 我是有一個參數的建構子
this.name = name;
}
public DemoBean(String name, Date year) {// 我是有一個參數的建構子
this.name = name;
this.year = year;
}
// public DemoBean(List<String> list1) {// 我是有一個參數的建構子
// this.list1 = list1;
// }
public DemoBean(List<String> list1, Map map) {// 我是有兩個參數的建構子
this.list1 = list1;
this.map = map;
}
public DemoBean(List<DemoBean> list2) {// 我是有兩個參數的建構子
this.list2 = list2;
}
public List<String> getList1() {
return list1;
}
public void setList1(List<String> list1) {
this.list1 = list1;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getYear() {
return year;
}
public void setYear(Date year) {
this.year = year;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public List<DemoBean> getList2() {
return list2;
}
public void setList2(List<DemoBean> list2) {
this.list2 = list2;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
<bean id="logProvider" class="util.LogProvider"></bean>
<!-- ↑這就是一個Bean物件,被呼叫時就會new出一個util.LogProvider的物件 -->
<bean id="demoBean0" class="model.DemoBean">
<constructor-arg value="這是一個demo的value1,用建構子一個參數"></constructor-arg>
<!-- 簡單的值 -->
</bean>
<!-- Spring會依據設定黨所提供的參數型別及數量來搜尋最是當的建構子,所以給value即可 -->
<bean id="year1" class="java.util.Date"></bean>
<!-- 先new一個日期,給予id year1 -->
<bean id="demoBean1" class="model.DemoBean">
<constructor-arg value="這是一個demo的value2,用建構子,兩個參數,用ref連結日期"></constructor-arg>
<constructor-arg ref="year1"></constructor-arg>
<!--ref 接受Spring管理的Bean元件 -->
</bean>
<bean id="demoBean4" class="model.DemoBean">
<constructor-arg value="這是一個demo的value2,用建構子,兩個參數,用bean直接輸入值"></constructor-arg>
<constructor-arg>
<bean class="java.util.Date"></bean>
<!--直接在裡面new一個日期 -->
</constructor-arg>
</bean>
<bean id="demoBean5" class="model.DemoBean">
<constructor-arg>
<bean class="java.util.Date"></bean>
<!-- 不受Spring管理的Bean元件 -->
</constructor-arg>
<constructor-arg
value="這是一個demo的value2,用建構子,兩個參數,設定檔中參數的順序跟建構子裡不一樣,但Spring不會搞錯"></constructor-arg>
</bean>
<bean id="demoBean6" class="model.DemoBean">
<constructor-arg name="year">
<bean class="java.util.Date"></bean>
</constructor-arg>
<constructor-arg name="name"
value="這是一個demo的value2,用建構子,兩個參數,用名字,沒設定type"></constructor-arg>
</bean>
<bean id="demoBean7" class="model.DemoBean">
<constructor-arg name="year" type="java.util.Date">
<bean class="java.util.Date"></bean>
</constructor-arg>
<constructor-arg name="name" type="java.lang.String"
value="這是一個demo的value2,用建構子,兩個參數,用名字,有設定type"></constructor-arg>
</bean>
<bean id="demoBean8" class="model.DemoBean">
<constructor-arg type="java.util.Date">
<bean class="java.util.Date"></bean>
</constructor-arg>
<constructor-arg type="java.lang.String"
value="這是一個demo的value2,用建構子,兩個參數,沒用名字,有設定type"></constructor-arg>
</bean>
<!-- <bean id="demoBean9" class="model.DemoBean"> -->
<!-- 原本建構子只有一個輸入參數為list -->
<!-- <constructor-arg type="java.util.List" name="list1"> -->
<!-- <list> -->
<!-- <value>d1</value> -->
<!-- <value>d2</value> -->
<!-- <value>d3</value> -->
<!-- <value>d4</value> -->
<!-- </list> -->
<!-- 設定list的值 -->
<!-- </constructor-arg> -->
<!-- </bean> -->
<bean id="demoBean10" class="model.DemoBean">
<!-- 建構子有兩個輸入參數,list跟map -->
<!-- java Collection -->
<constructor-arg type="java.util.List" name="list1">
<list>
<value>d1</value>
<value>d2</value>
<value>d3</value>
<value>d4</value>
</list>
<!-- 設定list的值 -->
</constructor-arg>
<constructor-arg type="java.util.Map" name="map">
<map>
<entry key="1" value="a"></entry>
<entry key="2" value="b"></entry>
<entry key="3" value="c"></entry>
</map>
<!-- 設定list的值 -->
</constructor-arg>
</bean>
<bean id="demoBean2" class="model.DemoBean">
<property name="name" value="這是一個setter方法"></property>
</bean>
<bean id="demoBean3" class="model.DemoBean">
<property name="name">
<null></null>
<!-- 設定為null值 -->
</property>
</bean>
<bean id="demoBean11" class="model.DemoBean">
<constructor-arg type="java.util.List" name="list2">
<list>
<ref bean="demoBean2"></ref>
<ref bean="demoBean3"></ref>
</list>
</constructor-arg>
</bean>
</beans>
package org.iiiedu.samuel.ProjSpringDemo;
import model.DemoBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestDemoBean {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.config.xml");
DemoBean demoBean0 = (DemoBean) context.getBean("demoBean0");
System.out.println("demoBean0="+demoBean0.getName());
DemoBean demoBean1 = (DemoBean) context.getBean("demoBean1");
System.out.println("demoBean1="+demoBean1.getName()+","+demoBean1.getYear());
DemoBean demoBean4 = (DemoBean) context.getBean("demoBean4");
System.out.println("demoBean4="+demoBean4.getName()+","+demoBean4.getYear());
DemoBean demoBean5 = (DemoBean) context.getBean("demoBean5");
System.out.println("demoBean5="+demoBean5.getName()+","+demoBean5.getYear());
DemoBean demoBean6 = (DemoBean) context.getBean("demoBean6");
System.out.println("demoBean6="+demoBean6.getName()+","+demoBean6.getYear());
DemoBean demoBean7 = (DemoBean) context.getBean("demoBean7");
System.out.println("demoBean7="+demoBean7.getName()+","+demoBean7.getYear());
DemoBean demoBean8 = (DemoBean) context.getBean("demoBean8");
System.out.println("demoBean8="+demoBean8.getName()+","+demoBean8.getYear());
// DemoBean demoBean9 = (DemoBean) context.getBean("demoBean9");
// System.out.println("demoBean9="+demoBean9.getList1());
DemoBean demoBean10 = (DemoBean) context.getBean("demoBean10");
System.out.println("demoBean10="+demoBean10.getList1()+demoBean10.getMap());
DemoBean demoBean2 = (DemoBean) context.getBean("demoBean2");
//context.getBean()回傳萬物件之父Object,所以要強制轉型
System.out.println("demoBean2="+demoBean2.getName());
DemoBean demoBean3 = (DemoBean) context.getBean("demoBean3");
//context.getBean()回傳萬物件之父Object,所以要強制轉型
System.out.println("demoBean3="+demoBean3.getName());
DemoBean demoBean11 = (DemoBean) context.getBean("demoBean11");
System.out.println("demoBean11="+demoBean11.getList2());
}
}
六月 07, 2016 5:54:05 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
資訊: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@117ae12: startup date [Tue Jun 07 17:54:05 CST 2016]; root of context hierarchy
六月 07, 2016 5:54:05 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
資訊: Loading XML bean definitions from class path resource [beans.config.xml]
demoBean0=這是一個demo的value1,用建構子一個參數
demoBean1=這是一個demo的value2,用建構子,兩個參數,用ref連結日期,Tue Jun 07 17:54:05 CST 2016
demoBean4=這是一個demo的value2,用建構子,兩個參數,用bean直接輸入值,Tue Jun 07 17:54:05 CST 2016
demoBean5=這是一個demo的value2,用建構子,兩個參數,設定檔中參數的順序跟建構子裡不一樣,但Spring不會搞錯,Tue Jun 07 17:54:05 CST 2016
demoBean6=這是一個demo的value2,用建構子,兩個參數,用名字,沒設定type,Tue Jun 07 17:54:05 CST 2016
demoBean7=這是一個demo的value2,用建構子,兩個參數,用名字,有設定type,Tue Jun 07 17:54:05 CST 2016
demoBean8=這是一個demo的value2,用建構子,兩個參數,沒用名字,有設定type,Tue Jun 07 17:54:05 CST 2016
demoBean10=[d1, d2, d3, d4]{1=a, 2=b, 3=c}
demoBean2=這是一個setter方法
demoBean3=null
demoBean11=[model.DemoBean@158fe70, model.DemoBean@136e637]
留言列表