October 2018
Intermediate to advanced
590 pages
15h 5m
English
The second way is to declare a method, usually a setter, and apply the @Autowired annotation or the @Required annotation to it. For example, we can remove the MessageService constructor and add a setRepository(MessageRepository) method, as follows:
public class MessageService { ... @Required public void setRepository(MessageRepository repository) { this.repository = repository; } ...}
Or, you can name it something else, such as the following:
@Autowiredpublic void prepare (MessageRepository repository) { this.repository = repository;}
Read now
Unlock full access