使用IntelliJ IDEA实现Spring HelloWorld
来源:互联网 发布:mysql数据定义语言 编辑:程序博客网 时间:2024/05/20 04:10
由于最近学Spring Boot的时候接触了IntelliJ IDEA,觉得它在框架集成和支持方面做得的确比老迈的Eclipse好太多了;而且IDEA原生支持Spring,这就省去了很多不必要的环节,所以就用它了。好了,Talk is cheap,show me the code.
一、创建工程:
File->new->project,选择“Spring“,选择勾选Spring(默认就是选中的),然后Next,再输入工程名,点击Finish,在短暂的下载后,就可以开始愉快地敲代码了。
二、先写一个没用到框架时的Hello World
2.1、一个普通的实体类
package com.myspring.test;public class Hello { private String name; public void showName() { System.out.println("name is: "+name); } public void setName(String name) { this.name = name; }}
2.2、一个普通的测试类
package com.myspring.test;public class Test { public static void main(String[] args){ Hello hello=new Hello();//创建一个Hello类对象hello hello.setName("Microsoft");//为hello的属性赋值 hello.showName();//调用方法 }}
2.3、运行结果如下:
至此,一个跟Spring完全没有关系的Hello World就写完了。
三、想要将这个改造成使用Spring的版本需要做哪些工作:
3.1、在spring-config.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="hello" class="com.myspring.test.Hello"> <property name="name" value="Microsoft"></property> </bean></beans>
这里需要手动完成的只有<bean></bean>标签内的内容,根标签下的内容在创建工程的时候就已经完成了。
3.2、在main()方法中需要对创建Hello类对象的步骤进行修改
package com.myspring.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test { public static void main(String[] args){// Hello hello=new Hello();// hello.setName("Microsoft"); ApplicationContext ctx=new ClassPathXmlApplicationContext("spring-config.xml"); Hello hello= (Hello) ctx.getBean("hello"); hello.showName(); }}
3.3、然后就可以看一下最终成果了:
好了,最后来梳理一下整个过程吧:
1、创建一个新工程,选择spring工程,并勾选“Create empty spring-config.xml”;
2、写一个实体类,并在测试类中实现对该类某个属性的打印;
3、在spring-config.xml文件配置该对象的属性,包括bean的id(对象的名称,genBean()方法根据该参数获取对象)、class(类名的完整包名),property的name(类的属性名)和value(该类属性的值);
3、在测试类中将创建类对象步骤,改用spring框架中的ClassPathXmlApplicationContext()读取xml文件的方法来实现,将对象交由spring容器来管理,并为该对象赋值。
4、方法调用步骤不变。
- 使用IntelliJ IDEA实现Spring HelloWorld
- 使用IntelliJ IDEA开发Spring MVC HelloWorld
- 使用IntelliJ IDEA开发Spring MVC HelloWorld
- 使用Intellij idea新建HelloWorld
- SpringMVC学习笔记(一)使用IntelliJ IDEA开发Spring MVC HelloWorld
- SpringMVC学习笔记(二)使用IntelliJ IDEA开发Spring MVC HelloWorld(基于Maven)
- SpringMVC学习笔记(三)使用IntelliJ IDEA开发Spring MVC HelloWorld(注解配置SpringMVC)
- intellij idea 写 Helloworld
- 【Spring】HelloSpring:使用IntelliJ IDEA学习Spring
- 使用2016版intellij idea创建SpringMVC 的helloWorld
- 使用IntelliJ IDEA导入Spring源码
- 使用Intellij IDEA编写Spring Hello World
- 使用IntelliJ idea开发Spring MVC服务
- Intellij IDEA 使用jrebel运行spring-boot并实现自动编译进行热部署
- IntelliJ Idea 实现热部署 Spring Loaded spring-boot-devtools
- 使用Intellij IDEA整合Spring+Spring MVC+MyBitis
- Intellij idea下spark开发HelloWorld
- IntelliJ IDEA 教程 - HelloWorld 和相关设置
- LeetCode 11. Container With Most Water(盛最多的水)
- 暑期学习 LSTM 笔记
- 使用spring @Scheduled注解执行定时任务
- 一起Talk Android吧(第三十七回:Android中的Fragment二)
- 编译apache出现的问题:configure: error: APR not found . Please read the documentation
- 使用IntelliJ IDEA实现Spring HelloWorld
- 傅里叶的开篇
- BZOJ 4318: OSU! 期望dp
- android自定义控件-----自定义数字价格键盘
- Android消息机制基本原理
- [BZOJ1898][ZJOI2005]沼泽鳄鱼(矩阵乘法)
- mysql查询缓存
- Java基础——Servlet(五)
- FreeRTOS任务API函数的使用