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