Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring: How to inject values with @Value annotation by reading from external properties file?
    text
    copied!<p>I have following situation<br> - Have a MongoService class, which reads host, port, database from file </p> <p><strong>xml configuration</strong> </p> <pre><code>&lt;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-3.0.xsd"&gt; &lt;bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;property name="locations"&gt; &lt;list&gt; &lt;value&gt;file:///storage/local.properties&lt;/value&gt; &lt;/list&gt; &lt;/property&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre> <p>The <strong>local.properties</strong> looks like </p> <pre><code>### === MongoDB interaction === ### host="127.0.0.1" port=27017 database=contract </code></pre> <p>and <strong>MongoService Class</strong> as </p> <pre><code>@Service public class MongoService { private final Mongo mongo; private final String database; private static final Logger LOGGER = LoggerFactory.getLogger(MongoService.class); public MongoService(@Nonnull final @Value("#{ systemProperties['host']}") String host, @Nonnull final @Value("#{ systemProperties['port']}") int port, @Nonnull final @Value("#{ systemProperties['database']}") String db) throws UnknownHostException { LOGGER.info("host=" + host + ", port=" + port + ", database=" + db); mongo = new Mongo(host, port); database = db; } } </code></pre> <p>When I want to test that bean is fine, I do the following in<br> <strong>MongoServiceTest.java</strong> </p> <pre><code>public class MongoServiceTest { @Autowired private MongoService mongoService; } </code></pre> <p>It complains saying that <code>can not identify bean for MongoService</code> . </p> <p>Then I add the following to above xml </p> <pre><code>&lt;bean id="mongoService" class="com.business.persist.MongoService"&gt;&lt;/bean&gt; </code></pre> <p>Then it complains saying <code>"No Matching Constructor found"</code> </p> <p><strong>What I want to do</strong> </p> <p>a.) MongoService should be <code>@Autowired</code> and reads configuration params from <code>&lt;value&gt;file:///storage/local.properties&lt;/value&gt;</code> </p> <p><strong>Question</strong> </p> <p>a.) Is accessing values in constructor are correct? <code>(file name is local.properties and I am using @Value("#{ systemProperties['host']}") syntax)</code> </p> <p>b.) What is that I need to make it work so that <code>@Autowired private MongoService mongoService</code> loads correctly and reads value off local.properties file. </p> <p>P.S. I am very new to Spring and don't really know how to make this work </p> <p>Thanks much for your help in advance</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload