Eclipse安装spring-tools和helloWorld案例

环境准备: 

下载zip包:https://spring.io/tools3/sts/all/  如下zip包下载下来

安装插件  Help ==>Install New SoftWare

如下点击Archive选择下载好后的zip包

如下选择即可

 

然后后面的同意协议什么的一路next最后finish即可

然后在窗口右下角有进度条进行安装插件,等待弹窗。最后会要求你重启eclipse。重启即可。

window==>preference可以看到已经安装好了如下图

Spring 的Hello World案例:

需要的5个jar包:

  1. commons-logging
  2. spring-beans
  3. spring-context
  4. spring-core
  5. spring-expresso

里面包含了spring-beans、spring-context、spring-core、spring-expresso四个jar包

然后复制到项目src/lib下并且Build Path添加到项目jar库

如下是添加好后的项目

  1. 创建HelloWorld对象

    HelloWorld.java

    public class HelloWorld {
    	
    	private String name;
    	
    	public void setName(String name) {
    		this.name=name;
    	}
    	
    	public void hello() {
    		System.out.println("hello "+name);
    	}
    
    }
  2. 在src下 创建springIOC容器的配置文件applicationContext.xml


    在<beans></beans>标签中添加<bean></bean>如下标签

    <!-- helloworldBean配置 -->
    	<bean id="helloWorld" class="com.taotao.beans.HelloWorld">
    		<property name="name"  value="spring" ></property>
        </bean>
  3. Main函数调用三步:
		//获取spring ioc容器(从配置的xml文件中获取)
		ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		
		//获取bean
		HelloWorld helloWorld=(HelloWorld)ctx.getBean("helloWorld");
		
		//调用hello方法
		helloWorld.hello();

执行一下main方法即可

截图分析代码:(放大查看)

发布了242 篇原创文章 · 获赞 13 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_41813208/article/details/103625666