1.创建一个空工程,new–project–empty project
2.new moudle–maven,配置jdk,选择模块quickstart
3.一路next,finish,就会开始自动导入,这是要耐心等一会
这时如果中途被打断,刷新maven就可以解决,下图为刷新maven
点击compile就可以解决。
4.在main文件目录下添加resources文件并mark as resources file
5.修改pom.xml
1)修改jdk版本,我这里是使用的 jdk1.8
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
2)加入maven依赖,spring依赖,junit依赖
spring依赖:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
unit依赖:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
6.创建接口和实现类
1)创建一个接口并定义方法
2)创建这个接口的实现类并重写方法
7.在resources文件下创建spring配置文件
8.创建spring的主配置文件
<!--spring把java对象转为beans对象-->
<!--声明bean,就是告诉spring要创建的对象
id:随意定义
class:自定义实现类的全限定名称
-->
<beans>
<bean id="" class="" />
</beans>
9.测试spring创建的对象。
//1.创建表示spring容器的对象AppliCationContext接口的对象
//ApplicationContext就是表示spring容器,通过容器获取对象
ApplicationContext ac=new ClassPathXmlApplicationContext();
/*
ApplicationContext接口的实现类是ClassPathXmlApplicationContext
ClassPathXmlApplicationContext表示加载spring的配置文件
*/
//2.调用getBean方法获取对象
//getBean("配置文件中bean的id值");
ac.getBean("oneService");