spring默认是单例模式
import java.util.HashSet;
import java.util.Set;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping(value = "/spring")
@Scope("prototype")
public class SpringDemo {
private static Set<Object> set = new HashSet<>();
@RequestMapping(value="test")
public void test(){
set.add(this);
System.out.println(set.size());
}
}
websocket 注解@ServerEndpoint("/mysocket")是原型模式

本文通过一个具体的示例,展示了Spring框架中如何使用@Scope注解来改变默认的单例模式为原型模式,并介绍了WebSocket中@ServerEndpoint注解的应用场景。通过对SpringDemo类的解析,揭示了不同作用域下实例创建的机制。
1327

被折叠的 条评论
为什么被折叠?



