Java springbean属性持久化
我有一个普通的POJO在Spring中自动连接,它的属性似乎持续存在 我发现快乐路径是OK的-设置bean属性并返回,但是当我不在快乐路径上并且我不再希望设置属性(在本例中为responseCode)时,我发现它仍然设置为以前的值(当调用成功时) 我希望此值不被设置,并且等于我当前在模型中指定的值 我有以下POJO原型beanJava springbean属性持久化,java,spring,spring-mvc,Java,Spring,Spring Mvc,我有一个普通的POJO在Spring中自动连接,它的属性似乎持续存在 我发现快乐路径是OK的-设置bean属性并返回,但是当我不在快乐路径上并且我不再希望设置属性(在本例中为responseCode)时,我发现它仍然设置为以前的值(当调用成功时) 我希望此值不被设置,并且等于我当前在模型中指定的值 我有以下POJO原型bean package com.uk.jacob.model; import org.springframework.context.annotation.Scope; imp
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;
}
我正在服务类中设置它的信息
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;
}
}
它是从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);
}
}
您的
网站
bean是@Scope(“prototype”)
这一事实意味着每次在创建另一个bean时将其作为依赖项注入另一个bean,就会创建并注入一个新实例。在本例中,PingerService
获取网站的新实例。如果sayWebsite
也被注入到另一个名为Website2
的bean中,那么就会注入一个不同的(新)实例
如果您的预期是网站
在网站
中的每次调用都是新的,那么这不能简单地用原型注释来完成。您需要公开上下文并调用ApplicationContext#getBean(“网站”)
您的网站bean是@Scope(“prototype”)
这一事实意味着每次创建这个bean时,它作为依赖项注入另一个bean时,都会创建并注入一个新实例。在本例中,PingerService
获取网站的新实例。如果sayWebsite
也被注入到另一个名为Website2
的bean中,那么就会注入一个不同的(新)实例
如果您的预期是网站
在网站
中的每次调用都是新的,那么这不能简单地用原型注释来完成。您需要公开上下文并调用ApplicationContext#getBean(“网站”)
对于您的用例,我知道您需要为每个请求提供一个新的WebsiteBean实例
使用@Scope(“请求”)
另一方面,Prototype范围意味着它将为它在任何地方看到的每个网站的自动连接创建一个单独的实例。例如,PingerService将拥有自己的WebsiteBean,并且不会在具有相同自动连接的其他类上共享,但其值将在PingerService上的http请求中保持不变。对于您的用例,我知道您需要为每个请求提供一个新的WebsiteBean实例
使用@Scope(“请求”)
另一方面,Prototype范围意味着它将为它在任何地方看到的每个网站的自动连接创建一个单独的实例。例如,PingerService将拥有自己的网站bean,并且不会在具有相同自动连接的其他类上共享,但其值将在PingerService上的http请求中保持不变。您能否解释更多关于ApplicationContext\getBean(“网站”)?
,它是什么?我的代码中有什么问题?你能解释更多关于ApplicationContext\getBean(“网站”)的信息吗?
,是什么问题?我的代码中有什么问题?