Spring Dependency Injection

Source: Internet
Author: User

Spring Dependency Injection: Spring and injection-related annotations are: Autowired,resource,qualifier,service,controller,repository,component,

Autowired: Automatically finds beans from the context of spring to inject.

Resource: Used to specify the name to inject.

Service,controller,repository is used to inject the service layer, the control layer, the database layer, and the spring scan annotations, which are flagged when these need to be generated beans.

The above autowired and resource are used to modify the field, constructor, or set method, and do inject. Instead, Service,controller,repository,component is used to modify classes to mark these classes to generate beans.

applicationcontext AppContext = new annotationconfigapplicationcontext(" Cn.outofmemory.helloannotation ");  carservice Service = appContext.  Getbean(carservice.  Class); service.  Addcar("BMW");                       

In the main method above we first initialized the Appcontext, which is Annotationconfigapplicationcontext, whose constructor accepts the name of a package to qualify the package to be scanned.

You can then get an example of Carservice using the Appcontext Getbean method.

    <context:annotation-config/>    <context:component-scanbase-package=" Cn.outofmemory.helloannotation "></context:component-scan>      





Spring Dependency Injection

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.