Spring的安装与简单使用

1. 下载与eclipse版本对应的springsource-tool-suite包

官网下载地址:http://spring.io/tools/sts/all

如果要其他版本的,直接改以下链接对应版本的地方就可以了:

http://download.springsource.com/release/TOOLS/update/3.9.5.RELEASE/e4.8/springsource-tool-suite-3.9.5.RELEASE-e4.8.0-updatesite.zip

2. 安装springsource-tool-suite

3. 安装完成后重启eclipse,出现下面表示安装成功

4. 下载Spring项目所需的jar包

http://repo.spring.io/libs-release-local/org/springframework/spring/5.0.5.RELEASE/

5.新建java项目,新建lib文件夹将下面的五个jar包导入工程

6.新建HelloWorld类

package com.njupt.spring;

public class HelloWorld {
    private String name;

    public String getName() {
        return name;
    }

    public void setName2(String name) {
        this.name = name;
    }
    public void sayHello() {
        System.out.println("Hello World!");
    }
    
}

7.新建Spring的配制文件

<?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="com.njupt.spring.HelloWorld">
        <property name="name2" value="Spring"></property>
    </bean>
</beans>

8.在main函数中使用该javaBean

package com.njupt.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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.调用bean的方法
        helloWorld.sayHello();
    }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值