java - Spring JavaConfig properties in bean is not getting set? -
i looking @ using spring javaconfig property files properties in bean not getting set?in bean not getting set?
here webconfig:
@configuration @enablewebmvc @propertysource(value = "classpath:application.properties") @import(databaseconfig.class) @importresource("/web-inf/applicationcontext.xml") public class webmvcconfig extends webmvcconfigureradapter { private static final string message_source = "/web-inf/classes/messages"; private static final logger logger = loggerfactory.getlogger(webmvcconfig.class); @value("${rt.setpassword}") private string rtpassword; @value("${rt.seturl}") private string rturl; @value("${rt.setuser}") private string rtuser; @bean public viewresolver resolver() { urlbasedviewresolver url = new urlbasedviewresolver(); url.setprefix("/web-inf/view/"); url.setviewclass(jstlview.class); url.setsuffix(".jsp"); return url; } @bean(name = "messagesource") public messagesource configuremessagesource() { logger.debug("setting message source"); reloadableresourcebundlemessagesource messagesource = new reloadableresourcebundlemessagesource(); messagesource.setbasename(message_source); messagesource.setcacheseconds(5); messagesource.setdefaultencoding("utf-8"); return messagesource; } @bean public localeresolver localeresolver() { sessionlocaleresolver lr = new sessionlocaleresolver(); lr.setdefaultlocale(locale.english); return lr; } @override public void addresourcehandlers(resourcehandlerregistry registry) { logger.debug("setting resource handlers"); registry.addresourcehandler("/resources/").addresourcelocations("/resources/**"); } @override public void configuredefaultservlethandling(defaultservlethandlerconfigurer configurer) { logger.debug("configuredefaultservlethandling"); configurer.enable(); } @override public void addinterceptors(final interceptorregistry registry) { registry.addinterceptor(new localechangeinterceptor()); } @bean public simplemappingexceptionresolver simplemappingexceptionresolver() { simplemappingexceptionresolver b = new simplemappingexceptionresolver(); properties mappings = new properties(); mappings.put("org.springframework.web.servlet.pagenotfound", "p404"); mappings.put("org.springframework.dao.dataaccessexception", "dataaccessfailure"); mappings.put("org.springframework.transaction.transactionexception", "dataaccessfailure"); b.setexceptionmappings(mappings); return b; } @bean public requesttrackerconfig requesttrackerconfig() { requesttrackerconfig tr = new requesttrackerconfig(); tr.setpassword(rtpassword); tr.seturl(rturl); tr.setuser(rtuser); return tr; } }
the value in tr.url "rt.seturl" not value in application.properties?
i'm not 100%, think @propertysource
isn't quite right. instead of
@propertysource(value = "classpath:application.properties")
it should be:
@propertysource("classpath:application.properties")
based on this:
spring propertysource documentation
also, based on link above , since have mentioned converting java config approach instead of xml, think below might solution issue:
resolving ${...} placeholders in , @value annotations in order resolve ${...} placeholders in definitions or @value annotations using properties propertysource, 1 must register propertysourcesplaceholderconfigurer. happens automatically when using in xml, must explicitly registered using static @bean method when using @configuration classes. see "working externalized values" section of @configuration javadoc , "a note on beanfactorypostprocessor-returning @bean methods" of @bean javadoc details , examples.
the example link above how it:
@configuration @propertysource("classpath:/com/myco/app.properties") public class appconfig { @autowired environment env; @bean public testbean testbean() { testbean testbean = new testbean(); testbean.setname(env.getproperty("testbean.name")); return testbean; } }
so add @ top:
@autowired environment env;
then in method use:
tr.setpassword(env.getproperty("rt.setpassword"));
and on remaining property values. not familiar way doing it. know above approach work though.
Comments
Post a Comment