1. 环境的搭建
  • eclipse中的spring插件
  • 导入jar包(博客搜索maven)
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.0.6.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>5.0.6.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>5.0.6.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.0.6.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>4.3.14.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.3.14.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.0.6.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.0.6.RELEASE</version>
    </dependency>
  1. helloworld实现
  • 1.新建一个Spring Bean Configuration File命名为beans-helloworld.xml (右键 new 搜索Spring Bean Configuration File)
    <?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">
    <!--id为该bean唯一标识、class指向bean的完整路径-->
      <bean id="timechoUsre" class="bean.TimechoUser">
      <!-- property为bean中属性,value为该属性赋的值 -->
          <property name="name" value="shafish"></property>
      </bean>
    </beans>
  • 2.新建javabean类
    bean.TimechoUser--name属性、set、get、toString方法
  • 3.main测试类
      import org.springframework.context.ApplicationContext;
      import org.springframework.context.support.ClassPathXmlApplicationContext;
      import bean.TimechoUser;
      public class Main {
          //* 1.创建spring的IOC 容器对象
          //* 2. 从IOC容器中取出bean实例
          //* 3. 调用方法
          //* @Title: main
          //* @Description: TODO(spring的简单实现)
          //* @param @param args  参数说明
          //* @return void    返回类型
          //* @throws
          public static void main(String[] args) {
              //TimechoUser user = new TimechoUser();
              //user.setName("你好");
              //创建对象以及为对象赋值--使用spring
                      //ctx IOC容器,埋坑1
              ApplicationContext  ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
              TimechoUser user = (TimechoUser) ctx.getBean("timechoUsre");
              System.out.println(user.getName());
          }
      }

总结:
IOC容器初始化时(new)会新建一个bean(调用bean的构造方法),
在容器中进行赋值(可以称为注入)等操作(beans-helloworld.xml),
使用时指定容器的bean id获取javabean使用


更多beans-helloworld.xml中的内容下篇文章会讲述,先把helloworld一个简单的demo跑起来-对spring操作有大概了解