Spring(1)基礎概念
1、Test Disk
MoveDisk
UsbDisk
applicationContext.xml標頭檔案
<bean name="disk" class="dao.impl.MoveDisk">
public class test extends TestCase{
ApplicationContext ctx=new
ClassPathXmlApplicationContext("applicationContext.xml");
Disk disk=(Disk)ctx.getBean("disk");
}
2、注入
<!--setter-->
<bean name="sService" class="dao.imple.StudentService">
<property name="sid" value="S001"/>
<property name="sname" value="流弊"/>
<property name="sage" value="25"/>
<!--constructor-->
<bean name="sService" class="dao.imple.StudentService">
<constructor-arg index="0" value="S001"/>
<constructor-arg index="1" value="流弊"/>
<constructor-arg index="2" value="25"/>
@Resource
@Autowise
@Qulified
聯絡
<bean name="s" class="StudentsDao">
<bean name="sService" class="dao.imple.StudentService">
<propert name="sDao" bean="s">
<bean name="s" class="StudentsDao">
<bean name="sService" class="dao.imple.StudentService">
<propert name="sDao" ref="s">
3、Set、List、Map
<List>
<value></value>
<value></value>
</List>
<List>
<bean></bean>
<bean></bean>
</List>
<Set>
</Set>
<Map>
4、型別轉換問題
(1)BeanFactory solution
<bean id="dateFormat" class="java.text.SimpleDateFormat">
<constructor-arg value="yyyy-MM-dd" />
</bean>
<bean>
<property name="birthday">
<bean factory-bean="dateFormat" factory-method="parse">
<constructor-arg value="2010-01-31" />
</bean>
</property>
</bean>
5、Bean生存範圍
Singleton:預設、引用出來兩個例子實際為同一地址。
Student s1=(Student).getBean("student");
Student s2=(Student).getBean("student");
s1==s2為true;
Prototype
s1==s2為false;
lazy-init="true"
呼叫程式才初始化然後載入
init-method="類內方法名"
/destroy-method="類內方法名"
depends-on/例項化這個bean之前要先例項化on後的bean
id等價於Java變數命名,不能出現兩個id
name沒有限定,可以出現多個 只返回後面的 且可以name="stu,student"
無類名情況
<bean class=1>
<bean class=1>
<bean class=1>
getBean("1#0");
getBean("1#1");
getBean("1#2");
6、Abstract Bean
<bean id="person" abstract="true"
class="dao.Person2">
<property name="name" value="降龍"/>
</bean>
<bean id="student" class="dao.impl.Student"
parent="person">
</bean>
person無法例項化,只能為student提供模板