我们可以在bean的原始setter方法上添加@Autowired
吗?
@Autowired
如何适用于原语?
答案 0 :(得分:0)
不,对于原始值使用@Value
注释。 @Autowired
正在寻找bean。
阅读How can I inject a property value into a Spring Bean which was configured using annotations?
使用Spring Boot,这更容易:
@Component
@ConfigurationProperties(prefix="connection")
public class ConnectionSettings {
private String username;
private InetAddress remoteAddress;
// ... getters and setters
}
其中值在application.yml
connection:
username: admin
remoteAddress: 192.168.1.1