| 40 | |
| 41 | * 建立 Spring 專案 |
| 42 | |
| 43 | {{{ |
| 44 | #!text |
| 45 | file -> new -> project -> Spring => Spring Project : myFirstSpring |
| 46 | }}} |
| 47 | |
| 48 | * 匯入函式庫 |
| 49 | {{{ |
| 50 | #!text |
| 51 | 右鍵點選 myFirstSpring -> properties -> Java Build Path => Libraries => add External Jars |
| 52 | }}} |
| 53 | |
| 54 | 將 spring 資料夾內的jar 檔加入,<<重要>> 並且將 jakara-commons 的 logging.jar 匯入 (可使用hadoop/lib/ 內的 commons-*.jar) |
| 55 | |
| 56 | * 建立佈署檔 beans-config.xml |
| 57 | |
| 58 | {{{ |
| 59 | #!text |
| 60 | 右鍵點選 myFirstSpring -> new -> others -> XML => XML File -> myFirstSpring , File name = beans-config.xml |
| 61 | }}} |
| 62 | |
| 63 | * beans-config.xml |
| 64 | |
| 65 | {{{ |
| 66 | #!xml |
| 67 | <?xml version="1.0" encoding="UTF-8"?> |
| 68 | <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" |
| 69 | "http://www.springframework.org/dtd/spring-beans.dtd"> |
| 70 | <beans> |
| 71 | <bean id="helloBean" |
| 72 | class="onlyfun.caterpillar.HelloBean"> |
| 73 | <property name="helloWord"> |
| 74 | <value>Hello! Waue!</value> |
| 75 | </property> |
| 76 | </bean> |
| 77 | </beans> |
| 78 | }}} |
| 79 | |
| 80 | * src/waue.org/HelloBean.java |
| 81 | |
| 82 | {{{ |
| 83 | #!java |
| 84 | package waue.org; |
| 85 | |
| 86 | public class HelloBean { |
| 87 | private String helloWord; |
| 88 | |
| 89 | public void setHelloWord(String helloWord) { |
| 90 | this.helloWord = helloWord; |
| 91 | } |
| 92 | public String getHelloWord() { |
| 93 | return helloWord; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | }}} |
| 98 | |
| 99 | * src/waue.org/SpringDemo.java |
| 100 | |
| 101 | {{{ |
| 102 | #!java |
| 103 | package waue.org; |
| 104 | |
| 105 | import org.springframework.beans.factory.BeanFactory; |
| 106 | import org.springframework.beans.factory.xml.XmlBeanFactory; |
| 107 | import org.springframework.core.io.FileSystemResource; |
| 108 | import org.springframework.core.io.Resource; |
| 109 | |
| 110 | public class SpringDemo { |
| 111 | public static void main(String[] args) { |
| 112 | Resource rs = |
| 113 | new FileSystemResource("beans-config.xml"); |
| 114 | BeanFactory factory = |
| 115 | new XmlBeanFactory(rs); |
| 116 | |
| 117 | HelloBean hello = |
| 118 | (HelloBean) factory.getBean("helloBean"); |
| 119 | System.out.println(hello.getHelloWord()); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | }}} |
| 124 | |
| 125 | * 看結果 |
| 126 | |
| 127 | run -> "run aspectj/java application" or "run java application" 皆可 |
| 128 | |