SpringBean common annotations

 

table of Contents:

1. Spring Bean related

1.1. @Autowired

1.2. Component,@Repository,@Service, @Controller

1.3. @RestController

1.4. @Scope

1.5. Configuration

 

 

1. Spring Bean related

1.1. @Autowired

Automatically import objects into the class, and the injected class must also be managed by the Spring container. For example, the Service class is injected into the Controller class.

@Service
public class UserService {
  ......
}

@RestController
@RequestMapping("/users")
public class UserController {
   @Autowired
   private UserService userService;
   ......
}

1.2. Component,@Repository,@Service, @Controller

We generally use the @Autowired annotation to let the Spring container automatically assemble the bean for us. If you want to identify a class as a class that can be used for @Autowired annotations for automatic assembly of beans, you can use the following annotations:

@Component: General annotation, you can mark any class as a Spring component. If a Bean does not know which layer it belongs to, it can be annotated with @Component annotation.

@Repository: Corresponding to the persistence layer, Dao layer, which is mainly used for database-related operations.

@Service: Corresponding to the service layer, which mainly involves some complex logic, and the Dao layer is needed.

@Controller: Corresponding to the Spring MVC control layer, the main user accepts user requests and calls the Service layer to return data to the front-end page.

 

1.3. @RestController

Automatically import objects into the class, and the injected class must also be managed by the Spring container. For example, the Service class is injected into the Controller class.

The @RestController annotation is a collection of @Controller and @ResponseBody, indicating that this is a controller bean, and the return value of the function is directly filled into the HTTP response body, which is a REST-style controller.

 

Brother Guide: Now it's the separation of front and back ends. To be honest, I haven't used @Controller for a long time. If your project is too old, just treat it as if I didn't say it.

 

Using @Controller alone without @ResponseBody is generally used when you want to return a view. This situation is a more traditional Spring MVC application, which corresponds to the situation where the front and back ends are not separated. @Controller +@ResponseBody returns data in JSON or XML format

1.4. @Scope

Declare the scope of Spring Bean, use method:

The scope of four common Spring Beans:

@Bean
@Scope("singleton")
public Person personSingleton() {
    return new Person();
}

singleton: the only bean instance, beans in Spring are singletons by default.

prototype: Each request will create a new bean instance.

request: Every HTTP request will generate a new bean, which is only valid in the current HTTP request.

session: Each HTTP request will generate a new bean, which is only valid in the current HTTP session.

1.5. Configuration

Generally used to declare configuration classes, you can use @Component annotation instead, but using Configuration annotations to declare configuration classes is more semantic.

@Configuration
public class AppConfig {
    @Bean
    public TransferService transferService() {
        return new TransferServiceImpl();
    }

}

 

Reference link:

https://snailclimb.gitee.io/javaguide/#/./docs/system-design/framework/spring/spring-annotations

 

other

Do you understand autowired and resource annotations, who owns Autowired (Spring)

https://www.zhihu.com/question/39356740

(1) Autowired annotation is to assemble dependent objects by type

         resource is assembled by name by default

(2)resource: JDK

         Autowired:Spring

 

What is the difference between @Component and @Bean

(1) Different targets

(2) Class path scanning

(3) More customization

 

 https://segmentfault.com/a/1190000005626375

 

 

Guess you like

Origin blog.csdn.net/qq_42198024/article/details/108346139