0

I'm working with Spring and I want to initiate my service (model) with dependecy injection. My service expects a Propertie object, containing the url, user, password,ssl,sslfactory parameters for the database. I was wondring if this could still be done using dependency injection.

So my constructor looks like this: Service(Properties propertie) Inside my controller I've got a private field service:

@Autowired
private Service service;

And now I want to inject the right properties in it.

<bean id="service" class="service.Service">
        <constructor-arg > 
            <value>?</value>
        </constructor-arg> 
    </bean>

I don't know if it can be done, can anyone help me? Thanks in advance.

Bosiwow
  • 2,025
  • 3
  • 28
  • 46

2 Answers2

0

Try this:

<util:properties id="props" location="classpath:props.properties" />

and then:

 @Value("${props.foo}")
 public String foo;

It will inject property directly to the field. You'll need to use annotation config.

Or if you prefer pure xml then try this:

<bean id="service" class="service.Service">
    <constructor-arg > 
        <value>#{props['foo']}</value>
    </constructor-arg> 
</bean>
Yegor Chumakov
  • 450
  • 4
  • 11
  • #{props['foo']} , Where does the foo points to? And also, where should my database.properties file be located? Uder web.xml? – Bosiwow Apr 08 '15 at 13:07
  • It's points to properties object with id "props" to the property name foo which should be located anywhere in classpath. For instance under WEB-INF/classes folder. Or in src/resources if you are using maven. – Yegor Chumakov Apr 08 '15 at 13:09
  • I don't understand your answer, sorry. I'm reading a bit more and try to get it working. Thanks anyways. – Bosiwow Apr 08 '15 at 13:27
  • You said #{props['foo']} points to one specific key-value pair from the properties file. But that's not what I want. I want to pass the entire Properties Object to my service, not key-value by key-value. But all the key-value pairs at once. Or am I missing something? – Bosiwow Apr 08 '15 at 13:50
0

The proper way to do this is by using PropertyPlaceholderConfigurer.

Palcente
  • 625
  • 2
  • 7
  • 21