在IDEA中使用Spring写一个HelloWorld
准备工作
1、使用IDEA2018专业版,
我试了IDEA2019教育版和IDEA2020社区版,都无法顺利创建一个Spring项目,实在是恼火,一气之下,统统卸载掉。
重装了一个IDEA2018专业版,突然就变得很顺利了。
2、在IDEA中安装Spring插件
点击File--settings--Plugins,搜索“Spring”,安装Spring Assistant。

新建Spring项目
1、新建项目:New--Project,选择Spring

项目名为“hellospring”
IDEA有一个好处,当你创建spring项目时,它会自动下载所需要的spring包。
2、右键src,创建一个包(Package),名字叫作"hello"吧。
3、在hello包下创建两个class源文件:HelloWorld.java 和MainApp.java
其中,HelloWorld.java 中写入:
package hello;
public class HelloWorld {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
}
MainApp.java中写入:
package hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
}
}
上面MainApp.java文件里,有一个Beans.xml

这是一个配置文件,需要手动创建它。
4、创建配置文件Beans.xml
右键src--New--XML Configuation File--Spring Config

命名为Beans,点击确定。
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="hello.HelloWorld">
<property name="message" value="Hello World!"/>
</bean>
</beans>
其实我们需要添加的只有这三行:
<bean id="helloWorld" class="hello.HelloWorld">
<property name="message" value="Hello World!"/>
</bean>
class 属性表示需要注册的 bean 的全路径,这里就是HelloWorld.java的文件路径
id 则表示 bean 的唯一标记。
这里的value中的值,就是输出到屏幕上的内容。
此时的目录结构如下:

忽略掉out目录,那是程序运行之后自动生成的。
运行MainApp.java文件
输出结果如下:

试一下,修改value中的值,比如,改成下面这样:
<bean id="helloWorld" class="hello.HelloWorld">
<property name="message" value="你好,Spring!"/>
</bean>
再运行MainApp.java,结果如下:

就这样,成功创建了第一个Spring程序。
每天学习一点点,每天进步一点点。
在IDEA中使用Spring写一个HelloWorld的更多相关文章
- Spring第一个helloWorld
Spring 简介: 轻量级:Spring是非侵入性的-基于Spring开发的应用中的对象可以不依赖于Spring的API 依赖注入(DI—dependdency injection.IOC) 面向切 ...
- 汇编入门——使用DOSBox写一个HelloWorld以及相关软件安装
0.0.0) 在D盘建立一个ASM文件夹 0.0.1) 放入所需要的文件 1所标示的红色框为必须要存在的文件,要处理汇编文件.百度网盘中下载. 2自己编写的汇编(asm)文件. 3编译汇编自己生成的文 ...
- 跟我一起写一个hello-world react组件并发布到npm
第一步:初始化我们的配置 $ mkdir react-hello-world $ cd react-hello-world/ $ npm init -y 修改我们的package.json文件 //p ...
- 微信小程序开发入门:10分钟从0开始写一个hello-world
小程序开发需要三个描述整体程序的app文件 和 一个描述多个页面的 pages文件夹. (1)三个app文件分别是app.js,app.json,app.wxss. app.js文件是脚本文件处理一些 ...
- 利用HADOOP中的jar写一个RPC
RPC调用需要服务端和客户端使用相同的协议: 协议: package cn.itcast.bigdata.hadooprpc.protocol; public interface IUserLogin ...
- Spring Boot 《一》开发一个“HelloWorld”的 web 应用
一,Spring Boot 介绍 Spring Boot不是一个新的框架,默认配置了多种框架使用方式,使用SpringBoot很容易创建一个独立运行(运行jar,内嵌Servlet).准生产级别的基于 ...
- 2.每人自己建立一个HelloWorld项目,练习使用git的add/commit/push/pull/fetch/clone等基本命令。比较项目的新旧版本的差别。答题人:张立鹏
第1步:创建SSH Key.在用户主目录下,看看有没有.ssh目录,如果有,再看看这个目录下有没有id_rsa和id_rsa.pub这两个文件,如果已经有了,可直接跳到下一步.如果没有,打开Shell ...
- eclipse使用javaFX写一个HelloWorkld
------------------------------------------------ 操作系统:Ubuntu18.04 EclipseVersion: Oxygen.3a Release ...
- 用Solidity在Truffle上构建一个HelloWorld智能合约
原文地址:石匠的blog Truffle的环境安装在前文已经整理,这次用Solidity在Truffle写一个HelloWorld以太坊智能合约,并在testrpc上进行测试.当前的软件版本信息如下: ...
随机推荐
- zabbix监控ftp
[root@agent ~]# yum -y install vsftpd [root@agent ~]# systemctl start vsftpd[root@agent ~]# systemct ...
- Vue Cli 报错:You are using the runtime-only build of Vue where the template compiler is not availabl
报错原因: 这里引用的是vue.runtime.esm.js,造成的不能正常运行. vue-cli 2.x 解决方法: 在webpack.base.conf.js配置文件中多加了一段代码,将 vue/ ...
- 基于UDP的客户端和服务器端的代码设计
实验平台 linux 实验内容 编写UDP服务器和客户端程序,客户端发送消息,服务器接收消息,并打印客户端的IP地址和端口号. 实验原理 UDP是无需连接的通信,其主要实现过程如下: 同样,我们可以按 ...
- CF #636 (Div. 3) 对应题号CF1343
unrated 选手悠闲做题,然后只做出四个滚蛋了 符合 div3 一贯风格,没啥难算法 E最后就要调出来了,但还是赛后才A的 CF1343A Candies 传送门 找到一个 \(x\),使得存在一 ...
- Nginx模块开发(2)————下载文件
Nginx的HTTP模块下载文件和传送缓冲区的字符串差不多,只需将文件标志置为1即可,我转送的文件是mp3的,所以HTTP的那个mine 类型要写为audio/mp3,二话不说了,贴代码,代码和之前那 ...
- 阿里云服务器连接AWS-S3
1.找到一个路径下载 aws-cli (使用离线包安装) wget -P /usr/local/software https://s3.amazonaws.com/aws-cli/awscli-bu ...
- C - A Plug for UNIX POJ - 1087 网络流
You are in charge of setting up the press room for the inaugural meeting of the United Nations Inter ...
- Mahout聚类和kafaka相关知识
1.说几种距离测度Mahout: 欧式距离测度:平方欧式距离测度:曼哈顿距离测度:余弦距离测度:加权距离测度 2.K-means算法参数:
- STM32F767ZI NUCLEO144 基于CubeIDE快速开发入门指南
刚入手的NUCLEO-F767ZI:整合官网资源,理清思路,便于快速进行快发: 文章目录 1 NUCLEO 系列 2 NUCLEO-F767ZI 3 环境搭建 3.1 Keil/IAR安装 3.2 C ...
- xml(4)
schema约束 dtd语法:<!ELEMENT 元素名称 约束> schema符合xml的语法,xml语句 一个xml中可以有多个schema,多个schema用名称空间区分(类似jav ...