i'm new spring framework , i'm having problems trying read , use properties file.
to summarize, want define class stores properties read, second class uses properties , third class uses results.
the class stores properties is:
@configuration public class propertyclass { @value("${propertyname") private integer propertyname; @bean(name = "propertyname") public integer getpropertyname() { return propertyname; } }
the class reads , uses properties:
@component public class propertyreader { private integer myproperty; @autowire @qualifier("propertyname") public void setmyproperty( integer myproperty) { this.myproperty = myproperty; } public integer getvalue() { //do myproperty return result; } }
and class uses propertyreader:
public class utilizer { private propertyreader getpropertyreader() { applicationcontext context = new annotationconfigapplicationcontext(propertyreader.class); propertyreader reader = (bakerstorageclassconfighelper)context.getbean("propertyreader"); return reader; } }
i've registered classes beans in application-config.xml file:
<bean class="property.class.package.propertyclass" depends-on="environment" /> <bean class="reader.class.package.propertyreader" />
and have environment.xml file "environment" bean defined location rules find property files.
now happens in class "utilizer" when try "applicationcontext" object exception thrown:
org.springframework.beans.factory.beancreationexception: error creating bean name 'propertyreader': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire method: public void reader.class.package.propertyreader.setmyproperty(java.lang.integer); nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [java.lang.integer] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {}
i've tried change annotation of propertyreader class @repository or @service , tried add @componentscan propertyclass package specified none of worked me..
could give me advices?
thank you!
i not quite why need declare propertyname
integer. if need properties file, can define propertiesfactorybean
, autowire other beans like.
let's have myvalues.properties file containing values:
key1=value1 key2=value2
define bean:
@bean(name = "myproperties") public propertiesfactorybean detailqueriesfactorybean() { propertiesfactorybean pfb = new propertiesfactorybean(); pfb.setlocation(new classpathresource("com/xxx/myvalues.properties")); return pfb; }
now wherever need it, do:
@autowired @qualifier("myproperties") private properties myvaluescontainer; public void mymethod(){ //this "value1" string value1 = myvaluescontainer.getproperty("key1"); }
hope works you.
--------------------- case----------------
if in application context, can use @value
inject value directly in propertyreader
, add getter/setter them. no need propertyclass
, right?
or can add @postconstruct method propertyreader
. inside method, can retrieve values need existing context.
@postcontstruct public void extractvalues(){ //retrieve value context , assign whichever var. }
Comments
Post a Comment