Spring—Hello World

安装好SST之后,我们开始来利用Spring写一个Hello World程序。

1. 不用Spring的写法

新建project->新建“com.spring.beans”包->新建“HelloWorld”和“Main”两个类

在HelloWorld.java中输入以下代码:

package com.spring.beans;

public class HelloWorld {
	
	private String name;
	
	public void setName(String name) {
		this.name = name;
	}
	
	public void hello() {
		System.out.print("hello:" + name);
	}
}

在“Main.java”中输入以下代码:

package com.spring.beans;

public class Main {
	
	public static void main(String[] args) {
		
		//1.创建HellowWorld的一个对象
		HelloWorld helloWorld = new HelloWorld();
		//2.为name属性赋值
		helloWorld.setName("world");
		
		//3.调用hello方法
		helloWorld.hello();
	}
}

执行程序就可得到“hello:world”这个输出。

但当我们使用Spring了之后,第一步和第二步就可以交给Spring来做。具体做法如下:

2. 利用Spring的做法

2.1 先导入包

在网上下载如下包:

然后再导入project中。

2.2 在“src”下新建Spring的配置文件

右键“src”->“new”->"spring bean configuration file"(如果没有的话点击“other”)进入以下界面

将该文件命名为“applicationContext”,再点击“finish”。

最终project如下:

2.3 配置applicationContext.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 -->
	<bean id="helloworld" class="com.spring.beans.HelloWorld">   <!-- class是bean的类名 -->
	    <property name="name" value="Spring"></property>
	</bean>  

</beans>

“property”中的“name”代表的是“HelloWorld”类中的“setName”中的“name”:即相当于setName("Spring")。

2.4 修改Main.java代码

Main中的代码修改如下

package com.spring.beans;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
	
	public static void main(String[] args) {
		
		//1.创建HellowWorld的一个对象
		//HelloWorld helloWorld = new HelloWorld();
		//2.为name属性赋值
		//helloWorld.setName("world");
		
		//1.创建Spring的IOC容器对象
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		//2.从IOC容器中获取Bean实例
		HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloworld");
		
		//3.调用hello方法
		helloWorld.hello();
	}
}

运行后结果如下:

红色字体表示的是Spring的日志。

2.5 Spring执行过程

首先先在“HelloWorld”类中加入点输出语句和一个类,修改如下:

package com.spring.beans;

public class HelloWorld {
	
	private String name;
	
	public void setName(String name) {
		System.out.print("setName:" + name);
		this.name = name;
	}
	
	public void hello() {
		System.out.println("hello:" + name);
	}
	public HelloWorld() {
		System.out.println("HelloWorld's Constructing..... ");
	}
}

再把Main中的2,3步给注释掉,修改如下:

package com.spring.beans;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
	
	public static void main(String[] args) {
		
		//1.创建HellowWorld的一个对象
		//HelloWorld helloWorld = new HelloWorld();
		//2.为name属性赋值
		//helloWorld.setName("world");
		
		//1.创建Spring的IOC容器对象
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		//2.从IOC容器中获取Bean实例
		//HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloworld");
		
		//3.调用hello方法
		//helloWorld.hello();
	}
}

执行结果如下:

可以看到“hello:spring”这句话没有输出,所以在创建容器的时候会调用 “HelloWorld”这个构造器并对配置文件中的bean进行初始化,同时会调setName这个方法对name属性进行赋值。

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值