views:

130

answers:

3

Can or should a Service layer be a Spring bean?

If so, how should it be got from a calling application, a consumer of a service?

Because the consumer must be aware that such a bean exists, so it in any case must use Spring

to make use of Service methods.

+2  A: 

Of course. The service layer is the part of your application that is visible to other users (e.g. a Web layer) so it needs to be configured and setup somewhere. Imho a Spring configuration is the best place to put this configuration in. The Service Layer user then has to take care of instantiating that context and getting the Service Objects he needs.

An alternative - if it needs to run standalone - would be for your service class(es) implementing the Service Layer interface(s) to instantiate the Spring application context themselves.

Daff
+2  A: 

By making your consumers also spring beans, and inject the service bean with dependency injection.

nos
+1  A: 

Yes, It is always nice to configure service beans as spring beans. In the web layer you need to take care of instantiating the needed service objects. Another option is to make the web layer classes also as spring beans and inject the necessary service layer spring beans. From the testing point of view also, this type of design is very helpful when we use Spring testing framework.