java - How to work with annotation based properties in Spring -
i use 'someotherproperty' value inside someifacedaoimpl
but when debug, it's null, inside bean definition , inside bean constructor well. tried use @value annotation inside class not work either.
however, database values works fine , available inside jdbctemplate bean.
my properties file contains
database.url=jdbc:mysql://localhost:3306/databasename database.username=root database.password=password someotherproperty=helloworld
my configuration class:
@configuration @profile("production") @componentscan(basepackages = { "com.packagename" }) @propertysource({"classpath:packagename.properties"}) public class contextconfig { @value("${database.url}") private string url; @value("${database.username}") private string username; @value("${database.password}") private string password; @value("${someotherproperty}") private string someotherproperty; @bean(name = "jdbctemplate") public jdbctemplate jdbctemplate() { jdbctemplate jdbctemplate = new jdbctemplate(); basicdatasource datasource = new basicdatasource(); datasource.seturl(stringutil.appendobjects(url, "?", "useserverprepstmts=false&rewritebatchedstatements=true")); datasource.setdriverclassname("com.mysql.jdbc.driver"); datasource.setusername(username); datasource.setpassword(password); jdbctemplate.setdatasource(datasource); return jdbctemplate; } @bean public isomeifacedao isomeifacedao() { return new isomeifacedaoimpl(); //<---- have someotherproperty value here or inside constructor } }
thank you.
you should able use 'someotherproperty' directly in bean method there's no misconfiguration in property file. better approach avoid having multiple fields annotated @value
using environment
abstraction
@configuration @profile("production") @componentscan(basepackages = { "com.packagename" }) @propertysource({"classpath:packagename.properties"}) public class contextconfig { @autowired private environment env; @bean public isomeifacedao isomeifacedao() { return new isomeifacedaoimpl(env.getrequiredproperty("someotherproperty")); } }
Comments
Post a Comment