本文研究的主要是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实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
您可能感兴趣的文章:- Java中Spring WebSocket详解
- SpringBoot文件上传控制及Java 获取和判断文件头信息
- Java框架搭建之Maven、Mybatis、Spring MVC整合搭建(图文)
- java使用spring实现读写分离的示例代码
- springboot的java配置方式(实例讲解)
- java springmvc实现验证码功能
- 通过Spring Shell 开发 Java 命令行应用
- Java编程实现springMVC简单登录实例
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|