/**
* The Resource annotation marks a resource that is needed
* by the application. This annotation may be applied to an
* application component class, or to fields or methods of the
* component class. When the annotation is applied to a
* field or method, the container will inject an instance
* of the requested resource into the application component
* when the component is initialized. If the annotation is
* applied to the component class, the annotation declares a
* resource that the application will look up at runtime. <p>
*
* Even though this annotation is not marked Inherited, deployment
* tools are required to examine all superclasses of any component
* class to discover all uses of this annotation in all superclasses.
* All such annotation instances specify resources that are needed
* by the application component. Note that this annotation may
* appear on private fields and methods of superclasses; the container
* is required to perform injection in these cases as well.
*
* @since Common Annotations 1.0
*/
@Target({TYPE, FIELD, METHOD})
@Retention(RUNTIME)
This is @Resource注解的介绍,@Resource用在字段或者方法上的时候,Spring会从容器里面寻找需要的类型Bean,@ResourceWhen used on a class, Spring will put the current class as a resource into the Spring container.
@Autowired注解表示让Spring容器自动注入这个bean,@Resource表示把当前注解的类交给Spring容器管理,你不使用@ResourceAnnotation, the Spring container will not load this type of bean, and an error will naturally be reported when it needs to be injected
There is something wrong with using @Resource on the interface, right? It should be placed on the implementation class
If the first interface does not add @Resource, the implementation of the interface will not be managed by spring, so subsequent injections that reference the interface will fail
@Resource annotation declaration, if you don’t declare that the bean cannot be found using @Autowired It can be simply understood like this, a resource pool called spring, you have to use @Resource @Controller and other methods to make a declaration in the pool, and then put it in Use methods such as @Autowired @Named to take it out from inside
@Resource means that the bean is managed by the spring container. Generally, the resource is written on the implementation class of the class or interface, rather than directly on the interface. Let spring container manage it means that when you want to instantiate the class, spring will automatically create the object for you.
@Autowired means that the attribute (usually written on the attribute or set method) is automatically injected by spring. Only if the resource exists in the spring container (the bean with resource added is treated as a resource), spring can automatically inject it for you. If there is no such resource in the spring container, if you automatically inject it, you will definitely not be able to find the resource and report an error.
Appropriate and inappropriate example There is a package register (Spring container) at the train station, and you want to store your package in the container. You put a @Resource tag on the package, and the staff will give you a small note indicating that the resource belongs to you. When you want to get the package (get the resource), you can take the strip (@Autowired) to find the resource in the package register. If your bag is indeed stored in the container, then the bag will be given to you according to your note. If you haven’t deposited your bag in the container and still want to ask the staff for a bag, then report an error. You start a duel with the staff. . . . . . . . . . . . . . Spring is very upright. If you have it, I will give it to you. If you don’t have it, you don’t have it. If you don’t have resources, you can ask me for it again. I will report the error to you! ! ! PS: I feel that the example given is very confusing, please understand it yourself.
This is
@Resource
注解的介绍,@Resource
用在字段或者方法上的时候,Spring会从容器里面寻找需要的类型Bean,@Resource
When used on a class, Spring will put the current class as a resource into the Spring container.@Autowired
注解表示让Spring容器自动注入这个bean,@Resource
表示把当前注解的类交给Spring容器管理,你不使用@Resource
Annotation, the Spring container will not load this type of bean, and an error will naturally be reported when it needs to be injectedThere is something wrong with using
@Resource
on the interface, right? It should be placed on the implementation classUnified and handed over to spring management
If the first interface does not add @Resource, the implementation of the interface will not be managed by spring, so subsequent injections that reference the interface will fail
@Resource annotation declaration, if you don’t declare that the bean cannot be found using @Autowired
It can be simply understood like this, a resource pool called spring, you have to use @Resource @Controller and other methods to make a declaration in the pool, and then put it in
Use methods such as @Autowired @Named to take it out from inside
@Resource
means that the bean is managed by the spring container. Generally, the resource is written on the implementation class of the class or interface, rather than directly on the interface. Let spring container manage it means that when you want to instantiate the class, spring will automatically create the object for you.
@Autowired
means that the attribute (usually written on the attribute or set method) is automatically injected by spring. Only if the resource exists in the spring container (the bean with resource added is treated as a resource), spring can automatically inject it for you. If there is no such resource in the spring container, if you automatically inject it, you will definitely not be able to find the resource and report an error.
Appropriate and inappropriate example
There is a package register (Spring container) at the train station, and you want to store your package in the container. You put a @Resource tag on the package, and the staff will give you a small note indicating that the resource belongs to you. When you want to get the package (get the resource), you can take the strip (@Autowired) to find the resource in the package register.
If your bag is indeed stored in the container, then the bag will be given to you according to your note.
If you haven’t deposited your bag in the container and still want to ask the staff for a bag, then report an error. You start a duel with the staff. . . . . . . . . . . . . .
Spring is very upright. If you have it, I will give it to you. If you don’t have it, you don’t have it. If you don’t have resources, you can ask me for it again. I will report the error to you! ! !
PS: I feel that the example given is very confusing, please understand it yourself.