首页 > 代码库 > spring1

spring1

源码学习第一天:


org.springframework.beans  和org.springframework.context 包是ioc 容器的基础包。

BeanFactory 接口 :负责通过配置机制管理任何类型对象。

ApplicationContext 接口: 继承BeanFactory  

可以和aop ,消息资源处理(国际化),事件发布集成。

webApplicationContext 是应用层的上下文。


beans :被ioc 容器管理的对象。


org.springframework.context.ApplicationContext    接口代表ioc 容器。

负责实例化,配置,装载beans

容器做这些工作是在配置文件或者注解中或者java 代码中获取指令。


ClassPathXmlApplicationContext  

 FileSystemXmlApplicationContext

是 ioc 容器的子类。

配置文件



<?xml version="1.0" encoding="UTF-8"?> 
  <beans xmlns="http://www.springframework.org/schema/beans" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans.xsd"> 


      <bean id="..." class="..."> 
          <!-- collaborators and configuration for this bean go here --> 
      </bean> 


      <bean id="..." class="..."> 
          <!-- collaborators and configuration for this bean go here --> 
      </bean> 


      <!-- more bean definitions go here --> 


  </beans> 


实例化 ApplicationContext 

  ApplicationContext context =  new ClassPathXmlApplicationContext(new String[] { "services.xml", "daos.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" 
      xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans.xsd"> 


      <!-- services --> 


     
    <bean id="petStore" class="org.springframework.samples.jpetstore.services.PetStoreServiceImpl"> 
          <property name="accountDao" ref="accountDao"/> 
          <property name="itemDao" ref="itemDao"/> 
          <!-- additional collaborators and configuration for this bean go here --> 
      </bean> 


      <!-- more bean definitions for services go here --> 


  </beans> 


The following example shows the data access objects daos.xml file: 


  <?xml version="1.0" encoding="UTF-8"?> 
  <beans xmlns="http://www.springframework.org/schema/beans" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans.xsd"> 


      <bean id="accountDao" 
          class="org.springframework.samples.jpetstore.dao.jpa.JpaAccountDao"> 
          <!-- additional collaborators and configuration for this bean go here --> 
      </bean> 


      <bean id="itemDao" class="org.springframework.samples.jpetstore.dao.jpa.JapItemDao"> 
          <!-- additional collaborators and configuration for this bean go here --> 
      </bean> 


      <!-- more bean definitions for data access objects go here --> 


  </beans> 


import  标签


<beans> 
      <import resource="services.xml"/> 
      <import resource="resources/messageSource.xml"/> 
      <import resource="/resources/themeSource.xml"/> 


      <bean id="bean1" class="..."/> 
      <bean id="bean2" class="..."/> 
  </beans> 



获取对象T getBean(String name, Class<T>  requiredType)


ApplicationContext context =
newClassPathXmlApplicationContext(newString[] {"services.xml", "daos.xml"});
// retrieve configured instance
PetStoreService service = context.getBean("petStore", PetStoreService.class);
// use configured instance
List<String> userList = service.getUsernameList();


bean 属性


起别名:


<alias name="subsystemA-dataSource" alias="subsystemB-dataSource"/>
<alias name="subsystemA-dataSource" alias="myApp-dataSource"/>



定义 创建对象的工厂方法

 <bean id="clientService" 
      class="examples.ClientService" 
      factory-method="createInstance"/> 


  public class ClientService { 
      private static ClientService clientService = new ClientService(); 
      private ClientService() {} 


      public static ClientService createInstance() { 
          return clientService; 
      } 
  } 


同一个bean如何定义 多个创建对象的工厂方法


 <bean id="serviceLocator" class="examples.DefaultServiceLocator"> 
      <!-- inject any dependencies required by this locator bean --> 
  </bean> 


  <!-- the bean to be created via the factory bean --> 
  <bean id="clientService" 
      factory-bean="serviceLocator" 
      factory-method="createClientServiceInstance"/> 


  public class DefaultServiceLocator { 


      private static ClientService clientService = new ClientServiceImpl(); 
      private DefaultServiceLocator() {} 


      public ClientService createClientServiceInstance() { 
          return clientService; 
      } 
  } 


One factory class can also hold more than one factory method as shown here: 


  <bean id="serviceLocator" class="examples.DefaultServiceLocator"> 
      <!-- inject any dependencies required by this locator bean --> 
  </bean> 


  <bean id="clientService" 
      factory-bean="serviceLocator" 
      factory-method="createClientServiceInstance"/> 


  <bean id="accountService" 
      factory-bean="serviceLocator" 
      factory-method="createAccountServiceInstance"/> 


  public class DefaultServiceLocator { 


      private static ClientService clientService = new ClientServiceImpl(); 
      private static AccountService accountService = new AccountServiceImpl(); 


      private DefaultServiceLocator() {} 


      public ClientService createClientServiceInstance() { 
          return clientService; 
      } 


      public AccountService createAccountServiceInstance() { 
          return accountService; 
      } 


  }