日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

spring配置jndi_在Spring Framework中通过JNDI进行配置

發布時間:2023/12/3 javascript 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring配置jndi_在Spring Framework中通过JNDI进行配置 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

spring配置jndi

從某個時候開始,應用程序必須是可配置的。 從第一個版本0.9開始,Spring Framework就為該問題提供了一個很好的輔助工具,該類為PropertyPlaceholderConfigurer類,而從Spring Framework 3.1開始,為PropertySourcesPlaceholderConfigurer類。 在Google上搜索PropertyPlaceholderConfigurer時,您會發現許多示例,這些示例中的配置項保存在屬性文件中。 但是在許多Java企業應用程序中,通常通過JNDI查找加載配置項。 我想演示PropertyPlaceholderConfigurer (在Spring Framework 3.1之前)以及相應的PropertySourcesPlaceholderConfigurer (從Spring Framework 3.1開始)如何幫助簡化在我們的應用程序中通過JNDI查找的配置。

初始情況

我們有一個與數據庫連接的Web應用程序。 該數據庫連接必須是可配置的。 配置項目在Web應用程序上下文文件中定義。

context.xml

<Context docBase="/opt/tomcat/warfiles/jndi-sample-war.war" antiResourceLocking="true"><Environment name="username" value="demo" type="java.lang.String" override="false"/><Environment name="password" value="demo" type="java.lang.String" override="false"/>url" value="jdbc:mysql://localhost:3306/wicket_demo" type="java.lang.String" override="false"/> </Context>

為了加載這些配置項,使用了JNDI查找機制。

在我們的應用程序中,我們在Spring上下文XML文件中定義了一個數據源bean。 該bean表示數據庫連接。

<?xml version="1.0" encoding="UTF-8"?> xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">url" value="${url}" /><property name="username" value="${username}" /><property name="password" value="${password}" /><!--<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->bean> </beans>

啟動應用程序時,應將每個以$ {}開頭和結尾的值替換為PropertyPlaceholderConfigurer,并相應地使用PropertySourcesPlaceholderConfigurer 。 下一步是設置PropertyPlaceholderConfigurer,并相應地設置PropertySourcesPlaceholderConfigurer。

在Spring Framework 3.1之前–為JNDI查找設置

我們在Spring上下文XML文件中定義了PropertyPlaceholderConfigurer bean。 此bean包含一個內部bean,該內部bean將數據源bean的屬性名稱映射到相應的JNDI名稱。 JNDI名稱由兩部分組成。 第一部分是資源所在的上下文的名稱(在我們的示例中為java:comp / env / ),第二部分是資源的名稱(在我們的示例中為用戶名,密碼或url)。

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="properties"><bean class="java.util.Properties"><constructor-arg><map><entry key="username"><jee:jndi-lookup jndi-name="java:comp/env/username" /></entry><entry key="password"><jee:jndi-lookup jndi-name="java:comp/env/password" /></entry><entry key="url"><jee:jndi-lookup jndi-name="java:comp/env/url" /></entry></map></constructor-arg></bean></property> </bean>

從Spring Framework 3.1開始–為JNDI查找設置

既然Spring 3.1 PropertySourcesPlaceholderConfigurer應該被用來替代PropertyPlaceholderConfigurer。 這會影響從Spring 3.1開始,<context:property-placeholder />命名空間元素將注冊PropertySourcesPlaceholderConfigurer的實例(命名空間定義必須為spring-context-3.1.xsd),而不是PropertyPlaceholderConfigurer (使用名稱空間定義spring-context-3.0.xsd)。 因此,當您遵守某些約定時(基于約定優于配置的原則) ,我們的Spring XML上下文配置非常短。

<context:property-placeholder/>

默認行為是PropertySourcesPlaceholderConfigurer遍歷一組PropertySource以收集所有屬性值。 在基于Spring的Web應用程序中,此集合默認包含JndiPropertySource 。 默認情況下, JndiPropertySource會在前綴為java:comp / env的 JNDI資源名稱之后進行查找。 這意味著,如果您的屬性為$ {url} ,則相應的JNDI資源名稱必須為java:comp / env / url 。

  • 該示例Web應用程序的源代碼托管在GitHub上 。

翻譯自: https://www.javacodegeeks.com/2015/05/configuration-over-jndi-in-spring-framework.html

spring配置jndi

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的spring配置jndi_在Spring Framework中通过JNDI进行配置的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。