Spring boot Java原型ArrayList
我正在尝试使用持有ArrayList的JavaSpringBootDO原型模式。我想实现有ArrayList,我可以从任何地方访问,修改它。我得到了什么结果。每次都是空的ArrayListSpring boot Java原型ArrayList,java,spring,spring-boot,design-patterns,Java,Spring,Spring Boot,Design Patterns,我正在尝试使用持有ArrayList的JavaSpringBootDO原型模式。我想实现有ArrayList,我可以从任何地方访问,修改它。我得到了什么结果。每次都是空的ArrayList @Component @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE,proxyMode = ScopedProxyMode.TARGET_CLASS) public class PrototypeBean { private List
@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE,proxyMode = ScopedProxyMode.TARGET_CLASS)
public class PrototypeBean {
private List<Long> listLong = new ArrayList<>();
public List<Long> getListLong() {
return listLong;
}
public void setListLong(List<Long> listLong) {
this.listLong = listLong;
}
public PrototypeBean() {}
public void printData() {
System.out.println("List size: " + listLong.size());
}
}
@组件
@范围(值=ConfigurableBeanFactory.Scope\u原型,proxyMode=ScopedProxyMode.TARGET\u类)
公共类原型bean{
private List listLong=new ArrayList();
公共列表getListLong(){
返回列表长;
}
公共void setListLong(列表listLong){
this.listLong=listLong;
}
公共PrototypeBean(){}
public void printData(){
System.out.println(“列表大小:+listLong.size());
}
}
我怎么用这个
class Calling {
@Lookup
public PrototypeBean get(){
return null;
}
private void buildList(){
List<Long> a = new ArrayList<>();
a.add(1L);
a.add(2L);
get().setListLong(a);
get().setListLong(a)
System.out.println(get().getListLong());
}
}
类调用{
@查找
公共PrototypeBean get(){
返回null;
}
私有void buildList(){
列表a=新的ArrayList();
a、 添加(1L);
a、 添加(2L);
get().setListLong(a);
get().setListLong(a)
System.out.println(get().getListLong());
}
}
我还试着从别人的班级列表
类构建列表{
@Lookup
public PrototypeBean get(){
return null;
}
private void checkList(){
List<Long> a = new ArrayList<>();
a.add(1L);
a.add(2L);
get().setListLong(a);
get().setListLong(a)
System.out.println(get().getListLong());
}
@查找
公共PrototypeBean get(){
返回null;
}
私人无效清单(){
列表a=新的ArrayList();
a、 添加(1L);
a、 添加(2L);
get().setListLong(a);
get().setListLong(a)
System.out.println(get().getListLong());
}
每次请求时,我的列表大小始终为空。我希望得到什么样的答案?我需要在同一请求呼叫中从任何地方修改列表。尝试以下方法之一:
@Component
@org.springframework.web.context.annotation.RequestScope
public class YourBean {
...
}
或
@Scope(value=“request”)
prototype
scope在被访问时总是返回一个新实例,这就是为什么您的列表总是空的。总是通过新请求调用返回新实例,但每个请求的整个生命周期都是相同的。这就是为什么您应该使用bean scoperequest
,而不是prototype
,谢谢,我会尝试。我有点迷路了
@Component
@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class YourBean {
...
}