private String helloWord;
//...省略getter、setter方法
}
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www./dtd/spring-beans.dtd">
<beans>
<bean id="helloBean"
class="onlyfun.caterpillar.HelloBean">
<property name="helloWord">
<value>Hello!Justin!</value>
</property>
</bean>
</beans>
public static void main(String[] args) {
Resource rs = new FileSystemResource("beans-config.xml");
BeanFactory factory = new XmlBeanFactory(rs);
HelloBean hello = (HelloBean) factory.getBean("helloBean");
System.out.println(hello.getHelloWord());
}
}
private String name;
private String helloWord;
// 建议有要无参数建构方法
public HelloBean() {
}
public HelloBean(String name, String helloWord) {
this.name = name;
this.helloWord = helloWord;
}
//...省略getter、setter方法
}
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www./dtd/spring-beans.dtd">
<beans>
<bean id="helloBean"
class="onlyfun.caterpillar.HelloBean">
<constructor-arg index="0">
<value>Justin</value>
</constructor-arg>
<constructor-arg index="1">
<value>Hello</value>
</constructor-arg>
</bean>
</beans>
public static void main(String[] args) {
ApplicationContext context =
new FileSystemXmlApplicationContext("beans-config.xml");
HelloBean hello = (HelloBean) context.getBean("helloBean");
System.out.print("Name: ");
System.out.println(hello.getName());
System.out.print("Word: ");
System.out.println(hello.getHelloWord());
}
}
private String helloWord;
private Date date;
//...省略getter、setter方法
}
<bean id="dateBean" class="java.util.Date"/>
<bean id="helloBean" class="onlyfun.caterpillar.HelloBean">
<property name="helloWord">
<value>Hello!</value>
</property>
<property name="date">
<ref bean="dateBean"/>
</property>
</bean>
</beans>
public static void main(String[] args) {
ApplicationContext context =
new FileSystemXmlApplicationContext("beans-config.xml");
HelloBean hello = (HelloBean) context.getBean("helloBean");
System.out.print(hello.getHelloWord());
System.out.print(" It's ");
System.out.print(hello.getDate());
System.out.println(".");
}
}
<bean id="dateBean" class="java.util.Date"/>
<bean id="helloBean" class="onlyfun.caterpillar.HelloBean" autowire="byType">
<property name="helloWord">
<value>Hello!</value>
</property>
</bean>
</beans>
<bean id="dateBean" class="java.util.Date"/>
<bean id="helloBean" class="onlyfun.caterpillar.HelloBean" autowire="byName">
<property name="helloWord">
<value>Hello!</value>
</property>
</bean>
</beans>
<bean id="dateBean" class="java.util.Date"/>
<bean id="helloBean" class="onlyfun.caterpillar.HelloBean" autowire="constructor">
<property name="helloWord">
<value>Hello!</value>
</property>
</bean>
</beans>
<bean id="dateBean" class="java.util.Date"/>
<bean id="helloBean" class="onlyfun.caterpillar.HelloBean" autowire="autodetect">
<property name="helloWord">
<value>Hello!</value>
</property>
</bean>
</beans>
<bean id="dateBean" class="java.util.Date"/>
<bean id="helloBean" class="onlyfun.caterpillar.HelloBean" autowire="autodetect" dependeny-check="all">
<property name="helloWord">
<value>Hello!</value>
</property>
</bean>
</beans>
private String[] someStrArray;
private Some[] someObjArray;
private List someList;
private Map someMap;
public String[] getSomeStrArray() {
return someStrArray;
}
public void setSomeStrArray(String[] someStrArray) {
this.someStrArray = someStrArray;
}
public Some[] getSomeObjArray() {
return someObjArray;
}
public void setSomeObjArray(Some[] someObjArray) {
this.someObjArray = someObjArray;
}
public List getSomeList() {
return someList;
}
public void setSomeList(List someList) {
this.someList = someList;
}
public Map getSomeMap() {
return someMap;
}
public void setSomeMap(Map someMap) {
this.someMap = someMap;
}
}
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return name;
}
}
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www./dtd/spring-beans.dtd">
<beans>
<bean id="some1" class="onlyfun.caterpillar.Some">
<property name="name">
<value>Justin</value>
</property>
</bean>
<bean id="some2" class="onlyfun.caterpillar.Some">
<property name="name">
<value>momor</value>
</property>
</bean>
<bean id="someBean" class="onlyfun.caterpillar.SomeBean">
<property name="someStrArray">
<list>
<value>Hello</value>
<value>Welcome</value>
</list>
</property>
<property name="someObjArray">
<list>
<ref bean="some1"/>
<ref bean="some2"/>
</list>
</property>
<property name="someList">
<list>
<value>ListTest</value>
<ref bean="some1"/>
<ref bean="some2"/>
</list>
</property>
<property name="someMap">
<map>
<entry key="MapTest">
<value>Hello!Justin!</value>
</entry>
<entry key="someKey1">
<ref bean="some1"/>
</entry>
</map>
</property>
</bean>
</beans>
public static void main(String[] args) {
ApplicationContext context =
new FileSystemXmlApplicationContext(
"beans-config.xml");
SomeBean someBean =
(SomeBean) context.getBean("someBean");
// 取得数组型态依赖注入对象
String[] strs =
(String[]) someBean.getSomeStrArray();
Some[] somes =
(Some[]) someBean.getSomeObjArray();
for(int i = 0; i < strs.length; i++) {
System.out.println(strs[i] + ","
+ somes[i].getName());
}
// 取得List型态依赖注入对象
System.out.println();
List someList = (List) someBean.getSomeList();
for(int i = 0; i < someList.size(); i++) {
System.out.println(someList.get(i));
}
// 取得Map型态依赖注入对象
System.out.println();
Map someMap = (Map) someBean.getSomeMap();
System.out.println(someMap.get("MapTest"));
System.out.println(someMap.get("someKey1"));
}
}