准备工作


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程序。

每天学习一点点,每天进步一点点。

最新文章

  1. Entity Framework 6 Recipes 2nd Edition(11-11)译 -&gt; 在LINQ中调用数据库函数
  2. 我是如何进行Spring MVC文档翻译项目的环境搭建、项目管理及自动化构建工作的
  3. webpack CommonsChunkPlugin详细教程
  4. 关于UIView的autoresizingMask属性的研究【转】
  5. Android代码优化----Application节点的模板写法及UI工具类
  6. No bootable device-insert boot disk and press any key
  7. redhat vim编辑器永久添加行号
  8. 小蔡计算器 V4.0新版全新发布上线啦~欢迎大家下载使用哈~
  9. java对象占用内存大小计算方式
  10. 关于时间对象Date()
  11. poj Hotel 线段树
  12. 【一天一道LeetCode】#76. Minimum Window Substring
  13. UE4学习心得:Scene Component蓝图的一个简单应用
  14. C++ 文件流的详解
  15. server被强制关闭,
  16. bootstrap中的行和列布局
  17. cocos2dx lua 绑定之一:自动绑定自定义类中的函数
  18. jdk1.8新特性----接口可以有方法体,子类可以不用重写接口中已实现的方法
  19. 国际音标en
  20. Unity3d ugui 实现image代码换图

热门文章

  1. Linux系统curl获取公网ip
  2. docker容器介绍
  3. 事件总线功能库,Reface.EventBus 详细使用教程
  4. 【ubuntu】windows+ubuntu 设置windows为第一启动项
  5. Swoole 实战:MySQL 查询器的实现(协程连接池版)
  6. 数学--数论--HDU 2802 F(N) 公式推导或矩阵快速幂
  7. POJ 3241Object Clustering曼哈顿距离最小生成树
  8. Jmeter 插件图表分析
  9. web安全笔记
  10. Android RecyclerView滚动类控件修改、去掉滑动边界的阴影效果