基本包坐标可以帮助开发者指定需要使用的Spring版本和基本模块,而包是实际提供Spring框架功能和特性的实体。
基本包坐标用于依赖管理,指定框架版本和模块,而包用于实际引入框架的功能和特性。
Spring程序开发步骤
- 导入Spring的基本包坐标
- 编写Dao接口和实现类
- 创建Spring核心配置文件
- 在Spring配置文件中配置UserDaolmpl
- 使用Spring的API获得Bean实例
导入坐标
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.29</version>
<scope>test</scope>
</dependency>
编写Dao接口和实现类
接口
public interface UserDao {
public void size();
}
实现类
public class UserDaolmpl implements UserDao {
public void size(){
System.out.println("The run was successful");
}
}
创建Spring核心配置文件
<bean id="userDao" class="com.it.dao.impl.UserDaolmpl"></bean>
定义测试类
public static void main(String[] args) {
ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("ApplicationContext.xml");
UserServcie bean = app.getBean(UserServcie.class);
bean.size();
}
注:在resources中创建Spring Config文件中配置Bean