I have been working on few web applications/REST web services recently (Spring IoC/MVC/Data JPA etc) and they usually follow the same pattern: Controller classes --> Service classes (which have number of "utility"/business logic classes autowired) --> Spring Data Repositories.
Pretty much all of them classes above are Spring singletons, which sometimes I feel like it makes the code and some functions within a class dirtier (for example because I cant have a state in a class, I need to pass quite a lot parameters between methods, and I dont really like having more than 1-2 parameters, although I know sometimes its necessary).
I was wondering how is this problem overcome in the big (e.g. enterprise) kind of application.
- Is it a common practice to use non-Spring managed classes in the Spring application? If so, how do you pass dependencies into it (the ones that would normally be autowired)? If I use constructor injection for example, then I need to autowire all necessary dependencies into the class that creates the object and I wanted to avoid that. Also I dont really want to be messing with load time weaving etc. to autowire beans into non-Spring objects
- Is using prototype scoped beans a good solution? The only thing is that I need to use AOP's scoped proxies (or method injection etc) to make sure that I get a new instance for any bean thats autowired into singleton in the first place. Is that a "clean" and reliable option - i.e. is it certain that there will be no concurrency type of issues and can I still autowire any singletons into those classes with no issues?
If anyone that worked on a large system, that actually managed to keep the structure not "bloated" and clean have any recommendations? Maybe there are some patterns I am not aware and could use?
Any help appreciated.