首页 \ 问答 \ Spring @Service vs Object Service(Spring @Service vs Object Service)

Spring @Service vs Object Service(Spring @Service vs Object Service)

我在春天有一个关于@Service的问题,但我没有找到任何关于它的回复。

情况:

我有一个使用spring的@RestController的Web应用程序

现在,对于我的服务层,我在一些项目中看到了两种处理方式

  • 服务类上的@Service和控制器类上的@Autowired (如果我们更改范围,创建一个单例的bean除外)

  • 创建一个像服务一样的对象

    MyService服务=新的MySerivce()

所以我的问题是:

  • 每次为控制器的每次调用创建一个对象都不会成为内存的问题? 如果我创建一个负载测试(使用Apache Jmeter)并发送1000个请求,它将创建1000个对象我的服务,所以可能是一个问题吗?

  • 使用@Service创建单例不会成为内存的问题,但是,例如,spring将如何在1秒内处理1000个请求。 他会在某种队列上推送请求并一次执行一个吗?

  • 服务声明的最佳做法是什么?为什么?

如有任何回复,请提前感谢


I have a question about @Service in spring, but i didn't find any response about it.

Situation :

I have a web application with @RestController using spring

Now for my service layer, I saw on some project two way of processing

  • @Service on service class and @Autowired on controller class (Create a bean that is a singleton excepted if we change the scope)

  • Create a object like a service

    MyService service = new MySerivce()

So my questions are :

  • Create a object each time for each call of controller will not be a issue for memory ? If i create a load test (with Apache Jmeter) and send 1000 requests, it will create 1000 object my service so could be a problem no ?

  • Create a singleton with @Service will not be a issue for memory but, how spring will handle 1000 requests on 1 seconds for example. Will he push requests on a sort of queue and execute one at a time ?

  • What is the best practice for Service declaration and why ?

Thank in advance for any response


原文:https://stackoverflow.com/questions/50713088
更新时间:2023-07-25 06:07

最满意答案

你不必为此使用Raphaël,你可以使用vanilla JS(如果加载那个库也可以使用jQuery)。

el.mouseover(function () {
    document.getElementById('myHoverContents').style.display = 'block'; //show the div
    this.toFront();
    this.attr({
        cursor: 'pointer',
        fill: 'black',
        stroke: '#fff',
            'stroke-width': '2',

    });
    this.animate({
        scale: '1.2'
    }, 200);
});
el.mouseout(function () {
    document.getElementById('myHoverContents').style.display = 'none'; //hide the div
    this.animate({
        scale: '1.05'
    }, 200);
    this.attr({
        fill: '#003366'
    });
});

http://jsfiddle.net/b3vLx8uh/3/


You don't have to use Raphaël for this, you can just use vanilla JS (or jQuery if you load that library too).

el.mouseover(function () {
    document.getElementById('myHoverContents').style.display = 'block'; //show the div
    this.toFront();
    this.attr({
        cursor: 'pointer',
        fill: 'black',
        stroke: '#fff',
            'stroke-width': '2',

    });
    this.animate({
        scale: '1.2'
    }, 200);
});
el.mouseout(function () {
    document.getElementById('myHoverContents').style.display = 'none'; //hide the div
    this.animate({
        scale: '1.05'
    }, 200);
    this.attr({
        fill: '#003366'
    });
});

http://jsfiddle.net/b3vLx8uh/3/

相关问答

更多