简体   繁体   English

设计模式-不带DI框架的Webapp

[英]Design patterns - webapp without DI framework

I need to implement servlet 3.0 webapp without dependency injection framework. 我需要在没有依赖项注入框架的情况下实现servlet 3.0 webapp。 Which solution is the best practice ? 哪种解决方案是最好的做法是什么? (in terms of performance/memory usage and scalability) (就性能/内存使用情况和可伸缩性而言)

1) static methods for DAO and service layer 1)DAO和服务层的静态方法

@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 2)接口

@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... 3)其他...

Thanks. 谢谢。

Its best to make both MyService and MyDAO in your application as stateless . 最好使application MyServiceMyDAO都为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 . 您可以将它们都设置为singletons (这是所有spring注入的beansdefault范围),只需在controller使用它即可。 Since its stateless there is no problem with concurrency issues. 由于它是无stateless因此concurrency问题没有问题。

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. 尽管假设dao不在内存中(并且本身不是有状态的),但它是线程安全的。

Why do you have to do it without a framework ? 为什么没有框架就必须这样做?

See if you want to implement singleton than create a 查看是否要实现单例而不是创建一个

class public class FetchBean { 类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;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM