java annotation: java javax.annotation.Resource
When we inject objects into class configurations in xml, we will find that the xml file will become more and more bloated, which is very troublesome to maintain. At this time, we can use annotation mechanism to inject objects into class configuration.
Java provides us with the annotation javax.annotation.Resource.
The spring framework provides org.springframework.beans.factory.annotation.Autowired.
Generally, we use the annotation javax.annotation.Resource, because in this way we can implement the solution to the spring framework.
@Resource can be used on fields and functions. When it works on a field, if we just write it simply
@Resource
PersonDao p;
At this time, the process of spring injection p is 1: First find out whether there are elements in the xml with id p with id p
2: If not found, see if there is a name attribute (@Resource name=""), and if there is, look for name
3: Otherwise, look for elements of type persondao
@Resource can be used on the set function.
For example:
@Resource public void setP(PersonDao p) { this.p = p; }@Autowired annotation is searched based on type, such as PersonDao p, which will search for elements of type PersonDao in the xml file.
Thank you for reading, I hope it can help you. Thank you for your support for this site!