新建maven项目
导入依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
Scope.java
package com.shrimpking.scope;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2023/7/5 11:14
*/
public class Scope
{
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="scope" class="com.shrimpking.scope.Scope" scope="prototype"/>
</beans>
ScopeTest.java
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2023/7/5 11:15
*/
public class ScopeTest
{
@Test
public void test()
{
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println(context.getBean("scope"));
System.out.println(context.getBean("scope"));
}
}