Shweta Priyadarshani
Shweta Priyadarshani

Reputation: 258

Standard Scope for Spring classes

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?

Upvotes: 0

Views: 1044

Answers (2)

Ken Chan
Ken Chan

Reputation: 90457

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.

Upvotes: 2

asgarov1
asgarov1

Reputation: 4098

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.

Upvotes: 2

Related Questions