Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hello;
- public interface MessageService {
- String getMessage();
- }
- package hello;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- @Component
- public class MessagePrinter {
- final private MessageService service;
- @Autowired
- public MessagePrinter(MessageService service) {
- this.service = service;
- }
- public void printMessage() {
- System.out.println(this.service.getMessage());
- }
- }
- package hello;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.annotation.*;
- @Configuration
- @ComponentScan
- public class Application {
- @Bean
- MessageService mockMessageService() {
- return new MessageService() {
- public String getMessage() {
- return "Hello World!";
- }
- };
- }
- public static void main(String[] args) {
- ApplicationContext context =
- new AnnotationConfigApplicationContext(Application.class);
- MessagePrinter printer = context.getBean(MessagePrinter.class);
- printer.printMessage();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement