Spring学习(一)idea中创建第一个Spring项目
1、前言
Spring框架是一个开放源代码的J2EE应用程序框架,由Rod Johnson发起,是针对bean的生命周期进行管理的轻量级容器(lightweight container)。 Spring解决了开发者在J2EE开发中遇到的许多常见的问题,提供了功能强大IOC、AOP及Web MVC等功能。Spring可以单独应用于构筑应用程序,也可以和Struts、Webwork、Tapestry等众多Web框架组合使用,并且可以与 Swing等桌面应用程序AP组合。因此, Spring不仅仅能应用于JEE应用程序之中,也可以应用于桌面应用程序以及小应用程序之中。Spring框架主要由七部分组成,分别是 Spring Core、 Spring AOP、 Spring ORM、 Spring DAO、Spring Context、 Spring Web和 Spring Web MVC。
2、创建Spring项目之前的准备
1、Maven
通过Maven项目管理工具、我们能在idea中更快的创建一个Spring项目、相关的jar包只需在pom.xml中添加依赖即可全部导入。
2、idea2019.6商业版
相关下载不过多赘述。
3、新建一个简单的Spring项目




pom.xml添加依赖
默认是没有spring相关依赖的、我们需要修改pom.xml、让idea 导入相关的依赖。

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
在pom.xml中加入这么一段话、所有spring需要的依赖即导入进来了、可以在下面看到。注意idea提示的时候点击import changes

4、进行IOC测试
IOC是什么呢?控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度。其中最常见的方式叫做依赖注入(Dependency Injection,简称DI),还有一种方式叫“依赖查找”(Dependency Lookup)。通过控制反转,对象在被创建的时候,由一个调控系统内所有对象的外界实体将其所依赖的对象的引用传递给它。也可以说,依赖被注入到对象中。Spring框架的核心就是IOC、通过IOC原则、代码之间的耦合性大大地降低、许多繁杂的对象创建被省去、效率大大提高。
1、新建一个实体类Student
package com.feng.entity;
import com.feng.factory.CourseFactory;
import com.feng.newinstance.HtmlCourse;
import com.feng.newinstance.ICourse;
import com.feng.newinstance.JavaCourse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Student {
private int stuNo;
private String stuName;
private int stuAge;
public int getStuNo() {
return stuNo;
}
public void setStuNo(int stuNo) {
this.stuNo = stuNo;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}
@Override
public String toString() {
return "Student{" +
"stuNo=" + stuNo +
", stuName='" + stuName + '\'' +
", stuAge=" + stuAge +
'}';
}
}
2、在applicationContext.xml中配置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放入spring IOC容器中 -->
<!-- id:唯一标识符、class:标识符的类型 -->
<bean id="student" class="com.feng.entity.Student">
<!-- property: 该class所代表的属性
name: 属性名
value: 属性值
-->
<property name="stuNo" value="2"></property>
<property name="stuName" value="ls"></property>
<property name="stuAge" value="20"></property>
</bean>
</beans>
3、在测试类中进行测试
package com.feng.test;
import com.feng.entity.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
//Spring 上下文对象:context
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 执行从springIOC容器中获取一个id为student的对象
Student student =(Student) context.getBean("student");
System.out.println(student);
}
}
输出如下:

5、总结
可以发现、SpringIOC容器帮助我们更加轻松的获取对象的属性和值、在这之前、我们只需要在application.xml中配置即可、从原来的new对象、变成现在从IOC容器中直接去取对象、这就是IOC的本质、也有一种说法叫做DI原则即依赖注入(Dependency Injection,简称DI)。即将对象注入到bean中、在从bean中去取就可以了。如果项目很大、这种方式免去了对象多次的赋值、减少代码耦合性、大大地提高了开发效率。
Spring学习(一)idea中创建第一个Spring项目的更多相关文章
- (Struts2学习系列一)MyEclipse创建第一个struts2项目
点击MyEclipse菜单栏File按钮,点击new-->Web Project 输入Project name之后点击Finish 项目创建完成. 然后右键项目,点击MyEclipse--> ...
- spring boot学习01【搭建环境、创建第一个spring boot项目】
1.给eclipse安装spring boot插件 Eclipse中安装Spring工具套件(STS): Help -> Eclipse Marketplace... 在Search标签或者Po ...
- 3.创建第一个android项目
安卓开发学习笔记 1.安卓开发之环境搭建 2.SDK目录结构和adb工具及命令介绍 3.创建第一个android项目 1.打开Eclipse,选择File——>new——>others.. ...
- 创建第一个Android项目
目录 创建第一个Android项目 创建HelloWorld项目 选择模板 选择模板界面的英文翻译 配置项目 配置项目界面英文翻译及解释 配置项目界面的注意事项 Name的命名规范 Package n ...
- 用Kotlin创建第一个Android项目(KAD 01)
原文标题:Create your first Android project using Kotlin (KAD 01) 作者:Antonio Leiva 时间:Nov 21, 2016 原文链接:h ...
- 在 Visual Studio 2013 中创建 ASP.NET Web 项目(0):专题导航 [持续更新中]
写在前面的话 随着 Visual Studio 2013 的正式推出,ASP.NET 和 Visual Studio Web 开发工具 也发布了各自的最新版本. 新版本在构建 One ASP.NET ...
- 在 Visual Studio 2013 中创建 ASP.NET Web 项目(1):概述 - 创建 Web 应用程序项目
注:本文是“在 Visual Studio 2013 中创建 ASP.NET Web 项目”专题的一部分,详情参见 专题导航 . 预备知识 本专题适用于 Visual Studio 2013 及以上版 ...
- 创建第一个Maven项目
-----------------------siwuxie095 创建第一个 Maven 项目 1.打开 Ec ...
- 在Eclipse中创建Maven多模块项目
在Eclipse中创建Maven多模块项目1,创建多模块项目选择File>New>Project,打开New Project窗口,选择Maven>Maven Project,选择下一 ...
随机推荐
- MariaDB——数据库登录
登录MariaDB数据库,用root账户和密码: 显示所有数据库列表:其中,information_schema.performance_schema.test.mysql,这4个库表是数据库系统自带 ...
- git 新建分支并切换到该分支_Git 从master拉取代码创建新分支 并且再将修改合并到master...
开发过程中会从master主分支copy到另一个开发分支: 1.切换到master分支 git checkout master 2.获取最新的代码 git pull origin master 3 ...
- C语言中宏定义#define 、 typedef 和枚举类型
替换时机 #define :预编译阶段简单替换,编译阶段展开源程序(1.词法扩展==程序生成期间的字符串替换 2.语义扩展==生成特定指令) 枚举常量:编译阶段确定其值 内联函数:编译阶段插入代码 t ...
- 日常Java 2021/9/20
Java随机数 运用Java的random函数实现猜数字游戏 随机产生一个1-50之间的数字,然后让玩家猜数,猜大猜小都给出提示,猜对后游戏停止 package pingchangceshi; imp ...
- adhere, adjust, adjacent
adhere to stick,不是to here. 在古英语里,stick是twig(细树枝).fasten(想必是用twig来固定).后引申为粘住.stick还有stab, pierce的意思,想 ...
- Output of C++ Program | Set 16
Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...
- hadoop Sort排序
1 public int getPartition(IntWritable key,IntWritable value,int numPartitions){ 2 int Maxnumber = 12 ...
- Centos 7 安装redis,修改配置文件不生效、外网不能访问。
前提: 在用Centos 7 安装 redis 时,遇上一下几个问题 ,记录下 . 1.修改配置文件,按官网步骤启动,不生效. 2.外网无法访问redis. 步骤: 1.打开centos 虚拟机 ,按 ...
- SQL Server中修改“用户自定义表类型”问题的分析与方法
前言 SQL Server开发过程中,为了传入数据集类型的变量(比如接受C#中的DataTable类型变量),需要定义"用户自定义表类型",通过"用户自定义表类型&quo ...
- 开发中的PR和MR
GitLab的是Pull Request缩写.GitHub则是Merge Request也就是MR. 当项目下载后进行更改并提交,每次过程算一次PR,一般会加入管理员审核,通过才能合并到master主 ...