Spring annotation @ Resource and @ Autowired, springautowired Annotation
Spring not only supports the @ Autowired annotation defined by itself, but also supports several annotations defined by the JSR-250 specification, such as: @ Resource, @ PostConstruct and @ PreDestroy. 1. @ Autowired is provided by Spring. You need to import Package: org. springframework. beans. factory. annotation. Autowired; only inject according to byType. 2. @ Resource is automatically injected by byName by default. It is provided by J2EE and needs to be imported into Package: javax. annotation. resource; @ Resource has two important attributes: name and type. Spring resolves the name attribute of @ Resource annotation to the bean name, And the type attribute to the bean type. Therefore, if the name attribute is used, the automatic injection policy of byName is used, and the automatic injection policy of byType is used when the type attribute is used. If neither name nor type is specified, the by Name automatic injection policy is used through reflection. @ Resource assembly sequence (1 ). if both name and type are specified, a unique matching bean is found in the Spring context for assembly. If no matching bean is found, an exception is thrown. (2 ). if the name is specified, the bean with the matching name (id) is searched in the context for assembly. If no name is found, an exception is thrown. (3 ). if the type is specified, an exception is thrown if the unique bean of the type matching is found in the context for assembly. (4 ). if neither name nor type is specified, the system automatically performs the Assembly in byName mode. If no match is found, the system returns to the original type for matching. If the match exists, the system automatically assembles the data; @ Resource is equivalent to @ Autowired, but @ Autowired is automatically injected by byType. 3. usage difference @ Resource (name = "loginService") private LoginService loginService; @ Autowired (required = false) @ Qualifier ("loginService") private LoginService loginService; (1 ). @ Autowired and @ Resource can both be used to assemble beans. can be written in fields or in the setter method; (2 ). @ Autowired is assembled by type by default. By default, dependent objects must exist. To allow null values, set the required attribute to false, for example: @ Autowired (required = false ). if you want to use name assembly, you can use the @ Qualifier annotation. (3 ). @ Resource (This annotation belongs to J2EE). The installation name is installed by default. Configuration, the name can be specified through the name attribute. If the name attribute is not specified, when the annotation is written on the field, the field name is used by default to find the installation name, if the annotation is written on the setter method, the property name is used by default for assembly. If the bean matching the name cannot be found, the configuration is based on the type. However, it should be noted that if the name attribute is specified, it will only be assembled by name. We recommend that you use the @ Resource Annotation on the field so that you do not need to write the setter method. This annotation belongs to J2EE and reduces the coupling with Spring. In this way, the code looks more elegant. In addition, through practice, we also come up with a rule: if @ requied or @ autowired is written above the set method, the program will go inside the set method. However, if it is written on the field, it will not enter the set method.
[New Technology] The most popular java background framework combination java springmvc mybaits mysql oracle html5 background framework source codeObtain 【]