spring相关配置及案例展示

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");
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值