I am turning old xml/java configuration into pure java config. In xml I used injection of parameters into configuration file like this:
<bean class="com.project.SpringRestConfiguration">
<property name="parameters" ref="parameters" />
</bean>
@Configuration
public class SpringRestConfiguration {
private Parameters parameters;
public void setParameters(Parameters parameters) {
this.parameters = parameters;
}
// @Bean definitions
...
}
Is it possible to inject Parameters in javaconfig? (Without the need of using autowiring!)
@Configuration
@Import(SpringRestConfiguration.class)
EDIT: With @Import I can't see any chance to inject Parameters into SpringRestConfiguration
Basically you would need to use @Autowired
but you can still use a name and not type interpretation like this:
@Configuration
public class SpringRestConfiguration {
@Autowired
@Qualifier("parameters") // Somewhere in your context you should have a bean named 'parameters'. It doesn't matter if it was defined with XML, configuration class or with auto scanning. As long as such bean with the right type and name exists, you should be good.
private Parameters parameters;
// @Bean definitions
...
}
This solves the confusion problem you mentioned when using @Autowired
- there's no question here which bean is injected, the bean that is named parameters
.
You can even do a little test, leave the parameters
bean defined in the XML as before, use @Autowired
, see that it works. Only then migrate parameters
to @Configuration
class.
In my answer here you can find a complete explanation of how you should migrate XML to @Configuration
step by step.
You can also skip the private member altogether and do something like this:
@Configuration
public class SpringRestConfiguration {
@Bean
public BeanThatNeedsParamters beanThatNeedsParamters (@Qualifier("parameters") Parameters parameters) {
return new BeanThatNeedsParamters(parameters)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With