代码之家  ›  专栏  ›  技术社区  ›  Vicky Thakor

在Spring中为每个HTTP请求清除/初始化列表(集合)

  •  1
  • Vicky Thakor  · 技术社区  · 8 年前

    我在Java上工作了4年多,但对Spring来说还是新手。我面临初始化空白的问题 List 对于每一个人 PUT

    enter image description here

    • public abstract class BaseX implements InterfaceX
    • public class DefaultX extends BaseX
    • public class DefaultXAndY extends DefaultX

    问题:

    在课堂上使用 BaseX 要求代码如下。

    InterfaceX。Java语言

    public interface InterfaceX {
        public void process(Long id);
        public void publish();
    }
    

    BaseX。Java语言

    import java.util.ArrayList;
    import java.util.List;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public abstract class BaseX implements InterfaceX{
        private List<Long> listLong = new ArrayList<Long>();
    
        public void addToList(Long id){
            System.out.println("Here in BaseX");
            listLong.add(id);
        }
    
        @Override
        public void publish() {
            System.out.println("Print list values");
            listLong.stream().forEach(System.out::println);
        }
    }
    

    默认X。Java语言

    import javax.annotation.Resource;
    
    import org.springframework.stereotype.Component;
    
    @Component
    @Resource(name = "defaultX")
    public class DefaultX extends BaseX{
    
        @Override
        public void process(Long id) {
            //business logic
            System.out.println("Here in DefaultX");
            addToList(id);
        }
    }
    

    import javax.annotation.Resource;
    import org.springframework.stereotype.Component;
    
    @Component
    @Resource(name = "defaultXAndY")
    public class DefaultXAndY extends DefaultX{
        @Override
        public void process(Long id) {
            //Business logic different than X
            System.out.println("Here in DefaultXAndY");
            id = id + 10;
            super.process(id);
        }
    }
    

    测试服务。Java语言

    public interface TestService {
        public void testServiceMethod(Long id);
    }
    

    TestServiceImpl

    import javax.annotation.Resource;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import com.orderhive.inventory.service.TestService;
    import com.orderhive.inventory.stock.InterfaceX;
    
    @Service
    public class TestServiceImpl implements TestService{
    
        @Autowired
        @Resource(name = "defaultXAndY")
        private InterfaceX interfaceX; 
    
        @Override
        public void testServiceMethod(Long id) {
            interfaceX.process(id);
            interfaceX.publish();
            System.out.println("API call finished for id: " + id);
            System.out.println("--------------------------------");
        }
    }
    

    TestRestController

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.orderhive.inventory.service.TestService;
    
    @RestController
    @RequestMapping("/test")
    public class TestRestController {
    
        @Autowired
        private TestService testService; 
    
        @RequestMapping(method = RequestMethod.PUT, value = "/number/{id}")
        void stockUpdate(@PathVariable Long id){
            testService.testServiceMethod(id);
        }
    }
    

    输出

    **PUT: localhost:8080/test/number/1**
    
    Here in DefaultXAndY
    Here in DefaultX
    Here in BaseX
    Print list values
    11
    API call finished for id: 1
    --------------------------------
    **PUT: localhost:8080/test/number/2**
    
    Here in DefaultXAndY
    Here in DefaultX
    Here in BaseX
    Print list values
    11
    12
    API call finished for id: 2
    --------------------------------
    

    列表

    ===========================================================

    以下改变对我来说很有效,但这是最佳做法吗?

    • 远离的 @Component 从…起 BaseX公司
    • @Scope("request") 添加到 DefaultX DefaultXAndY
    • @Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS) 添加到 TestServiceImpl 由@anatoly shamov建议

    解决方案:

    • 远离的 @组件 从…起 BaseX公司
    • 添加到 默认X DefaultXAndY
    1 回复  |  直到 8 年前
        1
  •  1
  •   Anatoly Shamov Fildor    8 年前

    BaseX 是一个豆子 singleton scope (默认情况下)。这意味着只有一个 BaseX公司 你应该抽象 listLong 用单独的bean表示 request scope . 将为每个HTTP请求创建该bean的新实例。

    @Component
    @Scope(value = "request", proxyMode= ScopedProxyMode.TARGET_CLASS)
    public class ListX {
        private List<Long> listLong = new ArrayList<Long>();
    
        public List<Long> getListLong() {
            return listLong;
        }
    
        public void setListLong(List<Long> listLong) {
            this.listLong = listLong;
        }
    }
    

    在as中使用 listLong公司

    @Component
    public abstract class BaseX implements InterfaceX{
    
        @Autowired
        ListX listHolder;
    
        public void addToList(Long id){
            System.out.println("Here in BaseX");
            listHolder.getListLong().add(id);
        }
    
        @Override
        public void publish() {
            System.out.println("Print list values");
            listHolder.getListLong().stream().forEach(System.out::println);
        }
    }
    
    推荐文章