I have a class Config
:
Config.java
public class Config {
private final String p = "Prop";
@Bean
public String getP(){return p;}
}
How do I inject this to some constructor, ie:
public class SomeC {
private String p;
public SomeC(String p) {
this. p = p;
}
}
I want this String p
to have injected value from Config. Is that possible?
You will have to name the bean, and then use the @Qualifier
annotation when autowiring referencing that name.
Example:
Config.java
public class Config {
private final String p = "Prop";
@Bean(name="p")
public String getP(){return p;}
}
SomeC.java
public class SomeC {
private String p;
@Autowired
public SomeC(@Qualifier("p") String p) {
this. p = p;
}
}
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