程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Spring Bean根本治理實例詳解

Spring Bean根本治理實例詳解

編輯:關於JAVA

Spring Bean根本治理實例詳解。本站提示廣大學習愛好者:(Spring Bean根本治理實例詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是Spring Bean根本治理實例詳解正文


本文實例講述了Spring Bean根本治理。分享給年夜家供年夜家參考,詳細以下:

1、應用setter方法完成依附注入

上面是Bean和beans-config.xml文件。

public class HelloBean { 
  private String helloWord; 
  //...省略getter、setter辦法   
}

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" 
"http://www.springframework.org/dtd/spring-beans.dtd"> 
<beans> 
  <bean id="helloBean" 
     class="onlyfun.caterpillar.HelloBean"> 
    <property name="helloWord"> 
      <value>Hello!Justin!</value> 
    </property> 
  </bean> 
</beans>
public class SpringDemo { 
  public static void main(String[] args) { 
    Resource rs = new FileSystemResource("beans-config.xml"); 
    BeanFactory factory = new XmlBeanFactory(rs); 
    HelloBean hello = (HelloBean) factory.getBean("helloBean"); 
    System.out.println(hello.getHelloWord()); 
  } 
}

2、應用constructor方法完成注入

public class HelloBean { 
  private String name; 
  private String helloWord; 
  // 建議有要無參數建構辦法 
  public HelloBean() { 
  } 
  public HelloBean(String name, String helloWord) { 
    this.name = name; 
    this.helloWord = helloWord; 
  } 
  //...省略getter、setter辦法   
}

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" 
"http://www.springframework.org/dtd/spring-beans.dtd"> 
<beans> 
  <bean id="helloBean" 
     class="onlyfun.caterpillar.HelloBean"> 
    <constructor-arg index="0"> 
      <value>Justin</value> 
    </constructor-arg> 
    <constructor-arg index="1"> 
      <value>Hello</value> 
    </constructor-arg> 
  </bean> 
</beans>

public class SpringDemo { 
  public static void main(String[] args) { 
    ApplicationContext context = 
      new FileSystemXmlApplicationContext("beans-config.xml"); 
    HelloBean hello = (HelloBean) context.getBean("helloBean"); 
    System.out.print("Name: "); 
    System.out.println(hello.getName()); 
    System.out.print("Word: "); 
    System.out.println(hello.getHelloWord()); 
  } 
}

3、屬性參考

public class HelloBean { 
  private String helloWord; 
  private Date date; 
  //...省略getter、setter辦法   
}

<beans> 
  <bean id="dateBean" class="java.util.Date"/> 
  <bean id="helloBean" class="onlyfun.caterpillar.HelloBean"> 
    <property name="helloWord"> 
      <value>Hello!</value> 
    </property> 
    <property name="date"> 
      <ref bean="dateBean"/> 
    </property> 
  </bean> 
</beans>
public class SpringDemo { 
  public static void main(String[] args) { 
    ApplicationContext context = 
      new FileSystemXmlApplicationContext("beans-config.xml"); 
    HelloBean hello = (HelloBean) context.getBean("helloBean"); 
    System.out.print(hello.getHelloWord()); 
    System.out.print(" It's "); 
    System.out.print(hello.getDate()); 
    System.out.println("."); 
  } 
}

4、“byType”主動綁定

將“三”中的設置裝備擺設文件改成上面,便可完成bean屬性的按類型主動綁定。

<beans> 
  <bean id="dateBean" class="java.util.Date"/> 
  <bean id="helloBean" class="onlyfun.caterpillar.HelloBean" autowire="byType"> 
    <property name="helloWord"> 
      <value>Hello!</value> 
    </property> 
  </bean> 
</beans>

5、“byName”主動綁定

將“三”中的設置裝備擺設文件改成上面,便可完成bean屬性的按稱號主動綁定。

<beans> 
  <bean id="dateBean" class="java.util.Date"/> 
  <bean id="helloBean" class="onlyfun.caterpillar.HelloBean" autowire="byName"> 
    <property name="helloWord"> 
      <value>Hello!</value> 
    </property> 
  </bean> 
</beans>

6、“constructor”主動綁定

將“三”中的設置裝備擺設文件改成上面,便可完成bean屬性的按結構辦法主動綁定。在樹立依附關系時,Srping容器會試圖比對容器中的Bean實例類型,及相干的結構辦法上的參數類型,看看在類型上能否相符,假如有的話,則選用該結構辦法來樹立Bean實例。假如沒法綁定,則拋出org.springframework.beans.factory.UnsatisfiedDependencyException異常。

<beans> 
  <bean id="dateBean" class="java.util.Date"/> 
  <bean id="helloBean" class="onlyfun.caterpillar.HelloBean" autowire="constructor"> 
    <property name="helloWord"> 
      <value>Hello!</value> 
    </property> 
  </bean> 
</beans>

6、“autodetect”主動綁定

將“三”中的設置裝備擺設文件改成上面,便可完成bean屬性的主動綁定,這個主動綁定是Spring會測驗考試用入constructor來處置依附關系的樹立,假如不可,則再測驗考試用byType類樹立依附關系。

<beans> 
  <bean id="dateBean" class="java.util.Date"/> 
  <bean id="helloBean" class="onlyfun.caterpillar.HelloBean" autowire="autodetect"> 
    <property name="helloWord"> 
      <value>Hello!</value> 
    </property> 
  </bean> 
</beans>

7、依附檢討方法

在主動綁定中,因為沒方法從界說文件中,清晰地看到能否每一個屬性都完成設定,為了肯定某些依附關系確切樹立,您可以假設依附檢討,在<bean>標簽應用時設定"dependency-check",可以有四種依附檢討方法:simple、objects、all、none。

simple:只檢討簡略的類型(像原生數據類型或字符串對象)屬性能否完成依附關系,。
objects:檢討對象類型的屬性能否完成依附關系。
all:則檢討全體的屬性能否完成依附關系。
none:設定是默許值,表現不檢討依附性。

<beans> 
  <bean id="dateBean" class="java.util.Date"/> 
  <bean id="helloBean" class="onlyfun.caterpillar.HelloBean" autowire="autodetect" dependeny-check="all"> 
    <property name="helloWord"> 
      <value>Hello!</value> 
    </property> 
  </bean> 
</beans>

8、聚集對象注入

關於像數組、List、Set、Map等聚集對象,在注入前必需填充一些對象至聚集中,然後再將聚集對象注入至所需的Bean時,也能夠交由Spring的IoC容器來主動保護或生成聚集對象,並完成依附注入。

public class SomeBean { 
  private String[] someStrArray; 
  private Some[] someObjArray; 
  private List someList; 
  private Map someMap; 
  public String[] getSomeStrArray() { 
    return someStrArray; 
  } 
  public void setSomeStrArray(String[] someStrArray) { 
    this.someStrArray = someStrArray; 
  } 
  public Some[] getSomeObjArray() { 
    return someObjArray; 
  } 
  public void setSomeObjArray(Some[] someObjArray) { 
    this.someObjArray = someObjArray; 
  } 
  public List getSomeList() { 
    return someList; 
  } 
  public void setSomeList(List someList) { 
    this.someList = someList; 
  } 
  public Map getSomeMap() { 
    return someMap; 
  } 
  public void setSomeMap(Map someMap) { 
    this.someMap = someMap; 
  } 
}
public class Some { 
  private String name; 
  public String getName() { 
    return name; 
  } 
  public void setName(String name) { 
    this.name = name; 
  } 
  public String toString() { 
    return name; 
  } 
}

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" 
"http://www.springframework.org/dtd/spring-beans.dtd"> 
<beans> 
  <bean id="some1" class="onlyfun.caterpillar.Some"> 
    <property name="name"> 
      <value>Justin</value> 
    </property> 
  </bean> 
  <bean id="some2" class="onlyfun.caterpillar.Some"> 
    <property name="name"> 
      <value>momor</value> 
    </property> 
  </bean> 
  <bean id="someBean" class="onlyfun.caterpillar.SomeBean"> 
    <property name="someStrArray"> 
      <list> 
        <value>Hello</value> 
        <value>Welcome</value> 
      </list> 
    </property> 
    <property name="someObjArray"> 
      <list> 
         <ref bean="some1"/> 
         <ref bean="some2"/> 
      </list> 
    </property> 
    <property name="someList"> 
      <list> 
         <value>ListTest</value> 
         <ref bean="some1"/> 
         <ref bean="some2"/> 
      </list> 
    </property> 
    <property name="someMap"> 
      <map> 
         <entry key="MapTest"> 
           <value>Hello!Justin!</value> 
         </entry> 
         <entry key="someKey1"> 
           <ref bean="some1"/> 
         </entry> 
      </map> 
    </property> 
  </bean> 
</beans>

public class SpringDemo { 
  public static void main(String[] args) { 
    ApplicationContext context = 
      new FileSystemXmlApplicationContext( 
          "beans-config.xml"); 
    SomeBean someBean = 
      (SomeBean) context.getBean("someBean"); 
    // 獲得數組型態依附注入對象 
    String[] strs = 
      (String[]) someBean.getSomeStrArray(); 
    Some[] somes = 
      (Some[]) someBean.getSomeObjArray(); 
    for(int i = 0; i < strs.length; i++) { 
      System.out.println(strs[i] + "," 
          + somes[i].getName()); 
    } 
    // 獲得List型態依附注入對象 
    System.out.println(); 
    List someList = (List) someBean.getSomeList(); 
    for(int i = 0; i < someList.size(); i++) { 
      System.out.println(someList.get(i)); 
    } 
    // 獲得Map型態依附注入對象 
    System.out.println(); 
    Map someMap = (Map) someBean.getSomeMap(); 
    System.out.println(someMap.get("MapTest")); 
    System.out.println(someMap.get("someKey1")); 
  } 
}

願望本文所述對年夜家Java法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved