简体   繁体   English

Spring bean属性持久存在

[英]Spring bean properties persisting

I have a plain POJO being autowired in Spring whose properties appear to persist. 我有一个普通的POJO在Spring中自动装配,其属性似乎仍然存在。

I find that the happy path is OK - Set bean properties and return then, however when I'm not on the happy path and I no longer wish to set a property (in this case responseCode), I find it is still set to the previous value (when a call was successful). 我发现幸福的路径是正确的 - 设置bean属性然后返回,但是当我不在快乐的路径而我不再希望设置属性(在这种情况下是responseCode)时,我发现它仍然设置为上一个值(当呼叫成功时)。

I would like this value to not be set and be equal to what I have currently specified in the model. 我希望这个值不被设置,并且等于我目前在模型中指定的值。

I have the following POJO Prototype bean 我有以下POJO Prototype bean

package com.uk.jacob.model;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope("prototype")
public class Website {
    public String url;
    public boolean response;
    public int responseCode = 0;
}

I am setting it's information within a service class 我在服务类中设置它的信息

package com.uk.jacob.service;

import java.net.HttpURLConnection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.uk.jacob.adapter.HttpAdapter;
import com.uk.jacob.model.Website;

@Component
public class PingerService {

    @Autowired
    HttpAdapter httpAdapter;

    @Autowired
    Website website;

    public Website ping(String urlToPing) { 
        website.url = urlToPing;

        try {
            HttpURLConnection connection = httpAdapter.createHttpURLConnection(urlToPing);

            website.response = true;
            website.responseCode = connection.getResponseCode();
        } catch (Exception e) {
            website.response = false;
        }

        return website;
    }
}

Which is called from a RestController 这是从RestController调用的

package com.uk.jacob.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.uk.jacob.model.Website;
import com.uk.jacob.service.PingerService;

@RestController
@RequestMapping("api/v1/")
public class PingController {

    @Autowired
    PingerService pingerService;

    @RequestMapping(value = "ping", method = RequestMethod.GET)
    public Website getPing(@RequestParam String url){
        return pingerService.ping(url);
    }

}

The fact that your Website bean is @Scope("prototype") means that every time that it gets injected as a dependency in another bean upon this bean's creation, a new instance gets created and injected. 您的Website bean是@Scope("prototype")这一事实意味着每次在创建此bean时将其作为依赖项注入另一个bean中,就会创建并注入新实例。 In this case PingerService gets a new instance of Website . 在这种情况下, PingerService获取一个新的Website实例。 If say Website is also injected on another bean called Website2 then a different (new) instance gets injected. 如果说在另一个名为Website2 bean上注入了Website则会注入另一个(新)实例。

If your anticipation is Website to be new upon each invocation within Website then this cannot be done simply with the prototype annotation. 如果您的预期是WebsiteWebsite内的每次调用时都是新的,那么这不能简单地使用原型注释完成。 You'll need to expose the context and invoke ApplicationContext#getBean("website") . 您需要公开上下文并调用ApplicationContext#getBean("website")

For your use case, I understand that you need a fresh instance of Website bean for every request. 对于您的用例,我了解您需要为每个请求提供一个新的Website bean实例。

Use @Scope("request"). 使用@Scope(“请求”)。

Prototype scope on the other hand means it will be creating a separate instance for every Autowiring of Website it sees everywhere. 另一方面,原型范围意味着它将为它在任何地方看到的每个网站自动装配创建一个单独的实例。 For example, PingerService will have its own Website bean and won't be shared on other classes with the same Autowiring but its values will persist across http requests on PingerService. 例如,PingerService将拥有自己的网站bean,并且不会在具有相同自动装配的其他类上共享,但其值将在PingerService上的http请求中保持不变。

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

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