1, an overview of the

The previous article organized the assignment of beans or certain attributes through XML files, which has become obsolete in enterprise development, and this article is a step forward. Describes how to add annotations to beans to quickly register them with the IOC container.

2. Register beans into the IOC container using annotations

If you want to add a custom bean object to the IOC container, you need to add some annotations on the class.

Spring includes four main components to add annotations:

@controller: Controller, we recommend adding this annotation to Controller layer @service: business logic, we recommend adding this annotation to business logic layer @repository: Repository management, It is recommended to add this annotation to the data access layer @Component: Add this annotation to components that are not part of the above base layerCopy the code

Note: We artificially annotate different layers, but from Spring’s point of view, you can annotate any layer. Spring does not annotate the specific level of validation at all. This is written to improve readability. The easiest way to do this is to add component annotations to all bean objects that you want to manage by the IOC container

Using annotations requires the following steps:

1. Add any of the above four annotations

Add a component that automatically scans annotations, depending on the context namespace

Context: Component ‐scan

Note: Registering a component with annotations is the same as registering a component with a configuration file, but note:

The default component ID is the first character of the component’s class name. If you must change the name, add it in the annotation. Such as @ Controller (” the userInfo “)

Components are singletons by default. If you need to configure multiple instances, you can add the @scope annotation under the annotation

<! Define the base package for automatic scanning: Base ‐package: the base package specified for scanning, <context:component-scan base-package="com.zhl"></context:component-scan>Copy the code
package com.zhl.controller;

import org.springframework.stereotype.Controller;

@Controller
public class UserController {
}
Copy the code
package com.zhl.service;

import org.springframework.stereotype.Service;

@Service
public class UserService {
}
Copy the code
package com.zhl.dao;

import org.springframework.stereotype.Repository;

@Repository
public class UserDao {
}
Copy the code

3. Define the classes to include and not to include when scanning packages

Once the basic scan package is defined, in some cases it may be necessary to selectively configure whether or not to register beans into the IOC container, as follows.

<! Define the base package for automatic scanning: Base ‐package: the base package specified for scanning, <context:component-scan base-package="com.zhl.entity" <context:component-scan base-package="com.zhl.entity" use-default-filters="false"> <! Once you have defined the basic scan package, you can exclude certain classes from the package using the following method: Annotation: exclude by annotation. Components marked with the annotation are not allowed. Expression indicates the fully qualified class name of the annotation to be filtered. Specifies to exclude a specific class. To exclude by class, expression indicates the name of the specific class that is not registered --> <! -- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>--> <! --<context:exclude-filter type="assignable" expression="com.zhl.entity.User"/> --> <! ‐default‐filters="false"--> <! --<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>--> <context:include-filter type="assignable" expression="com.zhl.entity.User"/> </context:component-scan>Copy the code

4. Use @autoWired for automatic injection

To implement automatic injection using annotations, use the @AutoWired annotation.

Note: When using the AutoWired annotation, autowiring is implemented by type.

A. If only one is found, the assignment is performed directly.

If not found, throw an exception.

C, if more than one variable is found, the match will continue according to the variable name as id.

C. 1. Assembly shall be carried out directly upon matching

C. 2. If no match is found, an exception is reported

package com.zhl.controller; import com.zhl.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @controller public class UserController {/* Use @autoWired to automatically inject byType? byname? · Match by type is preferred by default · Match by name if more than one type is matched · Error if name is not matched: 1 You can change the name of the property to the bean name: userServiceImpl 2. You can change the Bean name to correspond to the property name: @service ("userService") 3. You can set the name of the attribute (overridden) with @qualifier :@Qualifier("userServiceImpl") 4. You can set one of the beans to be the Primary auto-injected Bean with @primary: @primary 5. */ @autoWired Private UserService UserService; public void getUser() { userService.getUser(); }}Copy the code
package com.zhl.service; import org.springframework.stereotype.Service; @service public class UserService {public void getUser() {system.out.println (" return User"); }}Copy the code

5. @autowired can be defined in methods

This annotation can be used not only on member variables, but also on methods.

package com.zhl.controller; import com.zhl.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @controller public class UserController {/* Use @autoWired to automatically inject byType? byname? · Match by type is preferred by default · Match by name if more than one type is matched · Error if name is not matched: 1 You can change the name of the property to the bean name: userServiceImpl 2. You can change the Bean name to correspond to the property name: @service ("userService") 3. You can set the name of the attribute (overridden) with @qualifier :@Qualifier("userServiceImpl") 4. You can set one of the beans to be the Primary auto-injected Bean with @primary: @primary 5. */ @autoWired Private UserService UserService; public void getUser() { userService.getUser(); } /** * @autoWired can also be written on the constructor ** default to match by parameter type ** if multiple types are matched by parameter name */ @autoWired Public UserController(UserService) userService) { this.userService = userService; } /** * @autowired can also be written on a method ** default to match by parameter type ** if more than one type is matched by parameter name * @param userService * when @autowired is annotated on a method: */ @autoWired public void createUserSerive(UserService UserService){ this.userService=userService; }}Copy the code

6. AutoWired annotations @autowired, @Resource

In addition to the @AutoWired annotation, you can also use the @Resource annotation when using autowiring. The difference is as follows:

A, @autoWired: is the annotation provided in Spring, @Resource: is the annotation defined in the JDK, relying on the Java standard

By default, @autoWired is assembled by type. By default, the dependent object must exist. @Resource is matched by name by default, and the name attribute can be specified.

C, @autoWired only works with the Spring framework, while @Resource scales better

D. @autoWired should be used with @qualifier if it needs to be matched by name