In this example, how do I specify the value of "constrArg" in
MyBeanService
with the@Autowire
annotation? Is there any way to do this?
No, not in the way that you mean. The bean representing MyConstructorClass
must be configurable without requiring any of its client beans, so MyBeanService
doesn't get a say in how MyConstructorClass
is configured.
This isn't an autowiring problem, the problem here is how does Spring instantiate MyConstructorClass
, given that MyConstructorClass
is a @Component
(and you're using component-scanning, and therefore not specifying a MyConstructorClass
explicitly in your config).
As @Sean said, one answer here is to use @Value
on the constructor parameter, so that Spring will fetch the constructor value from a system property or properties file. The alternative is for MyBeanService
to directly instantiate MyConstructorClass
, but if you do that, then MyConstructorClass
is no longer a Spring bean.