天天看點

bboss ioc配置檔案中使用外部屬性檔案介紹

bboss ioc配置檔案中使用外部屬性檔案介紹

與spring ioc一樣,在bboss ioc中也可以非常友善地引用外部屬性檔案(5.0.1及後續版本),本文介紹使用方法。

在工程中引入bboss ioc:

maven坐标:

<dependency>
    <groupId>com.bbossgroups</groupId>
    <artifactId>bboss-core</artifactId>
    <version>5.0.3.5</version>
</dependency>      

gradle坐标:

compile group: 'com.bbossgroups', name: 'bboss-core', version: '5.0.3.5'

運作測試用例junit gradle坐标:

testCompile group: 'junit', name: 'junit', version: '4.+'

下載下傳本文示範gradle工程:

下載下傳

參考文檔将gradle工程導入eclipse:

bboss gradle工程導入eclipse介紹

定義和導入外部屬性檔案

屬性檔案必須包含在classpath環境中

例如:

bboss ioc配置檔案中使用外部屬性檔案介紹

可以定義多個屬性檔案

檔案定義好後需要在ioc配置檔案的最開始通過config元素導入,如果有多個配置檔案,可以在ioc根檔案中導入屬性檔案(可以同時導入多個):

<config file="org/frameworkset/spi/variable/ioc-var.properties"/>

<config file="org/frameworkset/spi/variable/ioc-var1.properties"/>

<config file="file:F:/workspace/bboss/bboss-core/test/org/frameworkset/spi/variable/ioc-var.properties"/>

通過file:字首指定實體路徑,預設是classpath目錄下的路徑

屬性檔案内容:

varValue1=hello varValue1!
varValue2=hello varValue2!      

使用外部屬性檔案

導入後就可以在注入的屬性、擴充屬性中引用屬性檔案中定義的變量:

引用變量文法:${xxxxx}

指定預設值文法:${varValue2:99}

在依賴注入的屬性值中引用外部屬性完整的示例

<properties>
	<config file="org/frameworkset/spi/variable/ioc-var.properties"/>
	<config file="org/frameworkset/spi/variable/ioc-var1.properties"/>
	<property name="test.beans"
	    f:varValue="aaa${varValue}aaa" 
	    f:intValue="2"
		long="1" int="1" boolean="true" string="${varValue1}string" object="object"
		class="org.frameworkset.spi.variable.VariableBean">
		<construction>
			<property ><![CDATA[${varValue1}ccc]]></property>
			<property value="ddd${varValue2}"/>
		</construction>
		<property name="varValue1" ><![CDATA[${varValue1}ccc]]></property>
		<property name="varValue2" value="ddd${varValue2:99}"/>
	</property>
	
	 
</properties>      

擷取使用了外部屬性檔案的元件執行個體:

@Test
	public void test()
	{
		BaseApplicationContext context = DefaultApplicationContext.getApplicationContext("org/frameworkset/spi/variable/ioc-var.xml");//定義一個ioc容器
		VariableBean variableBean = context.getTBeanObject("test.beans", VariableBean.class);//擷取元件執行個體
		System.out.println(variableBean.getExteral("string"));//擷取元件中配置的擴充屬性string
	}      

VariableBean類源碼:

package org.frameworkset.spi.variable;

import org.frameworkset.spi.BeanInfoAware;

public class VariableBean extends BeanInfoAware{
	private String varValue;
	private String varValue1;
	private String varValue2;
	private int intValue;
	public VariableBean(String varValue1,String varValue2)
	{
		System.out.println("varValue1:"+varValue1);
		System.out.println("varValue2:"+varValue2);
	}
	
	public String getExteral(String attr)
	{
		return super.beaninfo.getStringExtendAttribute(attr);
	}

}
      

通過ioc容器直接擷取外部屬性api方法執行個體

BaseApplicationContext context = DefaultApplicationContext.getApplicationContext("org/frameworkset/spi/variable/parent-var.xml");
		System.out.println(context.getExternalProperty("varValue"));
		System.out.println(context.getExternalProperty("varValue1"));
		System.out.println(context.getExternalProperty("varValue2"));      

外部屬性有效範圍

1.根容器配置檔案中導入的外部屬性檔案中的屬性值對根檔案中導入(managerimport)子檔案可見

2.子檔案中導入的外部屬性檔案中的屬性隻對本身及其下級子檔案可見,以此類推

3.mvc容器對應的根檔案是bboss-mvc.xml檔案,在其中引入的外部屬性配置檔案對所有其他mvc配置檔案可見,其他mvc配置檔案導入的外部屬性檔案隻對本身及其下級子檔案可見

繼續閱讀