Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. package lessons.services;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Qualifier;
  5. import org.springframework.context.ApplicationContext;
  6.  
  7. import javax.annotation.Resource;
  8. import java.util.Map;
  9.  
  10. public class AutowiredClass {
  11.  
  12. @Autowired //к полям класса
  13. @Qualifier("main")
  14. //@Autowired(required = false) //чтобы не бросалось исключение,
  15. //если не с кем связать
  16. //рекомендуется использовать @Required
  17. private GreetingService greetingService;
  18.  
  19. @Autowired //к полям класса в виде массива или коллекции
  20. private GreetingService[] services;
  21.  
  22. @Autowired //к Map, где ключами являются имена бинов, значения - сами бины
  23. private Map<String, GreetingService> serviceMap;
  24.  
  25. @Autowired //к конструктору
  26. public AutowiredClass(@Qualifier("main") GreetingService service) {}
  27.  
  28. @Autowired //к обычным методам с произвольным названием аргументов и их количеством
  29. public void prepare(GreetingService prepareContext){/* что-то делаем... */}
  30.  
  31. @Resource //По умолчанию поиск бина с именем "context"
  32. private ApplicationContext context;
  33.  
  34. @Autowired //к "традиционному" setter-методу
  35. @Resource(name="greetingService") //Поиск бина с именем "greetingService"
  36. public void setGreetingService(GreetingService service) { this.greetingService = service; }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement