本文研究的主要是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实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
如对本文有疑问, 点击进行留言回复!!
springmvc之ResponseBody响应json数据遇到的错误及解决
uni-app 后台升级 静默升级 uniapp 后台更新 静默更新 在线升级
SpringBoot多Module启动报错Could not transfer metadata
Hibernate项目报错:Cannot call sendError() after the response has been committed
网友评论