-1

Can we add @Autowired on primitive setter method of a bean?

How does @Autowired work for primitive?

MariuszS
  • 30,646
  • 12
  • 114
  • 155

1 Answers1

0

No, for primitive value use @Value annotation. @Autowired is looking for bean.

Read How can I inject a property value into a Spring Bean which was configured using annotations?

With Spring Boot this is even easier:

@Component
@ConfigurationProperties(prefix="connection")
public class ConnectionSettings {

    private String username;

    private InetAddress remoteAddress;

    // ... getters and setters

}

where values are configured in application.yml

connection:
    username: admin
    remoteAddress: 192.168.1.1
Community
  • 1
  • 1
MariuszS
  • 30,646
  • 12
  • 114
  • 155