【Spring】HelloSpring:使用IntelliJ IDEA学习Spring
来源:互联网 发布:游戏数据分析师薪酬 编辑:程序博客网 时间:2024/06/05 09:59
Spring
//最近和同学一起申报了一个国家创新项目,指导老师让我们用SSH框架,要我负责后台的开发。大一大二一直走移动开发路线,如今移动前端各种火爆,于是乎产生了走后台方向的想法,利用这个项目试试水,不过一切都需从头再来,希望不晚。
开发环境:
- IntelliJ IDEA 2017.1.5
关于IntelliJ IDEA和Eclipse,人们总是喜欢把它们两个进行比较,在我看来这没有比较性,毕竟一个收费,一个免费。由于jetbrains公司有学生版免费且个人有android-studo的使用习惯,所以选择了IDEA。
JetBrains 有个计划叫 Academic License Program,网址是https://www.jetbrains.com/student/。
在这个网址,只要你有学校里的edu账号,就可以申请免费试用。可以免费使用 Intellij IDEA, ReSharper,ReSharper C++, dotTrace,dotMemory,dotCover,AppCode,CLion,PhpStorm,PyCharm,RubyMine ,WebStorm 等一系列全家桶产品 。
在IntelliJ IDEA中创建Spring项目
1.在首先New Project
勾选Spring,此时在Libraries选项中默认是Download,当然你也可以用Use library选择自己下载好的,选择完成后点击next.
2.设置项目名字以及路径
设置好后点击finish即可,等待下载完成。
3.Spring的jars和配置文件都准备好了
此时helloWorld的项目创建成功,可以看到框架所需jar包都自动下载好了。
国际惯例:HelloWorld!
1.创建源文件
需要在scr目录下创建HelloWorld.java和MainAPP.java两个java源文件:
public class HelloWorld { private String message; public void setMessage(String message){ this.message = message; } public void sayHello(){ System.out.println("Your Message : " + message); }}
public class MainAPP { public static void main(String[] args) { ApplicationContext context= new ClassPathXmlApplicationContext("Beans.xml"); HelloWorld helloWorld=(HelloWorld) context.getBean("helloWorld"); helloWorld.sayHello(); helloWorld.setMessage("World"); helloWorld.sayHello(); }
关于主要程序有以下两个要点需要注意:
- 第一步是我们使用框架 API ClassPathXmlApplicationContext() 来创建应用程序的上下文。这个 API 加载 beans 的配置文件并最终基于所提供的 API,它处理创建并初始化所有的对象,即在配置文件中提到的 beans。
- 第二步是使用已创建的上下文的 getBean() 方法来获得所需的 bean。这个方法使用 bean 的 ID 返回一个最终可以转换为实际对象的通用对象。一旦有了对象,你就可以使用这个对象调用任何类的方法。
2.创建 bean 的配置文件
新建一个Spring配置,通常开发人员保存该文件的名称为 Beans.xml 文件。
<?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"> <bean id="helloWorld" class="HelloWorld"> <property name="message" value="Spring"/> </bean></beans>
当 Spring 应用程序被加载到内存中时,框架利用了上面的配置文件来创建所有已经定义的 beans,并且按照 标签的定义为它们分配一个唯一的 ID。你可以使用 标签来传递在创建对象时使用不同变量的值。
3.运行程序
新建一个Run配置,选择Application:
配置Name可有可无,主类Main class需要自己添加:
配置完成点击Apply和OK即可。
4.程序结果
初识Spring
以上例子中,与传统java模式不同的是,我们调用sayHello()方法的时候需要3个步骤。
1. 创建一个Spring的IOC容器对象
2. 从IOC容器中获取Bean实例
3. 调用sayHello()方法
我们使用了Spring的IOC容器,把对象的创建和管理的功能都交给了Spring去管理。
IOC ( Inversion of Control ) : 其思想是反转资源获取的方向。
传统的资源查找方式要求组件向容器发出请求查找资源,作为回应,容器适时返回资源。
应用IOC后,容器主动将资源传递给它所需要的组件,组件只需要选择一种合适的方式接收资源即可。
- 【Spring】HelloSpring:使用IntelliJ IDEA学习Spring
- Spring-(1)HelloSpring
- Intellij IDEA 学习Spring Boot (1)
- 使用IntelliJ IDEA导入Spring源码
- 使用IntelliJ IDEA开发Spring MVC HelloWorld
- 使用Intellij IDEA编写Spring Hello World
- 使用IntelliJ IDEA开发Spring MVC HelloWorld
- 使用IntelliJ IDEA实现Spring HelloWorld
- 使用IntelliJ idea开发Spring MVC服务
- 第一个使用spring实现hellospring的例子
- Spring学习(一)在IntelliJ IDEA上使用Maven创建Spring项目
- 使用Intellij IDEA整合Spring+Spring MVC+MyBitis
- Spring-Spring 开发环境搭建及HelloSpring
- SpringMVC学习笔记(一)使用IntelliJ IDEA开发Spring MVC HelloWorld
- SpringMVC学习笔记(二)使用IntelliJ IDEA开发Spring MVC HelloWorld(基于Maven)
- SpringMVC学习笔记(三)使用IntelliJ IDEA开发Spring MVC HelloWorld(注解配置SpringMVC)
- IntelliJ IDEA开发Spring mvc
- spring源码导入IntelliJ IDEA
- VS2013怎么配置opencv才能不用每次都重新配置一遍?
- DevOps之Docker自动化部署
- 红黑树
- Build Tiny C Compiler by VC in Visual Studio
- hadoop中的自动分区
- 【Spring】HelloSpring:使用IntelliJ IDEA学习Spring
- Oracle12C--包的初始化(四十八)
- Oracle12C--包的作用域(四十九)
- bzoj 3319 黑白树
- win7下通过easyBCD引导安装Ubuntu14.04
- Codeforces 424 div1 C. Bamboo Partition 830C 分块优化
- C语言定义了一个结构体怎么分配内存?C\C++中结构体变量与结构体指针内存分配问题?
- 为指针分配的内存太小
- 设计模式--单例模式(十)