Property frameworks in Java applications

I used spring for a while as my IOC. It also has a very good way to inject properties into your beans.

Now I am participating in a new project where IOC Guice . I do not fully understand the concept of how I can embed properties in my beans using Guice.

Question: is it really possible to embed the original properties (strings, ints) in my guice beans. If the answer is no, then perhaps you know some good Framework properties for java. Because right now I wanted to use the ResourceBundle class to easily manage properties in my application. But after using spring for a while, it just doesn't seem serious enough to me.

+1
java spring guice
Apr 12 '10 at
source share
3 answers

The injection properties in Guice are simple. After reading some properties from a file or, nevertheless, you bind them using Names.bindProperties (Binder, Properties) . You can then enter them using, for example, @Named("some.port") int port .

+1
Apr 12 '10 at 13:20
source share

this SO post discusses the use of various configuration frameworks as well as the use of properties. I'm not sure if this is exactly for your needs, but maybe you can find something valuable there.

+2
Apr 12 2018-10-12T00:
source share

Spring provides an embedding of configuration information found in XML files. I don’t want people installing my software to edit XML files, so to get configuration information more correctly in a text file (for example, path information), I returned to using java.util.Properties since it is simple in use and Spring is pretty good if you use ClassPathResource, which allows the location of the file to be access-free (it just needs to be in the class path, I put mine in the root of WEB-INF / classes.

Here is a quick method that returns a populated Properties object:

 /** * Load the Properties based on the property file specified * by <tt>filename</tt>, which must exist on the classpath * (eg, "myapp-config.properties"). */ public Properties loadPropertiesFromClassPath( String filename ) throws IOException { Properties properties = new Properties(); if ( filename != null ) { Resource rsrc = new ClassPathResource(filename); log.info("loading properties from filename " + rsrc.getFilename() ); InputStream in = rsrc.getInputStream(); log.info( properties.size() + " properties prior to load" ); properties.load(in); log.info( properties.size() + " properties after load" ); } return properties; } 

The file itself uses the usual format "name = value", but if you want to use the XML format "Properties", simply change the property .load (InputStream) to properties.loadFromXML (InputStream). Hope this helps.

+2
Apr 13 '10 at 11:37
source share



All Articles