Learn maven
Pom.xml <packaging>
<artifactId>
<dependencies>
Compile package Install
Spring Dependency Injection Usage examples
<artifactId>spring-context</artifactId>
1 Package Hello; 2 3 Public Interface Messageservice {4 String getMessage (); 5 }
1 PackageHello;2 3 Importorg.springframework.beans.factory.annotation.Autowired;4 Importorg.springframework.stereotype.Component;5 6 @Component7 Public classMessageprinter {8 9 Final PrivateMessageservice Service;Ten One @Autowired A PublicMessageprinter (Messageservice service) { - This. Service =Service; - } the - Public voidPrintmessage () { -System.out.println ( This. Service.getmessage ()); - } +}
1 PackageHello;2 3 ImportOrg.springframework.context.ApplicationContext;4 Importorg.springframework.context.annotation.*;5 6 @Configuration7 @ComponentScan8 Public classApplication {9 Ten @Bean One Messageservice Mockmessageservice () { A return NewMessageservice () { - PublicString getMessage () { - return"Hello world!"; the } - }; - } - + Public Static voidMain (string[] args) { -ApplicationContext context = + NewAnnotationconfigapplicationcontext (Application.class); AMessageprinter printer = Context.getbean (messageprinter.class); at printer.printmessage (); - } -}
Spring Framework Tutorial