1

I have a legacy application that uses a singleton pattern for all the services and accesses all the services via ServiceName.getInstance() inside services that use other services and in the web tier. I'm converting the project to use Spring and was thinking of creating a singleton utility class ServiceProvider with methods getServiceA, getServiceB..etc and have it get the bean from a Spring application context. I will use the ServiceProvider in the web tier only since I can't convert to it to use Spring just yet and autowire all the services that use other services. Is this a good solution?

I have a very simple web tier and maybe someone can recommend how to springify it with minimal changes. I have a map of url to controllers loaded on start up. The RequestDispatcher parses the request url, looks up the controller by class and executes a template method (there are various subclasses of the base controller but it doesn't complicate the problem).

RequestDispatcher:

protected void service(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {


        int slashIndex = req.getRequestURL().lastIndexOf("/");
        String path = req.getRequestURL().substring(slashIndex, req.getRequestURL().length());


        ServiceURL url = urlMap.get(path);

        ServiceProvider.getLog().info("Requested page: " + path);
        if (url != null) {

            try {
                Utils.authenticate(req, resp);
                grantAccess(req, url);
                BaseServlet servlet = (BaseServlet)url.getClass().getClassLoader().loadClass(url.getClassName()).newInstance();

                servlet.service(req, resp);
            } 
            catch (AuthorizationException e) {

                resp.getWriter().write(new ErrorModel("You are not authorized to perform the requested action.").getContent());
                ServiceProvider.getAuthLog().info("auth", e);
            }catch (SystemException e) {

I'm thinking of annotating my servlets as components, having the packages auto scanned. Can the ApplicationContext get the bean by full class name?

2
  • Why do you think it is a good solution or otherwise ?
    – AllTooSir
    May 16, 2013 at 16:05
  • What are the alternatives?
    – user979051
    May 16, 2013 at 16:19

2 Answers 2

2

Looks like your services are stateless. I would annotate them with @Service(make them spring beans) and just @Autowire them anywhere you need. Let Spring act as a service provider.

3
  • How can I autowire them in my web layer when I'm dealing with a legacy in house built web framework not under spring management? If I would be using Spring Web it would be simpler since the mvc controllers would be spring managed
    – user979051
    May 16, 2013 at 18:34
  • What are you trying to gain by converting to Spring? If you won't convert your web tier to Spring MVC then yes you can just create a factory like class which gets instances from the application context by their name. But still if you don't code to service interfaces you don't get much, it'll be the same as your current implementation, just instances created by Spring.
    – braincell
    May 16, 2013 at 18:51
  • I am planning to extract the interfaces. I'm looking to gain the ease of changing my execution environment via configuration. For one, test implementation for the service layer (unit testing / mock).
    – user979051
    May 16, 2013 at 19:02
0

Your getInstance() solution sounds like the objects are not unnder Spring's control.

If you need to access the services as JNDI lookups you should configure them as such in Spring.

If it's under Spring's control it should not be instantiated in your code. If it's instantiated in your code, it's not under Spring's control.

7
  • Currently they are not under Spring control that is what I'm trying to change. The ServiceProvider.getServiceA() would get the service from an ApplicationContext (it would be spring managed).
    – user979051
    May 16, 2013 at 18:36
  • I don't see why you'd do that. Just inject it into the object that needs it. Have you used Spring before?
    – duffymo
    May 16, 2013 at 18:37
  • I'm a newbie to Spring but how can I inject anything in my web tier (in house built in house mvc framework) when my controllers are not spring beans? I can have Spring wire everything up to the service layer only
    – user979051
    May 16, 2013 at 18:43
  • Your controllers should be Spring beans.
    – duffymo
    May 16, 2013 at 18:49
  • In-house web MVC? Are you mad? You can't throw a rock without hitting a Java web MVC framework, yet your company still wrote an in-house version? That's crazy.
    – duffymo
    May 16, 2013 at 18:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.