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方法來進行設定。

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

          分享到:
          主站蜘蛛池模板: 多伦县| 洛阳市| 广饶县| 达孜县| 荆门市| 宣武区| 年辖:市辖区| 牙克石市| 长子县| 大渡口区| 临夏市| 临汾市| 阿拉善左旗| 万山特区| 无锡市| 南丹县| 黄冈市| 泸西县| 沙田区| 那曲县| 田东县| 法库县| 临夏县| 双峰县| 崇文区| 嘉黎县| 四平市| 彭州市| 临西县| 平阴县| 清水县| 临邑县| 吐鲁番市| 兴山县| 内丘县| 珲春市| 嘉荫县| 南乐县| 漯河市| 修水县| 巨野县|