1

Given the following MyConstructorClass:

@Component
public class MyConstructorClass{

  MyObj var;
  public MyConstructorClass( MyObj constrArg ){
    this.var = var;
  }
...
}

How can I autowire a field that requires a constructor argument from a previously @Autowired field? Below is an example.

Note - this question, I believe, is similar to this one, except that my constructor argument is not a String. This code sample is slightly modified from this question.

@Service
public class MyBeanService{

  @Autowired               
  CustomObject customObj;                // no arguments to constructor

  @Autowired
  MyConstructorClass myConstructorClass; // requires `customObj` as an argument

  ....
}

How can I modify MyBeanService to properly construct myConstructorClass with customObj?

Community
  • 1
  • 1
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384

4 Answers4

2

You just need to annotated the constructor of MyConstructorClass with @Autowired:

@Component
public class MyConstructorClass {
    final private CustomObject customObj;

    @Autowired 
    public MyConstructorClass(CustomObject customObj) {
        this.customObj = customObj;
    }
}

Another alternative, (without adding the @Autowired constructor to MyConstructorClass) is to use a @Configuration bean:

@Configuration
public class MyConfiguration {         

     @Bean 
     public CustomObject customObj() {
        return customObj;
     }

     @Bean 
     public MyConstructorClass myConstructorClass() {
        return new MyConstructorClass(customObj());
     }

     @Bean
     public MyBeanService myBeanService() {
         return new MyBeanService();
     }
 }
Ricardo Veguilla
  • 3,107
  • 1
  • 18
  • 17
0

you can use the <constructor-arg> in your servlet

<bean id="myConstructorClass" class="package.MyConstructorClass">
        <constructor-arg>
            <bean class="package.CustomObject"/>
        </constructor-arg>
    </bean>
Ker p pag
  • 1,568
  • 12
  • 25
0

If this is the only place you need to use that class could use @PostConstruct to instantiate itself. I think there is a better solution, but this is off the top of my head.

@Service
public class MyBeanService{

  @Autowired               
  CustomObject customObj;

  MyConstructorClass myConstructorClass;      

  @PostConstruct
  public void construct()
  {
    myConstructorClass = new MyConstructorClass(customObj);
  }
}
  • so, `MyConstructorClass`, in this case, wouldn't need to be a Spring-managed bean, is that right? – Kevin Meredith Aug 15 '14 at 03:48
  • Yeah, if you aren't at all interested sharing the MyConstructorClass instance and the dependencies are simple you could do it this way, but the answer Ricardo Veguilla provided is better and more flexible down the road. – Joe Terzieva Aug 15 '14 at 13:00
0

In the constructor, you can refer to other beans defined in Spring.

<bean id="myConstructorClass" class="package.MyConstructorClass">
    <constructor-arg index="0" ref="customObj"/>
</bean>
sendon1982
  • 9,982
  • 61
  • 44