2009年7月14日 星期二

Spring- IoC container

1.基本上是由org.springframework.beans 以及 org.springframework.context這兩個packages提供 spring ioc container.而 interface BeanFactory則提供了進階的設定.
2.Interface ApplicationContext 是一個super interface of BeanFactory,並且有其他更多的function在內,所以簡單的說,BeanFactory提供了簡單的一些設定與基本function,而ApplicationContext則是提供了更多,是一個完整的 supertset of BeanFactory.然則!建議使用ApplicationContext.
3.在ioc的控制上主要是以設定的方式進行對應,目前已知的有xml-based , annotation-based等方式
4.範例..

<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-2.5.xsd">







5.初始化一個container:
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"services.xml","daos.xml"});
BeanFactory factory = context;
6.可以用import的方式把config metadata 綁進至另一個config metadata








7.BeanFactory 式可以讓你用來讀取bean definitions,透過以下指令處理
Resource res = new FileSystemResource("beans.xml");
BeanFactory fac = new XmlBeanFactory(res);
8.DI - Dependency Injection,有區分為 constructor injection and setter injection
9.到底該用 constructor or setter based DI ?大部分建議用Setter DI

沒有留言: