Advertisement
Gigi95

Spring 'hello' code

Apr 4th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.15 KB | None | 0 0
  1. package hello;
  2.  
  3. public interface MessageService {
  4.     String getMessage();
  5. }
  6.  
  7. package hello;
  8.  
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.stereotype.Component;
  11.  
  12. @Component
  13. public class MessagePrinter {
  14.  
  15.     final private MessageService service;
  16.  
  17.     @Autowired
  18.     public MessagePrinter(MessageService service) {
  19.         this.service = service;
  20.     }
  21.  
  22.     public void printMessage() {
  23.         System.out.println(this.service.getMessage());
  24.     }
  25. }
  26.  
  27. package hello;
  28.  
  29. import org.springframework.context.ApplicationContext;
  30. import org.springframework.context.annotation.*;
  31.  
  32. @Configuration
  33. @ComponentScan
  34. public class Application {
  35.  
  36.     @Bean
  37.     MessageService mockMessageService() {
  38.         return new MessageService() {
  39.             public String getMessage() {
  40.               return "Hello World!";
  41.             }
  42.         };
  43.     }
  44.  
  45.   public static void main(String[] args) {
  46.       ApplicationContext context =
  47.           new AnnotationConfigApplicationContext(Application.class);
  48.       MessagePrinter printer = context.getBean(MessagePrinter.class);
  49.       printer.printMessage();
  50.   }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement