这篇文章主要介绍spring中如何创建项目Hello Spring,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
1、eclipse创建java project项目 HelloSpring
2、创建lib目录,加入spring必须的5个jar包
3、选中5个文件,右键 -> Build Path -> add to build path
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”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。