我有一节课Config
:
Config.java
public class Config { private final String p = "Prop"; @Bean public String getP(){return p;} }
我如何将其注入一些构造函数,即:
public class SomeC { private String p; public SomeC(String p) { this. p = p; } }
我希望这个String p
从Config注入值.那可能吗?
您必须为bean命名,然后@Qualifier
在自动装配引用该名称时使用注释.
例:
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; } }