Spring 基础

1. Spring的主要特性

a. 非侵入性框架,解耦
b. 一致性,在任何环境下都可以编程, J2EE,WEB,DESKTOP
c. 提高代码重用性
d. 自由选择各种框架

Spirng的核心技术

a. 反向控制 和依赖注入
b. 面向方面编程

2. 装配Bean的两种方法

a. Bean工厂,(org.springframework.beans.factory.BeanFactory)
BeanFactory factory = new XmlBeanFactory(new FileSystemResource("src\appliactionContext.xml"));

HelloService hello = (HelloService)factory.getBean("greeting");


b. 应用上下文 org.springframework.beans.factory.xml.XmlBeanFactory
下面两个类经常被ApplicationContext用到

FileSystemXmlApplicationContext
ClassPathXmlApplicationContext


ApplicationContext context = new FileSystemXmlApplicationContext("src\applicatioonContext.xml");
HelloService hello = (HelloService) context.getBean("alias")


3. scope属性

scope属性默认是singleton, 要想每次都获得新实例, 可以设为prototype
<bean id="greeting" class="company.user" scope="prototype">
</bean>


4. ref
引用另外一个bean

5. list属性

<bean id="sss" class="test.user">
<property>
   <list>
     <value>abcd</value>
     <idref bean="mybean"/>
   </list>
</property>
</bean>


6. Set

<bean id="sss" class="test.user">
<property name="mySet">
   <set>
     <value>abcd</value>
     <ref bean="mybean"/>
   </set>
</property>
</bean>


7. Map

<bean id="sss" class="test.user">
<property name="myMap">
   <map>
     <entry>
        <key><value>1233</value>
        <value>897</value>
     </entry>
   </map>
</property>
</bean>


8. 不想装载某些属性

<property name="myMap><null/></property>


9. 装载构造方法

spring 会根据参数类型自动搜索匹配的构造函数,会将数字当做字符串类匹配
也可加index属性

<bean ...>
   <constructor-arg index=1>
      <value>bill</value>
   </constructor-arg>
   <constructor-arg index=0>
      <value>20</value>
   </constructor-arg>
</bean>


10. 如果Bean中需要装配的属性过多,Spring 允许设置<Bean>标签的autowire属性来自动装配这些属性

autowire有下列四个值

byName,byType, consturctor,autodetect

11. 分散配置

ApplicationContext context = new FileSystemXmlApplicationContext(new String[]{"a.xml","b.xml"}


你可能感兴趣的:(spring)