spring - @Value annotations inside my Java class don't load values from .properties file -


before asking question tried follow following questions similar:

injecting properties using spring & annotation @value

how can inject property value spring bean configured using annotations?

loading properties file class in spring

however, in case not using web applications or tomcat; i'm trying load cluster.properties file regular java project via spring can ingest dummy data accumulo. also, i'm trying load properties cluster.properties file, not key value pairs defined in xml file.

using learned links above , lots of reading on spring, here's have:

i created following context.xml file:

    <?xml version="1.0" encoding="utf-8"?>     <beans xmlns="http://www.springframework.org/schema/beans"     xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"     xsi:schemalocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">        <!-- define spring bean load our cluster properties -->       <bean id="props" class="accumuloingest.loadproperties"></bean>      </beans>   

and here small snippet of cluster.properties file looks like:

    cluster.instance=instance     cluster.username=user      etc... 

next, created following spring main method under class mainapp.java:

    package accumuloingest;      import org.springframework.context.applicationcontext;     import org.springframework.context.support.classpathxmlapplicationcontext;      public class mainapp {          // spring main method used load cluster.properties file spring framework         public static void main(string[] args) {             applicationcontext ctx = new classpathxmlapplicationcontext("context.xml");             loadproperties myobj = loadproperties.class.cast(ctx.getbean("props"));              // print out cluster.properties loaded spring verify aren't null             stringbuffer springpropsbuffer = new stringbuffer();             springpropsbuffer.append("printing out cluster.properties read via spring...");             springpropsbuffer.append("\n\n");             springpropsbuffer.append("instancename= ");             springpropsbuffer.append(myobj.getinstancename());             springpropsbuffer.append("\n");             springpropsbuffer.append("username= ");             springpropsbuffer.append(myobj.getusername());             springpropsbuffer.append("\n");             springpropsbuffer.append("password= ");             springpropsbuffer.append(myobj.getpassword());             springpropsbuffer.append("\n");             springpropsbuffer.append("zooservers= ");             springpropsbuffer.append(myobj.getzooservers());             springpropsbuffer.append("\n");             springpropsbuffer.append("tablename= ");             springpropsbuffer.append(myobj.gettablename());             springpropsbuffer.append("\n");             springpropsbuffer.append("datafile= ");             springpropsbuffer.append(myobj.getdatafile());             springpropsbuffer.append("\n");             springpropsbuffer.append("datadelim= ");             springpropsbuffer.append(myobj.getdatadelim());             springpropsbuffer.append("\n");             springpropsbuffer.append("rowcount= ");             springpropsbuffer.append(myobj.getrowcount());               springpropsbuffer.append("\n");             system.out.println(springpropsbuffer.tostring());              // start data ingest             myobj.startingest(); // method calls ingester class start data ingest         } // end of main method      } // end of mainapp class 

spring loads context.xml file , loads bean called "props", values still null. seems @value annotations aren't working in loadproperties class:

    package accumuloingest;     import java.io.ioexception;      import org.apache.accumulo.core.client.accumuloexception;     import org.apache.accumulo.core.client.accumulosecurityexception;     import org.apache.accumulo.core.client.tableexistsexception;     import org.apache.accumulo.core.client.tablenotfoundexception;     import org.springframework.beans.factory.annotation.value;     import org.springframework.beans.factory.config.propertyplaceholderconfigurer;     import org.springframework.context.annotation.bean;     import org.springframework.core.io.classpathresource;     import org.springframework.core.io.resource;      public class loadproperties {          // class defines spring bean , loads cluster properties         // using springframework          @bean         public static propertyplaceholderconfigurer props(){         propertyplaceholderconfigurer ppc = new propertyplaceholderconfigurer();         resource[] resource = new classpathresource[ ]                 { new classpathresource("/eclipseprojectname/src/cluster.properties") };         ppc.setlocations(resource);         ppc.setignoreunresolvableplaceholders(true);         return ppc; }  // load properties cluster.properties using spring framework private @value("${cluster.instance}") string instancename; private @value("${cluster.username}") string username; private @value("${cluster.password}") string password; private @value("${cluster.zooservers}") string zooservers; private @value("${cluster.tablename}") string tablename; private @value("${cluster.datafile}") string datafile; private @value("${cluster.datadelimiter}") string datadelim; private @value("${cluster.rowcount}") int rowcount;  // getters other java classes access properties loaded spring public string getinstancename() {     return instancename; } public string getusername() {     return username; } public string getpassword() {     return password; } public string getzooservers() {     return zooservers; } public string gettablename() {     return tablename; } public string getdatafile() {     return datafile; } public string getdatadelim() {     return datadelim; } public int getrowcount() {     return rowcount; }          // method kick off ingest of dummy data         void startingest() {             ingester ingestobject = new ingester();             try {                 ingestobject.ingestdata();             } catch (ioexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             } catch (tablenotfoundexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             } catch (tableexistsexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             } catch (accumuloexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             } catch (accumulosecurityexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             } // end of try-catch block         } // end of startingest method      } // end of loadproperties class 

yet when run mainapp.java in eclipse values null when ingester.java class calls getters.

here's console output when run mainapp.java in eclipse:

    13/09/24 14:08:24 info support.classpathxmlapplicationcontext: refreshing org.springframework.context.support.classpathxmlapplicationcontext@191f667c: startup date [tue sep 24 14:08:24 edt 2013]; root of context hierarchy     13/09/24 14:08:24 info xml.xmlbeandefinitionreader: loading xml bean definitions class path resource [context.xml]     13/09/24 14:08:24 info support.defaultlistablebeanfactory: pre-instantiating singletons in org.springframework.beans.factory.support.defaultlistablebeanfactory@3cdd17f5: defining beans [props]; root of factory hierarchy     printing out cluster.properties read via spring...      instancename= null     username= null     password= null     zooservers= null     tablename= null     datafile= null     datadelim= null     rowcount= 0      exception in thread "main" java.lang.illegalargumentexception: argument null:is null- arg1? true arg2? true         @ org.apache.accumulo.core.util.argumentchecker.notnull(argumentchecker.java:36)         @ org.apache.accumulo.core.client.zookeeperinstance.<init>(zookeeperinstance.java:99)         @ org.apache.accumulo.core.client.zookeeperinstance.<init>(zookeeperinstance.java:85)         @ accumuloingest.ingester.ingestdata(ingester.java:65)         @ accumuloingest.loadproperties.startingest(loadproperties.java:69)         @ accumuloingest.mainapp.main(mainapp.java:44) 

am missing piece of spring framework loads properties in cluster.properties file? had tried adding @autowired both mainapp , loadproperties java classes didn't seem help.

if you're going use @bean, you'll need @configuration. shouldn't declare xml context include annotation context. shouldn't use @configuration class instance bean. classpathxmlapplicationcontext no processing annotation based configurations.

use following

@configuration @componentscan(basepackageclasses = loadproperties.class) public static class config {     @bean     public static propertyplaceholderconfigurer props() {         propertyplaceholderconfigurer ppc = new propertyplaceholderconfigurer();         resource[] resource = new classpathresource[] { new classpathresource(                 "/eclipseprojectname/src/cluster.properties") };         ppc.setlocations(resource);         ppc.setignoreunresolvableplaceholders(true);         return ppc;     }        @bean     public loadproperties loadproperties() {         return new loadproperties();     } }  public static class loadproperties {     private @value("${cluster.zooservers}") string zooservers;     ... // getters , setters }  public static void main(string[] args) throws exception {     annotationconfigapplicationcontext context = new annotationconfigapplicationcontext(config.class);     loadproperties load = (loadproperties) context.getbean(loadproperties.class);     system.out.println(load.getzooservers()); } 

a few things note:

  1. in classpathresource need specify classpath resource. have resource /eclipseprojectname/src/cluster.properties @ root of classpath? doubt it.
  2. in case won't need @componentscan, familiarize it.
  3. a propertyplaceholderconfigurer needs declared static can initialized before other @bean declarations. should use propertysourcesplaceholderconfigurer explained in javadoc.

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -