我有一个类Config:@H_403_2@
@H_403_2@
public class Config {
private final String p = "Prop";
@Bean
public String getP(){return p;}
}
@H_403_2@
public class SomeC {
private String p;
public SomeC(String p) {
this. p = p;
}
}
我希望这个String p从Config注入值.那可能吗?
最佳答案
您必须为bean命名,然后在自动装配引用该名称时使用@Qualifier注释.@H_403_2@
原文链接:https://www.f2er.com/spring/431595.html例:@H_403_2@
Config.java@H_403_2@
@H_403_2@
public class Config {
private final String p = "Prop";
@Bean(name="p")
public String getP(){return p;}
}
SomeC.java@H_403_2@
@H_403_2@
public class SomeC {
private String p;
@Autowired
public SomeC(@Qualifier("p") String p) {
this. p = p;
}
}