0

I need to implement servlet 3.0 webapp without dependency injection framework. Which solution is the best practice ? (in terms of performance/memory usage and scalability)

1) static methods for DAO and service layer

@WebServlet("/welcome")
public class MyController extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException {

        MyService.doSomething(/*args*/);
    }
}

public class MyService {

    public static void doSomething(/*args*/) {
        MyDAO.crud(/*args*/);
    }
}

public class MyDAO {

    public static void crud(/*args*/) {
        //...
    }
}

2) interfaces

@WebServlet("/welcome")
public class MyController extends HttpServlet {

    private MyService myService;

    public MyController() {
        super();
        if (myService != null) {
            myService = new MyServiceImpl();
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException {

        myService.doSomething(/*args*/);
    }
}

public interface MyService {

    void doSomething(/*args*/);
}

public class MyServiceImpl implements MyService {

    private MyDAO myDAO;

    public MyServiceImpl() {
        if (myService != null) {
            myDAO = new MyDAOImpl();
        }
    }

    @Override
    public void doSomething(/*args*/) {
        myDAO.crud(/*args*/);
    }
}

public interface MyDAO {

    void crud(/*args*/);
}

public class MyDAOImpl implements MyDAO {

    @Override
    public void crud(/*args*/) {
        //...
    }
}

3) something else...

Thanks.

tty
  • 314
  • 1
  • 2
  • 8

3 Answers3

1

Its best to make both MyService and MyDAO in your application as stateless. You can make both of them as singletons (which is any way the default scope of all spring injected beans), and just use it in your controller. Since its stateless there is no problem with concurrency issues.

Sajan Chandran
  • 11,287
  • 3
  • 29
  • 38
0

Don't use a static method as it can't be mocked or injected. Although assuming you dao is not in memory (and is not itself stateful) it would be threadsafe.

Why do you have to do it without a framework ?

NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
  • The main requirement is that the application must be lightweight. But i can't find a simple, stable and lightweight IoC container. Spring MVC and EJB is too big. – tty Jun 23 '14 at 10:29
  • there are others, guice, pico ? see this for statics http://stackoverflow.com/questions/5173399/are-non-synchronised-static-methods-thread-safe-if-they-dont-modify-static-clas – NimChimpsky Jun 23 '14 at 10:30
  • guice have memory leak http://stackoverflow.com/questions/8842256/guice-3-0-tomcat-7-0-classloader-memory-leak pico not updated since 2011 – tty Jun 23 '14 at 10:41
  • @user3766891: what is your definition of 'lightweight' - no frameworks at all? – home Jun 23 '14 at 11:47
  • 1 - low memory usage, 2 - kiss, 3 - scalability – tty Jun 23 '14 at 12:35
  • @user3766891 whats wrong with pico then according to 1,2,3 ? Or guice for that matter, that erro rhardly looks like a showstopper – NimChimpsky Jun 23 '14 at 12:41
  • pico looks ok. but it's not maintained since quite a long time... is pico enterprise-ready ? – tty Jun 23 '14 at 15:20
0

See if you want to implement singleton than create a

class public class FetchBean {

static Map<String, Object> beans;

static {
    beans = new HashMap<String, Object>();
}

public static Object getBean(Class bean) {
    try {            
        if (!beans.containsKey(bean.getName())) {
            Object instance = bean.newInstance();                
            beans.put(bean.getName(), instance);
        }
        return beans.get(bean.getName());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

public static void main(String[] args) {
    Person person = (Person) FetchBean.getBean(Person.class);
    person.setName("Taher");
    System.out.println(person.getName());
    Person person2 = (Person) FetchBean.getBean(Person.class);
    person2.setName("Tinwala");
    System.out.println(person2.getName());
} }
public class Person {

    public Person() {
        System.out.println("In person");
    }

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}
TaherT
  • 1,285
  • 2
  • 22
  • 41