Skip to main content

Injecting Configuration

The following example uses a properties file:

jdbcSettings.jdbcDriver = com.mysql.jdbc.Driver
jdbcSettings.jdbcURL = jdbc:mysql://127.0.0.1:3306/test
jdbcSettings.userName = sa
jdbcSettings.userPassword =

The same configuration can also be expressed in XML:

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://www.hasor.net/sechma/main">
<jdbcSettings>
<jdbcDriver>com.mysql.jdbc.Driver</jdbcDriver>
<jdbcURL>jdbc:mysql://127.0.0.1:3306/test</jdbcURL>
<userName>sa</userName>
<userPassword></userPassword>
</jdbcSettings>
</config>

In a bean, use the net.hasor.core.InjectSettings annotation to indicate that the injected value comes from the configuration file.

Example
public class DataBaseBean {
@InjectSettings("jdbcSettings.jdbcDriver")
private String jdbcDriver;

@InjectSettings("jdbcSettings.jdbcURL")
private String jdbcURL;

@InjectSettings("jdbcSettings.userName")
private String user;

@InjectSettings("jdbcSettings.userPassword")
private String password;

...
}

Finally, specify the configuration file to load when creating the container.

AppContext appContext = Hasor.create().mainSettingWith("<config-file-name>").build();

Automatic Type Conversion

@InjectSettings can perform simple type conversion. The conversion utility is net.hasor.cobble.convert.ConverterUtils.

public class TestBean {
@InjectSettings("userInfo.myAge")
private int myAge;
}