1

Starting out with Spring. And, I heard someone say that it prevents the use of the 'new' keyword. So, I was curious to know how would one write the code below the Spring DI way?

private ArrayList<String> arrayMan;

public SpringDI() {
arrayMan = new ArrayList<String>();

}
AppSensei
  • 8,270
  • 22
  • 71
  • 99
  • See the link below http://stackoverflow.com/questions/2416056/how-to-define-a-list-bean-in-spring – Jabir Mar 21 '13 at 12:59

2 Answers2

3

This is how this can be done using spring.

<bean id="arrayMan" class="java.util.ArrayList">
    <constructor-arg>
        <list>
            <ref bean="one"/>
            <ref bean="two"/>                
        </list>
    </constructor-arg>
</bean>

Hope it helps

Jabir
  • 2,776
  • 1
  • 22
  • 31
1

This is perfectly ok, in terms of spring DI.

If it is the default value of the variable there is nothing wrong with initializing it with new.

But if you still want to inject a list you can use

<list>
    <value>...</value>
</list>
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531