| 142 | | |
| 143 | | |
| 144 | | |
| 145 | | |
| 146 | | |
| 147 | | |
| | 142 | == BeanFactory == |
| | 143 | |
| | 144 | * HelloBean.java |
| | 145 | |
| | 146 | {{{ |
| | 147 | #!java |
| | 148 | package onlyfun.caterpillar; |
| | 149 | |
| | 150 | public class HelloBean { |
| | 151 | private String helloWord; |
| | 152 | |
| | 153 | public void setHelloWord(String helloWord) { |
| | 154 | this.helloWord = helloWord; |
| | 155 | } |
| | 156 | public String getHelloWord() { |
| | 157 | return helloWord; |
| | 158 | } |
| | 159 | } |
| | 160 | |
| | 161 | }}} |
| | 162 | |
| | 163 | * beans-config.xml |
| | 164 | |
| | 165 | {{{ |
| | 166 | #!java |
| | 167 | <?xml version="1.0" encoding="UTF-8"?> |
| | 168 | <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" |
| | 169 | "http://www.springframework.org/dtd/spring-beans.dtd"> |
| | 170 | |
| | 171 | <beans> |
| | 172 | <bean id="helloBean" |
| | 173 | class="onlyfun.caterpillar.HelloBean"> |
| | 174 | <property name="helloWord"> |
| | 175 | <value>Hello!Justin!</value> |
| | 176 | </property> |
| | 177 | </bean> |
| | 178 | </beans> |
| | 179 | |
| | 180 | }}} |
| | 181 | |
| | 182 | * SpringDemo.java |
| | 183 | |
| | 184 | {{{ |
| | 185 | #!java |
| | 186 | package onlyfun.caterpillar; |
| | 187 | |
| | 188 | import org.springframework.core.io.FileSystemResource; |
| | 189 | import org.springframework.core.io.Resource; |
| | 190 | import org.springframework.beans.factory.BeanFactory; |
| | 191 | import org.springframework.beans.factory.xml.XmlBeanFactory; |
| | 192 | |
| | 193 | public class SpringDemo { |
| | 194 | public static void main(String[] args) { |
| | 195 | Resource rs = |
| | 196 | new FileSystemResource("beans-config.xml"); |
| | 197 | BeanFactory factory = |
| | 198 | new XmlBeanFactory(rs); |
| | 199 | |
| | 200 | HelloBean hello = |
| | 201 | (HelloBean) factory.getBean("helloBean"); |
| | 202 | System.out.println(hello.getHelloWord()); |
| | 203 | } |
| | 204 | } |
| | 205 | }}} |
| | 206 | |
| | 207 | == ApplicationContext == |
| | 208 | |
| | 209 | BeanFactory負責讀取Bean定義檔,管理物件的載入、生成,物件之間的關係維護,負責Bean的生命週期,對於簡單的應用程式來說,使用 BeanFactory就已經足夠,但是若要利用到Spring在框架上的一些功能以及進階的容器功能,則可以使用 ApplicationContext,BeanFactory則通常用於一些資源有限的裝置,像是行動設備。 |
| | 210 | |
| | 211 | ApplicationContext的基本功能與BeanFactory很相似,它也負責讀取Bean定義檔,維護Bean之間的關係等,然而ApplicationContext提供的一個應用程式所需的更完整的框架功能: |
| | 212 | |
| | 213 | * ApplicationContext提供取得資源檔案更方便的方法。 |
| | 214 | * ApplicationContext提供文字訊息解析的方法,並支援國際化(Internationalization, I18N)訊息。 |
| | 215 | * ApplicationContext可以發佈事件,對事件感興趣的Bean可以接收到這些事件。 |
| | 216 | |
| | 217 | |
| | 218 | Rod Johnson建議使用ApplicationContext來取代BeanFactory,在許多實作ApplicationContext的類別中,最常使用的大概是以下三個: |
| | 219 | |
| | 220 | * FileSystemXmlApplicationContext |
| | 221 | * 可指定XML定義檔的相對路徑或絕對路徑來讀取定義檔。 |
| | 222 | * ClassPathXmlApplicationContext |
| | 223 | * 從Classpath中來讀取XML定義檔。 |
| | 224 | * XmlWebApplicationContext |
| | 225 | * 在Web應用程式中的檔案架構中讀取定義檔。 |
| | 226 | |
| | 227 | |
| | 228 | * SpringDemo.java |
| | 229 | |
| | 230 | {{{ |
| | 231 | #!java |
| | 232 | |
| | 233 | package onlyfun.caterpillar; |
| | 234 | |
| | 235 | import org.springframework.context.ApplicationContext; |
| | 236 | import org.springframework.context.support.FileSystemXmlApplicationContext; |
| | 237 | |
| | 238 | public class SpringDemo { |
| | 239 | public static void main(String[] args) { |
| | 240 | ApplicationContext context = |
| | 241 | new FileSystemXmlApplicationContext("beans-config.xml"); |
| | 242 | |
| | 243 | HelloBean hello = |
| | 244 | (HelloBean) context.getBean("helloBean"); |
| | 245 | System.out.println(hello.getHelloWord()); |
| | 246 | } |
| | 247 | } |
| | 248 | }}} |