Kela's Blog

                      前面的路很坎坷,但畢竟是條路.也許走過這一段就會發現,走過去就是夢想中的地方.因此堅持成為此刻唯一能做且必須去做的事情.
          posts - 9, comments - 27, trackbacks - 0, articles - 15

          kela的筆記 應用程序框架 ---- spring(2)

          Posted on 2006-08-22 09:36 Kela 閱讀(258) 評論(0)  編輯  收藏 所屬分類: 我的筆記(Spring)

          摘要: spring 兩種常用的注入方式( Type 2 Ioc , Type 3 Ioc

          ?

          ???? HelloBean.java

          ?

          package com.kela.spring.ioc;

          ?

          public class HelloBean {

          ???

          ??? private String name;

          ??? private String helloWord;

          ???

          ??? public HelloBean() {

          ??? }

          ???

          ??? public HelloBean(String name, String helloWord) {

          ??????? this.name = name;

          ??????? this.helloWord = helloWord;

          ??? }

          ???

          ??? public String getHelloWord() {

          ??????? return helloWord;

          ??? }

          ??? public void setHelloWord(String helloWord) {

          ??????? this.helloWord = helloWord;

          ??? }

          ??? public String getName() {

          ??????? return name;

          ??? }

          ??? public void setName(String name) {

          ??????? this.name = name;

          ??? }

          }

          該程序文件中講兩種常用的注入方式寫在了一起。

          ???? Beans-config_1.xml

          ?

          <?xml version= "1.0" encoding= "GB2312" ?>

          <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >

          ?

          <beans>

          ???

          ??? <!-- Type 3 Injection 通過構造方法(這里注意構造方法中參數的順序保持一致) -->

          ??? <bean id= "helloBean_1" class= "com.kela.spring.ioc.HelloBean" >

          ?????? <constructor-arg index= "0" >

          ?????????? <value> KangFeng </value>

          ?????? </constructor-arg>

          ?????? <constructor-arg index= "1" >

          ?????????? <value> 你好! </value>

          ?????? </constructor-arg>

          ??? </bean>

          ???

          ??? <!-- Type2 Injection 通過 set 注入法 -->

          ??? <bean id= "helloBean_2" class= "com.kela.spring.ioc.HelloBean" >

          ?????? <property name= "name" >

          ?????????? <value> Kela </value>

          ?????? </property>

          ?????? <property name= "helloWord" >

          ?????????? <value> hello ! </value>

          ?????? </property>

          ??? </bean>

          </beans>

          ???? TestClass.java

          ?

          package com.kela.spring.ioc;

          ?

          import org.springframework.context.ApplicationContext;

          import org.springframework.context.support.FileSystemXmlApplicationContext;

          ?

          public class TestClass {

          ?

          ??? public void method_1() {

          ??????? try {

          ??????????? ApplicationContext context = new

          ?????????????????????????????????????? FileSystemXmlApplicationContext("bin\\com\\kela\\spring\\ioc\\beans-config_1.xml");

          ??????????? HelloBean helloBean_1 = (HelloBean)context.getBean("helloBean_1");

          ??????????? System.out.println(" 構造方法注入(歡迎詞):" + helloBean_1.getName() + ";" + helloBean_1.getHelloWord());

          ???????????

          ??????????? HelloBean helloBean_2 = (HelloBean)context.getBean("helloBean_2");

          ??????????? System.out.println("set 方法注入(歡迎詞):" + helloBean_2.getName() + ";" + helloBean_2.getHelloWord());

          ??????? } catch (Exception e) {

          ??????????? System.out.println("[ERROR]" + e.getMessage());

          ??????? }

          ??? }

          ???

          ??? public static void main(String[] args) {

          ??????? TestClass testClass = new TestClass();

          ??????? testClass.method_1();

          ??? }

          }

          ???? 學習小結

          ?

          關于ConstructorSetter注入的區別其實就是說,是要在對象建時是就準備好資源還是在對象建立好之后,再使用Setter方法來進行設定。

          從實際使用角度來看,一個適用于較短的屬性列,一個適用于較長的屬性列。

          分享到:
          主站蜘蛛池模板: 满城县| 石泉县| 上高县| 阿图什市| 嵩明县| 金阳县| 娄底市| 昌江| 蓬溪县| 偏关县| 临武县| 兴海县| 拜泉县| 个旧市| 屏东县| 万全县| 正蓝旗| 西林县| 偃师市| 新龙县| 读书| 东光县| 海晏县| 河池市| 鹤庆县| 得荣县| 大丰市| 丰宁| 东源县| 潢川县| 积石山| 百色市| 方山县| 灵川县| 恭城| 瓦房店市| 道真| 乌恰县| 通河县| 钟祥市| 马尔康县|