Spring Framework Tutorial

Source: Internet
Author: User
Tags getmessage

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.