1.创建XML配置规范
<?xml version="1.0" encoding="UTF-8"?>
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- ... -->
</beans>
2.声明bean
<bean id="loginPanel" class="top.tanmujin.login.loginPanel" />
3.借助构造器初始化bean
<bean id="loginPanel" class="top.tanmujin.login.loginPanel">
<constructor-arg ref="compactDisc" /> <!--传递对compactDisc的引用-->
<constructor-arg value="This is String" /> <!--传递一个字符串-->
<constructor-arg><null/></constructor-arg> <!--给构造器传递null-->
<constructor-arg> <!--传递list-->
<list>
<value>1</value>
<value>2</value>
<value>3</value>
<value>4</value>
</list>
</constructor-arg>
<constructor-arg> <!--传递list,元素为对 bean 的引用-->
<list>
<ref bean="bean1" />
<ref bean="bean2" />
<ref bean="bean3" />
<ref bean="bean4" />
</list>
</constructor-arg>
<constructor-arg> <!--set与list区别不大-->
<set>
<value>1</value>
<value>2</value>
<value>3</value>
<value>4</value>
</set>
</constructor-arg>
</bean>
4.为bean设置属性
<bean id="loginPanel" class="top.tanmujin.login.loginPanel" >
<property name="compactDisc" ref="compactDisc" /> <!--注入bean的引用-->
<property name="artist" value="The Beatles" /> <!--注入字符串-->
</bean>