java - @Value in spring boot is not injecting value from application.properties -
i using easyrules api in code. following rule class accesses double value application.properties
file present in src/main/resources
:
@rule(name = "over-weight rule") // indicate class rule @propertysource("/application.properties") public class overweightrule { /** * user input represents data * rule operate on. */ private metric metric; @autowired @qualifier("alertservicedao") private alertservice alertservice; @value("${base.weight}") private double baseweight; public overweightrule(metric metric) { this.metric = metric; } @condition public boolean isoverweight() { // rule should applied if // weight in metric object shoots 10% on base weight. if(metric.getweight() > (baseweight + (0.1 * baseweight))) return true; return false; } @action public void createandstoreoverweightalert() { // when rule conditions satisfied, // alert created. alert alert = new alert(); alert.setalerttype(alert.alerttype.over_weight); alert.setweight(metric.getweight()); alert.settimestamp(metric.gettimestamp()); alertservice.createalert(alert); } }
the value in baseweight 0.0 whereas have set 150.0 in properties file. read spring boot automatically reads values application.properties
file present in src/main/resources
no need use @propertysource("/path properties file")
. still added @propertysource
annotation result same. wrong in code? not want add @configuration
class because want keep class rule class defined easy rules framework.
one alternative declare @component
, removes @propertysource
, keep @value("${base.weight}")
@rule(name = "over-weight rule") // indicate class rule @component("myoverweightrule") public class overweightrule {
when create rulesenginefactorybean
can autowire myoverweightrule
:
@bean @autowired rulesenginefactorybean rulesenginefactorybean(overweightrule myoverweightrule){ rulesenginefactorybean rules = new rulesenginefactorybean(); rules.setrules(arrays.aslist(myoverweightrule)); return rules; }
and call firerules getting rulesenginefactorybean
rulesengine rulesengine = (rulesengine) ctx.getbean("rulesenginefactorybean"); rulesengine.firerules();
Comments
Post a Comment