Standard Scope for Spring classes
Asked Answered
L

2

0

In a spring MVC app , by default all beans are singleton ,but what should be the standard scopes for below classes according to good programming practices:

1.DAO classes

2.Controller classes

3.DTO classes

4.Service classes

I have read that DAO and Controller classes should be singleton scoped and DTO classes should not be beans so not annotated, whenever required, DTO classes should be instantiated using "new".

What will be the scope of @Service classes ?

And Which classes will have the Request and Session scopes if none of the above classes are created in these 2 scopes?

Libertinage answered 4/2, 2022 at 22:48 Comment(0)
S
2

First of all not classes, but Spring managed beans have a scope. Difference is that you can have classes in your application that you didn't configure to be managed by Spring (So for example you didn't provide @Component annotation)

For the Spring managed beans default scope is Singleton. That means Spring container will provide the same instance everytime you ask for that bean to be autowired.

You can change that default scope with for example @Scopeannotation. So to answer your question, all of the above mentioned choices would have default scope of singleton but you could changed that to be requestor sessionscope if you would like (only applicable in web applications though). You can read more about setting scopes here.

ps. DTO classes are usually not declared to be managed by Spring - letting Spring manage a simple data transfer object doesn't make much sense.

Smetana answered 5/2, 2022 at 6:17 Comment(2)
Hi , I have edited my question, I know the default scope for all beans is singleton, my question is what is the standard for assigning scopes according to the different types of beans, and which beans are suitable to be request and session scoped in a webapplication context. What would be the best fit scope for @Service beansLibertinage
standard usecase is Singleton, sometimes it is useful to have session scope for example for shopping cart bean, so that items added to shopping cart stay there for user for the duration of their session, and it is very rare that you would need any other scope.Smetana
E
2

So basically two things to consider here. The 1st is that if a bean is required to be declared as a spring bean . It depends on if you need to use the spring features for this class such as @Transactional , @Async , @PreAuthorize , @Autowired (i.e dependency injection) , or ensure the bean has certain scope etc. If not , it is simpler not define it as a spring bean and simply create it by yourself.

So the following types of the classes are required to define them as spring bean in most cases:

  • DAO because most probably need to inject EntityManager or JdbcTemplate to it
  • Controller because it is a part of spring-mvc and you need to define it as a bean such that you can use @RequestMapping / @GetMapping / @PostMapping / @PutMapping / @DeletMapping / @PatchMapping etc. on its method.
  • Service class because you need to inject it into the controller and you need to use @Transactional to manage the DB transaction for its method.

For DTO , in most case you can create it by yourself since it is just a data container in nature and does not require to use any spring features.

The 2nd thing to consider is what scope does a bean should be. You mainly need to think about if an instance of that class is okay to be executed safely by multiple request (i.e thread) concurrently. If yes , you can simply use the default singleton scope. If not , you can think about if you want each HTTP request (i.e @RequestScope) or each HTTP session (i.e. @SessionScope) has their own instance of that class to work with. For example , if you are implementing some shopping cart , you most probably want that the HTTP session has their won instance of a shopping cart and so you should use @SessionScope for the shopping cart.

Enduring answered 5/2, 2022 at 10:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.