「spring」@程序员,如何快速配置 Spring?( 二 )


  • maven
  • idea 社区版
同时由于 spring 的 jar 包都在国外服务器 , 下载速度慢 , 所以我们使用阿里云的仓库进行加速 。
打开maven的配置文件(windows机器一般在maven安装目录的conf/settings.xml) , 在标签中添加mirror子节点插入如下代码就可以使用阿里云的镜像加速下载 jar 包了:
< mirror>
< id> aliyunmaven </ id>
< mirrorOf> * </ mirrorOf>
< name> 阿里云公共仓库 </ name>
< url> https://maven.aliyun.com/repository/public </ url>
</ mirror>
使用 idea 创建一个 maven 项目:
「spring」@程序员,如何快速配置 Spring?
本文图片

并在 pom.xml 的dependencies中添加如下内容:
< dependency>
< groupId> junit </ groupId>
< artifactId> junit </ artifactId>
< version> 4.11 </ version>
< scope> test </ scope>
</ dependency>
< dependency>
< groupId> org.springframework </ groupId>
< artifactId> spring-beans </ artifactId>
< version> 4.3.6.RELEASE </ version>
</ dependency>
< dependency>
< groupId> org.springframework </ groupId>
< artifactId> spring-beans </ artifactId>
< version> 4.3.6.RELEASE </ version>
</ dependency>
< dependency>
< groupId> org.springframework </ groupId>
< artifactId> spring-context </ artifactId>
< version> 4.3.6.RELEASE </ version>
</ dependency>
< dependency>
< groupId> org.springframework </ groupId>
< artifactId> spring-core </ artifactId>
< version> 4.3.6.RELEASE </ version>
</ dependency>
其中:
  • spring-core 包含了 spring 框架基本的核心工具类 , spring 其他组件 , 比如 spingboot、springcloud 都要用到它 。
  • spring-beans 所以应用都要用到的 jar 包 , 它包含了访问配置文件、创建和管理 bean 以及进行 Ioc和 DI 操作的相关类 。
  • spring-context 提供了基础的 IoC 功能上的扩展服务 。 还提供了远程访问、缓存、邮件服务等各种试图层框架的封装 。
spring 的核心容器
spring 框架提供了两个最基本最重要的包 org.springframework.beans.factory和 org.springframework.context 。 前者的主要接口是 BeanFactory , 后者的主要接口是 ApplicationFactory 。
IoC 框架的主要组件有 Beans、配置文件 applicationcontext.xml、Beanfactory 接口和相关类、ApplicationContext 接口和相关类 。
Beans 是项目中为业务提供功能的 Bean , 就是容器要管理的 Bean , 也就是我们场景的 JavaBean、Java 类 。
在 Spring 中 Bean 的管理是基于配置文件配置的 , 如下所示:
<?xml version="1.0" encoding="UTF-8"?>
< beansxmlns= "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-4.3.xsd" >


推荐阅读