Spring学习笔记-01、HelloWorld

一、Spring下载

官网地址:https://spring.io/projects/spring-framework

下载地址:https://repo.spring.io/webapp/#/artifacts/browse/tree/General/libs-release-local/org/springframework/spring/4.3.25.RELEASE/spring-framework-4.3.25.RELEASE-dist.zip

二、创建项目

idea新建一个spring项目,创建一个spring全局库。
在这里插入图片描述
项目文件结构如下:
在这里插入图片描述
需要导入commons-logging,否则运行时会报错。

三、HelloWorld程序

1、定义bean类
package com.chan.spring.bean;

/**
 * Student Bean
 * @author Chan
 */
public class Student {
    private String name;

    public Student() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void hello() {
        System.out.println("我是" + name);
    }
}
2、配置bean实例对象

创建src目录下创建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="stu1" class="com.chan.spring.bean.Student">
        <property name="name" value="小明" />
    </bean>
</beans>
3、在主程序中使用bean实例
package com.chan.spring.app;

import com.chan.spring.bean.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 应用类,程序入口
 * @author Chan
 */
public class Application {
    public static void main(String[] args) {
        //创建Spring IOC容器对象
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        //从IOC容器获取对象
        Student stu1 = (Student) context.getBean("stu1");

        //调用对象hello方法
        stu1.hello();
    }
}
4、编译运行

在这里插入图片描述

四、总结

Spring的特点
  1. 控制反转(IOC),通过依赖注入的方式(DI)
  2. 面向切面编程(AOP)
  3. 轻量级,对象不依赖于Spring
  4. 容器,Spring是容器,创建和管理对象的生命周期
  5. 一站式,可以整合其他框架和类库,自己也提供了MVC和JDBC框架。
Spring模块

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值