Comments (10)
Hi Alexeyr
There are several ways that could possibly help:
- The
@Sources
annotation does support variable expansion from system properties:
class Example {
MyConfig cfg = createMyConfig();
@Sources("file:${mypath}/myconfig.properties");
interface MyConfig extends Config {
}
static MyConfig createMyConfig() {
String myPath = ...;
System.setProperty("mypath", myPath);
return ConfigFactory.create(MyConfig.class);
}
One could also specify -Dmypath=...
to the JVM at the command line, ie. from a batch file.
You can also use ${user.dir}
in the @Sources
annotation, that is contained in the System properties by default when the JVM starts and points to the work dir from which the JVM has been launched, as somebody suggested on StackOverflow as well.
By default, the url specifed with file:
protocol are relative to the workdir (unless they start with a /
char), so if you specify:
@Sources("file:etc/myconfig.properties");
interface MyConfig extends Config { ... }
the path etc/myconfig.properties
will be relative to the path from where you launched the JVM.
So, for instance, if you launch the JVM from the directory /home/luigi/myapp/
this will be resolved as /home/luigi/myapp/etc/myconfig.properties
.
- OWNER supports a feature called 'importing properties', so basically you can load properties as you prefer then wrap into an OWNER Config object:
Properties props = new Properties();
props.load(new FileInputStream(new File("path/to/whatever.properties")));
MyConfig cfg = ConfigFactory.create(MyConfig.class, props);
- On the master branch (that will be released on the next version) it is possible to config objects to implement the
Mutable
interface, that allows the Config object to be altered. If you like I can add some ways to load properties programmatically from an InputStream:
interface MyConfig extends Config, Mutable { ... }
MyConfig cfg = ConfigFactory.create(MyConfig.class);
cfg.load(new FileInputStream(...));
// or
cfg.load(new FileReader(...));
// or
cfg.loadFromXML(new FileInputStream(...));
Those methods will delegate the loading to the internal Properties
object. At the moment those methods are not available, but I think I may add them. What do you think?
Option 3 will be (eventually) available in version 1.0.4 and superior. Option 1 and 2 should already work with released version 1.0.3 (and 1.0.2 too).
Let me know.
from owner.
Yes, option 3 fits what I want quite well and this isn't an immediate requirement.
from owner.
Ok, I will add the load methods. It's very quick to do and it will be included in the next released version. I'll probably implement it today, so you can test with the master branch. I can send you the jar if you need to verify that it does what you need and you don't have time to package it by yourself.
from owner.
And now I thought that it won't play well with hot reload and change events coming in 1.0.4. Unless this could also be easily added?
from owner.
Hot reload is optional (if you don't specify the @HotReload
annotation it won't be enabled). If you decide to load properties manually, you need to implement hot reload by your own if you want it.
The HotReload only works with file:
(or jar:file:
) URLs specified by @Sources
annotation. It's quite inconvenient to monitor for changes in InputStreams
or Reader
objects.
from owner.
I could possibly add something like:
@Sources("file:${mypath}/myconfig.properties"); // notice ${mypath} here
interface MyConfig extends Config { ... }
ConfigFactory.setProperty("mypath", "/foo/bar/baz"); // ${mypath} will be expanded as "/foo/bar/baz"
MyConfig cfg = ConfigFactory.create(MyConfig.class);
from owner.
Yes, this seems to cover cases I can think of.
from owner.
from owner.
Implemented.
See 61605e7 and 0449ecd.
Things to do before closing:
- document in the website or the wiki.
from owner.
Documented here: http://owner.aeonbits.org/docs/configuring/
from owner.
Related Issues (20)
- The annotation @Config.Key is disallowed for this location
- Distinct between actual source of a merged property
- System properties aren't actually checked for changes in reload logic
- Multiple keys HOT 3
- Owner Properties File Location HOT 1
- Unable to ThreadLocal ConfigCache.getOrCreate()
- Accessible.fill() does not decrypt @EncryptedValue properties
- Initializing maps HOT 1
- Converter / Decryptor is not being called when property value is referenced in another property
- Get wrong value which contains '%' HOT 1
- Tests about Reloading failing seemingly randomly
- Fix Vulnerable JUnit - Upgrade JUnit version
- Dependency Dashboard
- System property is @Sources - how to provide a default value? HOT 1
- java.lang.IllegalAccessException when using OWNER with Java 17 HOT 1
- Is it possible, somehow, to have Variables expansion with Accessible.getProperty(String) HOT 1
- Confusing documentation for ByteSizeConverter HOT 1
- Incompatibility between @Spy annotation and java.util.Properties in OpenJDK17
- "Keys to send should be a not null CharSequence" when using Maven
- Documentation site domain appears to have been taken HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from owner.