Advertisement
Guest User

Spring puzzle

a guest
May 24th, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.76 KB | None | 0 0
  1. ##### Код
  2. package tmpjavaproj;
  3.  
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.beans.factory.annotation.Qualifier;
  6. import org.springframework.boot.SpringApplication;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9.  
  10. @Configuration
  11. public class Test {
  12.  
  13.     @Bean(name = { "aBean", "myBean" })
  14.     public Object myBean() {
  15.         return "Hi there";
  16.     }
  17.  
  18.     @Bean(name = { "aBean", "yourBean" })
  19.     public Object yourBean() {
  20.         return "Bye here";
  21.     }
  22.  
  23.     @Bean
  24.     public Object check(@Autowired @Qualifier("myBean") Object myBean, @Autowired @Qualifier("yourBean") Object yourBean) {
  25.         System.out.println(myBean + " // " + yourBean);
  26.         return null;
  27.     }
  28.  
  29.     public static void main(String[] args) {
  30.         SpringApplication.run(Test.class);
  31.     }
  32. }
  33.  
  34.  
  35. ##### Вивід
  36. Bye here // Bye here
  37.  
  38. ##### Код №2
  39. package tmpjavaproj;
  40.  
  41. import org.springframework.beans.factory.annotation.Autowired;
  42. import org.springframework.beans.factory.annotation.Qualifier;
  43. import org.springframework.boot.SpringApplication;
  44. import org.springframework.context.annotation.Bean;
  45. import org.springframework.context.annotation.Configuration;
  46.  
  47. @Configuration
  48. public class Test {
  49.  
  50.     @Bean(name = { "myBean", "aBean" })
  51.     public Object myBean() {
  52.         return "Hi there";
  53.     }
  54.  
  55.     @Bean(name = { "yourBean", "aBean" })
  56.     public Object yourBean() {
  57.         return "Bye here";
  58.     }
  59.  
  60.     @Bean
  61.     public Object check(@Autowired @Qualifier("myBean") Object myBean, @Autowired @Qualifier("yourBean") Object yourBean) {
  62.         System.out.println(myBean + " // " + yourBean);
  63.         return null;
  64.     }
  65.  
  66.     public static void main(String[] args) {
  67.         SpringApplication.run(Test.class);
  68.     }
  69. }
  70.  
  71. ##### Вивід №2
  72. Hi there // Bye here
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement