Introduction to using external property files in bboss ioc configuration files

Introduction to the use of external property files in bboss ioc configuration files

Like spring ioc, external property files (5.0.1 and later versions) can also be easily referenced in bboss ioc. This article describes how to use them.
Introduce bboss ioc into the project:
maven coordinates:

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


gradle coordinates:

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

run test case junit gradle coordinates:
testCompile group: 'junit', name: 'junit', version: ' 4.+'

Download this demo gradle project: Download

the reference document and import the gradle project into eclipse: Introduction of bboss gradle project importing into eclipse

Definition and import of external property files
Property files must be included in the classpath environment

For example :



multiple property files can be defined

File defined After that, you need to import through the config element at the beginning of the ioc configuration file. If there are multiple configuration files, you can import the attribute file in the ioc root file (you can import multiple at the same time):

<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"/>

Specify the physical path through the file: prefix, the default is the path in the classpath directory
Properties file content:
varValue1=hello varValue1!
varValue2=hello varValue2!




After importing using an external property file
, you can refer to the variable defined in the property file in the injected property and extended property: reference variable syntax: ${xxxxx}
Specify default value syntax: ${varValue2:99}

in the property value of dependency injection Full example of referencing external properties

<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>


Get a component instance using an external properties file:
@Test
	public void test()
	{
		BaseApplicationContext context = DefaultApplicationContext.getApplicationContext("org/frameworkset/spi/variable/ioc-var.xml");//Define an ioc container
		VariableBean variableBean = context.getTBeanObject("test.beans", VariableBean.class);//Get the component instance
		System.out.println(variableBean.getExteral("string"));//Get the extended attribute string configured in the component
	}


VariableBean class source code:
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);
	}

}


Directly obtain external property api method instance through ioc container
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"));


Valid range of external attributes

1. The attribute values ​​in the external attribute file imported in the root container configuration file are visible
to the sub-file imported in the root file (manager import) 2. The attributes in the external attribute file imported in the sub-file are only for itself and its subordinatessub-files are visible, and so on
. 3. The root file corresponding to the mvc container is the bboss-mvc.xml file. The external attribute configuration file introduced in it is visible to all other mvc configuration files, and the external attribute files imported by other mvc configuration files are only for itself and its subordinate sub-files are visible


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327040007&siteId=291194637