spring用于处理解耦的问题。
1、下载地址:
https://repo.spring.io/webapp/#/artifacts/browse/tree/General/libs-release-local/org/springframework/spring
打开以上链接,选择需要下载的版本:
2、点开该版本对应的文件夹,选择第一个,即所需的安装包。
3、点击右侧的下载按钮下载:
4、下载成功。在libs子文件夹中,无任何后缀的即我们要导入的包,javadoc后缀表示文档,source后缀表示源码。
docs子文件夹中,第三个子文件夹表示开发文档。
5、添加依赖。打开pom.xml文件,添加如下内容:
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
</dependencies>
添加依赖后点击界面中出现的这个按钮,包就会被自动导入成功:
6、在src->main->resources目录下新建一个文件:
将以下内容复制到新文件中:
<?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">
</beans>
该段内容在上面提到的开发文档文件夹中core.xml文件当中。
7、将对象的创建交给spring来管理。在第六步所建立的文件中,写入:
<bean id="唯一标识" class="全限定类名"></bean>
8、在java类当中获取spring的IOC核心容器,并根据id来获取对象。
//1、获取核心容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2、根据id获取bean对象
类名 对象名 = (类名)对象名.getBean("唯一标识id");