1. 程式人生 > >spring xml 檔案的一些寫法,包括map ,set ,list 等

spring xml 檔案的一些寫法,包括map ,set ,list 等

To switch over from the DTD-style to the new XML Schema-style, you need to make the following change.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
    "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>

<!-- <bean/> definitions here -->
</beans>

The equivalent file in the XML Schema

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
>
<!-- <bean/> definitions here --> </beans>

To use the tags in the util schema, you need to have the following preamble at the top of your Spring XML configuration file; the bold text in the snippet below references the correct schema so that the tags in the util namespace are available to you.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

<!-- <bean/> definitions here -->

</beans>

Before…

<bean id="..." class="...">
  <property name="isolation">
    <bean id="java.sql.Connection.TRANSACTION_SERIALIZABLE"
    class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" />
  </property>
</bean>

The above configuration uses a Spring FactoryBean implementation, the FieldRetrievingFactoryBean, to set the value of the ‘isolation’ property on a bean to the value of the ‘java.sql.Connection.TRANSACTION_SERIALIZABLE’ constant. This is all well and good, but it is a tad verbose and (unneccessarily) exposes Spring’s internal plumbing to the end user.

The following XML Schema-based version is more concise and clearly expresses the developer’s intent (‘inject this constant value’), and it just reads better.

<bean id="..." class="...">
  <property name="isolation">
    <util:constant static-field="java.sql.Connection.TRANSACTION_SERIALIZABLE"/>
  </property>
</bean>

Setting a bean property or constructor arg from a field value

FieldRetrievingFactoryBean is a FactoryBean which retrieves a static or non-static field value. It is typically used for retrieving public static final constants, which may then be used to set a property value or constructor arg for another bean.

Find below an example which shows how a static field is exposed, by using the staticField property:

<bean id="myField"
        class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
  <property name="staticField" value="java.sql.Connection.TRANSACTION_SERIALIZABLE"/>
</bean>

There is also a convenience usage form where the static field is specified as the bean name:

<bean id="..." class="...">
  <property name="isolation">
    <bean id="java.sql.Connection.TRANSACTION_SERIALIZABLE"
          class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" />
  </property>
</bean>

It is also possible to access a non-static (instance) field of another bean, as described in the API documentation for the FieldRetrievingFactoryBean class.

Injecting enum values into beans as either property or constructor arguments is very easy to do in Spring, in that you don’t actually have to do anything or know anything about the Spring internals (or even about classes such as the FieldRetrievingFactoryBean). Let’s look at an example to see how easy injecting an enum value is; consider this JDK 5 enum:

package javax.persistence;

public enum PersistenceContextType {

    TRANSACTION,
    EXTENDED

}

Now consider a setter of type PersistenceContextType:

package example;

public class Client {

    private PersistenceContextType persistenceContextType;

    public void setPersistenceContextType(PersistenceContextType type) {
        this.persistenceContextType = type;
    }
}

.. and the corresponding bean definition:

<bean class="example.Client">
    <property name="persistenceContextType" value="TRANSACTION" />
</bean>

This works for classic type-safe emulated enums (on JDK 1.4 and JDK 1.3) as well; Spring will automatically attempt to match the string property value to a constant on the enum class.

Before…

<!-- target bean to be referenced by name -->
<bean id="testBean" class="org.springframework.beans.TestBean" scope="prototype">
  <property name="age" value="10"/>
  <property name="spouse">
    <bean class="org.springframework.beans.TestBean">
      <property name="age" value="11"/>
    </bean>
  </property>
</bean>
<!-- will result in 10, which is the value of property 'age' of bean 'testBean' -->
<bean id="testBean.age" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>

The above configuration uses a Spring FactoryBean implementation, the PropertyPathFactoryBean, to create a bean (of type int) called ‘testBean.age’ that has a value equal to the ‘age’ property of the ‘testBean’ bean.

After…

<!-- target bean to be referenced by name -->
<bean id="testBean" class="org.springframework.beans.TestBean" scope="prototype">
  <property name="age" value="10"/>
  <property name="spouse">
    <bean class="org.springframework.beans.TestBean">
      <property name="age" value="11"/>
    </bean>
  </property>
</bean>

Before…

<!-- creates a java.util.List instance with values loaded from the supplied 'sourceList' -->
<bean id="emails" class="org.springframework.beans.factory.config.ListFactoryBean">
  <property name="sourceList">
      <list>
        <value>[email protected]</value>
        <value>[email protected]</value>
        <value>[email protected]</value>
        <value>[email protected]</value>
      </list>
  </property>
</bean>

The above configuration uses a Spring FactoryBean implementation, the ListFactoryBean, to create a java.util.List instance initialized with values taken from the supplied ‘sourceList’.

After…

<!-- creates a java.util.List instance with the supplied values -->
<util:list id="emails">
    <value>[email protected]</value>
    <value>[email protected]</value>
    <value>[email protected]</value>
    <value>[email protected]</value>
</util:list>

You can also explicitly control the exact type of List that will be instantiated and populated via the use of the ‘list-class’ attribute on the element. For example, if we really need a java.util.LinkedList to be instantiated, we could use the following configuration:

<util:list id="emails" list-class="java.util.LinkedList">
    <value>[email protected]</value>
    <value>[email protected]</value>
    <value>[email protected]</value>
    <value>d'[email protected]</value>
</util:list>

If no ‘list-class’ attribute is supplied, a List implementation will be chosen by the container.

Before…

<!-- creates a java.util.Map instance with values loaded from the supplied 'sourceMap' -->
<bean id="emails" class="org.springframework.beans.factory.config.MapFactoryBean">
  <property name="sourceMap">
      <map>
        <entry key="pechorin" value="[email protected]"/>
        <entry key="raskolnikov" value="[email protected]"/>
        <entry key="stavrogin" value="[email protected]"/>
        <entry key="porfiry" value="[email protected]"/>
      </map>
  </property>
</bean>

The above configuration uses a Spring FactoryBean implementation, the MapFactoryBean, to create a java.util.Map instance initialized with key-value pairs taken from the supplied ‘sourceMap’.

After…

<!-- creates a java.util.Map instance with the supplied key-value pairs -->
<util:map id="emails">
    <entry key="pechorin" value="[email protected]"/>
    <entry key="raskolnikov" value="[email protected]"/>
    <entry key="stavrogin" value="[email protected]"/>
    <entry key="porfiry" value="[email protected]"/>
</util:map>

You can also explicitly control the exact type of Map that will be instantiated and populated via the use of the ‘map-class’ attribute on the element. For example, if we really need a java.util.TreeMap to be instantiated, we could use the following configuration:

<util:map id="emails" map-class="java.util.TreeMap">
    <entry key="pechorin" value="[email protected]"/>
    <entry key="raskolnikov" value="[email protected]"/>
    <entry key="stavrogin" value="[email protected]"/>
    <entry key="porfiry" value="[email protected]"/>
</util:map>

If no ‘map-class’ attribute is supplied, a Map implementation will be chosen by the container.

Before…

<!-- creates a java.util.Set instance with values loaded from the supplied 'sourceSet' -->
<bean id="emails" class="org.springframework.beans.factory.config.SetFactoryBean">
  <property name="sourceSet">
      <set>
        <value>[email protected]</value>
        <value>[email protected]</value>
        <value>[email protected]</value>
        <value>[email protected]</value>
      </set>
  </property>
</bean>

The above configuration uses a Spring FactoryBean implementation, the SetFactoryBean, to create a java.util.Set instance initialized with values taken from the supplied ‘sourceSet’.

After…

<!-- creates a java.util.Set instance with the supplied values -->
<util:set id="emails">
    <value>[email protected]</value>
    <value>[email protected]</value>
    <value>[email protected]</value>
    <value>[email protected]</value>
</util:set>

You can also explicitly control the exact type of Set that will be instantiated and populated via the use of the ‘set-class’ attribute on the element. For example, if we really need a java.util.TreeSet to be instantiated, we could use the following configuration:

<util:set id="emails" set-class="java.util.TreeSet">
    <value>[email protected]</value>
    <value>[email protected]</value>
    <value>[email protected]</value>
    <value>[email protected]</value>
</util:set>

If no ‘set-class’ attribute is supplied, a Set implementation will be chosen by the container.

C.2.3 The jee schema

The jee tags deal with Java EE (Java Enterprise Edition)-related configuration issues, such as looking up a JNDI object and defining EJB references.

To use the tags in the jee schema, you need to have the following preamble at the top of your Spring XML configuration file; the bold text in the following snippet references the correct schema so that the tags in the jee namespace are available to you.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">

<!-- <bean/> definitions here -->

</beans>

C.2.3.1 (simple)

Before…

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="jdbc/MyDataSource"/>
</bean>

<bean id="userDao" class="com.foo.JdbcUserDao">
    <!-- Spring will do the cast automatically (as usual) -->
    <property name="dataSource" ref="dataSource"/>
</bean>
After...

<jee:jndi-lookup id="dataSource" jndi-name="jdbc/MyDataSource"/>

<bean id="userDao" class="com.foo.JdbcUserDao">
    <!-- Spring will do the cast automatically (as usual) -->
    <property name="dataSource" ref="dataSource"/>
</bean>

C.2.3.2 (with single JNDI environment setting)

Before…

<bean id="simple" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="jdbc/MyDataSource"/>
    <property name="jndiEnvironment">
        <props>
            <prop key="foo">bar</prop>
        </props>
    </property>
</bean>

After…

<jee:jndi-lookup id="simple" jndi-name="jdbc/MyDataSource">
    <jee:environment>foo=bar</jee:environment>
</jee:jndi-lookup>

(with multiple JNDI environment settings)

Before…

<bean id="simple" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="jdbc/MyDataSource"/>
    <property name="jndiEnvironment">
        <props>
            <prop key="foo">bar</prop>
            <prop key="ping">pong</prop>
        </props>
    </property>
</bean>

After…

<jee:jndi-lookup id="simple" jndi-name="jdbc/MyDataSource">
    <!-- newline-separated, key-value pairs for the environment (standard Properties format) -->
    <jee:environment>
        foo=bar
        ping=pong
    </jee:environment>
</jee:jndi-lookup>

參考此連結

相關推薦

spring xml 檔案一些寫法包括map set list

To switch over from the DTD-style to the new XML Schema-style, you need to make the following change. <?xml version="1.0" enc

java 日誌 logback-spring.xml 檔案配置

  maven配置: <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <versi

Spring學習(二):Spring xml檔案格式、載入上下文六種方式及作用域

Bean的XML檔案 <?xml version="1.0" encoding="UTF-8"?> <beans <!--標準名稱空間 --> xmlns="http://www.springframework.org/

Spring Xml 檔案的配置 引數 屬性 說明

1、value元素 <value/>元素通過字串來指定屬性或構造器引數的值。 <bean id="myDataSource" detroy-method="close" class="org.apache.commons.dbcp.BasicDataS

java中解析xml檔案的五種常見方法:DOM4JdompullSAXJdom

package com.zhidi.dom4jtest; import java.io.File; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import or

Spring中使用MapSetList、陣列、屬性集合的注入方法配置檔案

(1)下邊的一個java類包含了所有Map、Set、List、陣列、屬性集合等這些容器,主要用於演示Spring的注入配置; package com.lc.collection; import java.util.List; import java.util.Map;

SpringMVC專案中配置xml一些和名稱空間相關的問題如mvc:annotation-driven的字首 "mvc"未繫結

Q1.元素 "mvc:annotation-driven" 的字首 "mvc"未繫結 辦法:在spring-servlet.xml檔案裡使用<mvc>開頭的標籤時,忘記引入了名稱空間。在xml的beans裡面加入如下程式碼即可 xmlns:mvc="http:

Spring xml檔案中dubbo相關標籤不能被識別的問題

1、解決Eclipse的XML驗證錯誤:手動指定XSD檔案 http://blog.csdn.net/tech4j/article/details/46754751 回到Eclipse中右鍵點選配置檔案,選擇Validate,然後紅叉就都消失了嘿!

Java的Excel檔案操作工具類包括讀、寫、合併功能

一、直接上程式碼: package com.cycares.crm.utils.ExcelUtil; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputS

Spring xml檔案配置不提示問題

在eclipse使用過程中,配置Spring的xml檔案的時候,有時候沒有按下ALT+/ 沒有自動提示。是因為沒有匯入約束檔案。寫個部落格,記下。前提:首先確定從官網下載了與Spring相關的壓縮包,解壓之後在我們看到裡面有一個schema資料夾,等會就需要用到這個資料夾。1

spring-data-jpa 介紹 複雜查詢包括多表關聯分頁排序

本篇進行Spring-data-jpa的介紹,幾乎涵蓋該框架的所有方面,在日常的開發當中,基本上能滿足所有需求。這裡不講解JPA和Spring-data-jpa單獨使用,所有的內容都是在和Spring整合的環境中實現。如果需要了解該框架的入門,百度一下,很多入門的介紹。在這篇

Spring中使用MapSetList、數組、屬性集合的註入方法配置文件

查看 main list highlight 配置 spring配置 pla lec while (1)下邊的一個Java類包含了所有Map、Set、List、數組、屬性集合等這些容器,主要用於演示spring的註入配置; [java] view plain c

C# 遍歷所有的子控件和孫控件包括容器中的並批量操作和調用

cnblogs ati tex foreach pri int 遍歷 asc 語句 這裏要用兩個知識,一個是遞歸,一個是隊列。 //定義一個Control類型的隊列allCtrls private static Queue <Control> allCtrls

My97DatePicker設置包括隱藏 清空設置最大日期 轉載

快速 高亮 hang 特殊 com 不可用 學會 日期選擇框 pick My97DatePicker是一款非常靈活好用的日期控件。使用非常簡單。 1、下載My97DatePicker組件包 2、在頁面中引入該組件js文件: <script type="te

Qt編寫的大資料的運算包括基礎的四則運算階乘

編寫背景:在平常的程式設計中我們會遇到很多的大資料,這些資料的值超出了整型int的最大範圍;甚至超過了long long的資料範圍;遇到這種情況,我們的資料處理就會變的十分的麻煩。因為我們不可以使用整型變數來表示這些值當然也就不可使用四則運算來,就算這些值的加減

使用java.lang.instrument實現第三方jar包內類的修改包括引入外部依賴引數獲取

最近專案開發需求中,使用了第三方供應商提供的jar包形式的sdk ,sdk中的日誌由其自己管理列印,現在想獲取到日誌列印時傳入的message,就必須想辦法對sdk的原始碼進行改動。 首先想到的是反編譯jar包,然後修改後重新打包,嘗試了一下後感覺很麻煩,而且很不cool。

武漢大學GNSS中心給IGS提供的資料產品下載地址包括精密GPS軌道鐘差EOP以及實時軌道和鐘差

中心對外提供資料服務 中心igs伺服器 地址:ftp://ics.gnsslab.cn PANDA軟體tables目錄,每天更新;  包含我們自己的精密軌道和衛星鐘差:whu目錄;  igs資料和產品;  IERS資料和產品。 IGS實時資料流 c

IIC詳解包括原理、過程最後一步步教你實現IIC

IIC詳解 1、I2C匯流排具有兩根雙向訊號線,一根是資料線SDA,另一根是時鐘線SCL   2、IIC總線上可以掛很多裝置:多個主裝置,多個從裝置(外圍 裝置)。上圖中主裝置是兩個微控制器,剩下的都是從裝置。  3、多主機會產生匯流排裁決問題。當多個主機同時想佔用匯

如何通過JS在html網頁上進行新增元素包括div 以及下拉框

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  <html