<bean id="userService" class="com.example.Service.Impl.UserServiceImpl" scope="singleton">
- ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
- Object userService1 = context.getBean("userService");
- Object userService2 = context.getBean("userService");
- Object userService3 = context.getBean("userService");
- System.out.println(userService1);
- System.out.println(userService2);
- System.out.println(userService3);
运行结果如下:

当scope=prototype时
"userService" name="aaa,bbb" class="com.example.Service.Impl.UserServiceImpl" scope="prototype">
- ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
- Object userService1 = context.getBean("userService");
- Object userService2 = context.getBean("userService");
- Object userService3 = context.getBean("userService");
- System.out.println(userService1);
- System.out.println(userService2);
- System.out.println(userService3);
运行结果

PS:如果添加了SpringWebMVC依赖,scope的值就有多个
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-webmvcartifactId>
- <version>5.3.7version>
- dependency>
