Spring Constructor Injection and Argument names

At runtime, java classes do not retain the name of the constructor or method parameters, unless classes are compiled with debug options on.

This has some interesting implications for Spring Constructor Injection.

Consider the following simple class

 

 

 

 

package dbg;
public class Person {

 private final String first;
 private final String last;
 private final Address address;

 public Person(String first, String last, Address address){
  this.first = first;
  this.last = last;
  this.address = address;
 }

 public String getFirst() {
  return first;
 }

 public String getLast() {
  return last;
 }

 public Address getAddress() {
  return address;
 }
}

and a sample Spring bean configuration xml file:

 <bean name='address1' class='dbg.Address' p:street1='street1' p:street2='street1' p:state='state1'/>
 <bean name='person1' class='dbg.Person' c:address-ref='address1' c:last='Last1' c:first='First1'  ></bean>
 <bean name='person2' class='dbg.Person' c:first='First2' c:address-ref='address1' c:last='Last2'   ></bean>

Here I am using the c namespace for constructor injection. This fails with the exception that the argument types are ambiguous – this is because the first argument is a String and since its runtime representation does not have the argument name present, Spring cannot determine if it should be substituted for the first name or last. There are a couple of fixes possible for this scenario: 1. To use index based constructor injection, the drawback though is that it is very verbose:

 <bean name='person1' class='dbg.Person' >
  <constructor-arg value='First1'></constructor-arg>
  <constructor-arg value='Last1'></constructor-arg>
  <constructor-arg ref='address1'></constructor-arg>
 </bean>
 <bean name='person2' class='dbg.Person' >
  <constructor-arg value='First2'></constructor-arg>
  <constructor-arg value='Last2'></constructor-arg>
  <constructor-arg ref='address1'></constructor-arg>
 </bean>

2. To compile with debug symbols on, this can be done by passing a -g or -g:var flag to the java compiler – this will ensure that the parameter names are preserved in the class file and the original concise bean configuration with c namespace will work. 3. A neat fix is to annotate the constructor with @ConstructorProperties which basically provides the argument names to Spring:

public class Person {

 private final String first;
 private final String last;
 private final Address address;

 @ConstructorProperties({'first','last','address'})
 public Person(String first, String last, Address address){
  this.first = first;
  this.last = last;
  this.address = address;
 }

This works with or without debug options turned on. 4. Probably the best fix of all is to simply use @Configuration to define the beans:

@Configuration
public static class TestConfiguration{

 @Bean
 public Address address1(){
  return new Address();
 }
 @Bean
 public Person person1(){
  return new Person('First1', 'Last1', address1());
 }
 @Bean
 public Person person2(){
  return new Person('First2', 'Last2', address1());
 }

}


Reference: Spring Constructor Injection and Argument names from our JCG partner Biju Kunjummen at the all and sundry blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

One Response to "Spring Constructor Injection and Argument names"

  1. +1 for mentioning about @Configuration. It should be THE configuration choice for every new application.

Leave a Reply


six − = 1



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close