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提供模板

 

猜你喜欢

转载自thus.iteye.com/blog/2351434