spring中如何创建项目Hello Spring

发布时间:2021-06-07 11:25:48 作者:小新
来源:亿速云 阅读:153

这篇文章主要介绍spring中如何创建项目Hello Spring,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

一、创建eclipse项目,引入jar包

1、eclipse创建java project项目 HelloSpring

2、创建lib目录,加入spring必须的5个jar包

3、选中5个文件,右键 -> Build Path -> add to build path

二、编写spring的hello spring代码

1、创建包io.spring.beans,并编写HelloWorld.java

package io.spring.beans;
/** 
 * @author 胖胖のALEX E-mail:zanbin168@qq.com 
 * @version 1.0 
*/
public class HelloWorld {
	private String name;
	public void setName(String name) {
		this.name = name;
	}
	public void hello() {
		System.out.println("hello " + name);
	}
}

2、src右键 -> 创建spring bean configuration文件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="io.spring.beans.HelloWorld"> 
    <property name="name" value="大红"></property> 
  </bean> 
 
</beans>

3、编写Main.java

package io.spring.beans;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/** 
 * @author 胖胖のALEX E-mail:zanbin168@qq.com 
 * @version 1.0 
*/
public class Main {
	public static void main(String[] args) {
		//1、创建Spring的IOC容器对象 
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		//2、从IOC容器中获取Bean实例 
		HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
		//3、调用hello方法 
		helloWorld.hello();
	}
}

输出结果

当console内打印出红色spring日志,表示spring应用成功

以上是“spring中如何创建项目Hello Spring”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. spring-data-mongodb-hello-world-example
  2. 一、Spring MVC的hello world

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java spring

上一篇:Java中String如何创建对象

下一篇:Android开发之圆角矩形如何创建工具RoundRect类

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》