31

My spring bean have a constructor with an unique mandatory argument, and I managed to initialize it with the xml configuration :

<bean name="interfaceParameters#ota" class="com.company.core.DefaultInterfaceParameters">
  <constructor-arg>
    <value>OTA</value>
  </constructor-arg>
 </bean>

Then I use this bean like this and it works well.

 @Resource(name = "interfaceParameters#ota")
 private InterfaceParameters interfaceParameters;

But I would like to specify the contructor arg value with the annocations, something like

 @Resource(name = "interfaceParameters#ota")
 @contructorArg("ota") // I know it doesn't exists!
 private InterfaceParameters interfaceParameters;

Is this possible ?

Thanks in advance

tbruyelle
  • 12,895
  • 9
  • 60
  • 74

2 Answers2

77

First, you have to specify the constructor arg in your bean definition, and not in your injection points. Then, you can utilize spring's @Value annotation (spring 3.0)

@Component
public class DefaultInterfaceParameters {

    @Inject
    public DefaultInterfaceParameters(@Value("${some.property}") String value) {
         // assign to a field.
    }
}

This is also encouraged as Spring advises constructor injection over field injection.

As far as I see the problem, this might not suit you, since you appear to define multiple beans of the same class, named differently. For that you cannot use annotations, you have to define these in XML.

However I do not think it is such a good idea to have these different beans. You'd better use only the string values. But I cannot give more information, because I dont know your exact classes.

Kaan
  • 381
  • 5
  • 13
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • thanks for your response, what do you mean by "use only the string values" ? – tbruyelle Nov 17 '10 at 12:48
  • well, you need the "OTA" string. You don't need a whole object for it. – Bozho Nov 17 '10 at 12:49
  • 1
    oh I see, the "OTA" string is used in the @postConstruct method of my bean and it's a mandatory information for building the bean. The string is used to retrieve datas from the database. Each bean of this type will return different datas for each string value. BTW you are right about the xml usage, as I define multiple bean for the same class, I can't use annotations. – tbruyelle Nov 17 '10 at 13:02
  • 30
    > "spring frowns upon constructor injection" ... not sure of your reference for that, but this Spring blog post (http://spring.io/blog/2007/07/11/setter-injection-versus-constructor-injection-and-the-use-of-required/) says "We usually advise people to use constructor injection for all mandatory collaborators and setter injection for all other properties." Certainly when I worked for SpringSource there was no frowning going on. – Andrew Swan Oct 28 '14 at 02:12
  • Why are you using @inject instead of @autowired? – Phate Jun 03 '20 at 20:50
2

As Bozho said, instead of constructor arg you could set the property...@PostConstruct will only get called after all the properties are set...so, you will still have your string available ...

tapasvi
  • 322
  • 1
  • 7
  • 15