搭建Spring开发环境

前言

下一篇:Spring框架前言

Spring开发所需JAR包

1.Spring框架包

下载地址:https://maven.springframework.org/release/org/springframework/spring/5.2.2.RELEASE/
在这里插入图片描述
下载完成后,将下载后的压缩包解压缩在自定义的文件夹中,结构目录如下:
在这里插入图片描述
libs目录下可以看到60多个JAR文件
在这里插入图片描述
libs包下JAR包分类

1.以.RELEASE.jar结尾的是class文件JAR包
2.以.RELEASE-javadoc.jar结尾的是API文档压缩包
3.以.RELEASE-sources.jar结尾的是源文件压缩包
Spring为每个模块都提供了这三类JAR包

libs包下有四个Spring的基础包,分别对应Spring核心容器的四个模块

  1. spring-beans-5.2.2.RELEASE.jar:所有应用都要用到的JAR包,它包含访问配置文件、创建和管理Bean以及进行控制反转或者依赖注入操作相关的所有类。
  2. spring-context-5.2.2.RELEASE.jar:提供了在基础IoC功能上的扩展服务,还提供了许多企业级服务的支持。
  3. spring-core-5.2.2.RELEASE.jar:包含Spring框架的核心工具类,Spring其它组件都要用到这个包里的类。
  4. spring-expression-5.2.2.RELEASE.jar:定义了Spring的表达式语言。

2.第三方依赖包

下载地址:https://commons.apache.org/proper/commons-logging/download_logging.cgi
在这里插入图片描述
下载完成后,将下载后的压缩包解压缩在自定义的文件夹中,结构目录如下:
在这里插入图片描述

在Eclipse搭建Spring环境

  1. 创建一个Web项目,将Spring四个基础包及commons-logging的JAR包复制到lib目录下,并发布到类路径下
    在这里插入图片描述
  2. 在src目录下,创建一个cn.js.ccit.ioc包,并在包中创建接口UserDao,在接口中定义一个print()方法。
public interface UserDao {
     public void print();
}
  1. 在cn.js.ccit.ioc包下,创建UserDao接口的实现类UserDaoImpl,该类需要实现接口中的print()方法,并在方法中编写一条输出语句。
public class UserDaoImpl implements UserDao{

	@Override
	public void print() {
		// TODO Auto-generated method stub
		System.out.println("我是沐晨,软件精英1912班学生");
	}

}
  1. 在src目录下,创建Spring的配置文件applicationContext.xml,并在配置文件中创建一个id为userDao的Bean。
<?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"> 
             <!-- 将指定类配置给Spring,让Spring创建其对象实例 -->
             <bean id="userDao" class="cn.js.ccit.ioc.UserDaoImpl" />
       </beans>
  1. 在cn.js.ccit.ioc包下,创建测试类TestIoC,并在类中编写main()方法。在main()方法中,需要初始化Spring容器,并加载配置文件,然后通过Spring容器获取userDao实例(即Java对象),最后调用实例中的print()方法。
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestIoc {
	public static void main(String[] args) {
		//初始化Spring容器,加载配置文件
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		//通过容器获取userDao实例
		UserDao userDao = (UserDao) applicationContext.getBean("userDao");
		//调用实例中的方法
		userDao.print();

	}
}

6.运行结果:

我是沐晨,软件精英1912班学生

总结

下一篇:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值