将其下libs文件夹下的4个jar包导入:

spring-context-5.2.3.RELEASE.jar、spring-beans-5.2.3.RELEASE.jar、

spring-core-5.2.3.RELEASE.jar、spring-expression-5.2.3.RELEASE.jar

参考:

spring jar包转成android spring导入jar包_xml

总共8个模块:(绿色框——模块,黑色框——该模块依赖的jar包)

Core Container —— 就是IOC容器,是核心。后面的AOP也是要依赖IOC容器的。

(想要使用IOC容器,只需要把其下面的4个包导进来即可,见上面)

导包步骤

从File —> Project Structure 进入:

spring jar包转成android spring导入jar包_spring_02

 导入对应的jar包即可(一共5个包,还有一个 jar包是:commons.logging.LogFactory,见下面 )

导包成功后:

spring jar包转成android spring导入jar包_java_03

导包成功后,就可以创建 .xml 配置文件了(在src下创建即可)。

代码目录结构:

spring jar包转成android spring导入jar包_jar包_04

 代码示例:

Person类:

package com.zhoulz.bean;

/**
 * @Auther: zhoulz
 * @Description: com.zhoulz.bean
 * @version: 1.0
 */
public class Person {
    private int id;
    private String name;
    private int age;
    private String gender;

    public Person() {
        System.out.println("person被创建了!");
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                '}';
    }
}

测试类:

package com.zhoulz.test;

import com.zhoulz.bean.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @Auther: zhoulz
 * @Description: com.zhoulz.test
 * @version: 1.0
 */
public class MyTest {
    public static void main(String[] args) {
        //原:
        /*Person person = new Person();
        //person.set 的方式进行赋值——下面的输出才会有值
        System.out.println(person);*/

        //现用一个较高端的方式
        ApplicationContext context = new ClassPathXmlApplicationContext("ioc.xml");
        //context 表示容器,
        //然后从当前容器获取对象,对象就是bean
        //Object person = context.getBean("person");//Object不合适——转换为Person类
        Person person = (Person) context.getBean("person");//强制类型转换
        System.out.println(person);

        //这样直接运行会报错:
        //Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
        //即:少一个jar包:commons.logging.LogFactory——加上即可

        //总结:
        //发现,自己没有new对象,但是现在用对象的时候却有了,
        //说明,现在是交由spring容器来帮我们进行整体对象的创建了

        //上面代码的注释:
        /*
        * applicationContext:表示IOC容器的入口,想要获取对象的话,必须要创建该类
        *    该类有两个读取配置文件的实现类
        *         1)ClassPathXmlApplicationContext:表示从classpath中读取数据
        *         2)FileSystemXmlApplicationContext:表示从当前文件系统读取数据
        * */
        //然后是:
        //Person person = (Person) context.getBean("person");
        //这是获取具体的bean实例对象,需要进行强制类型转换

        //上面用到了强转
        //下面还有一种方式:——获取对象的时候不需要进行强制类型转换
        //context.getBean ——> 选择:getBean(String s, Class<T> aClass) —— 重载的函数
        //即:
        Person person1 = context.getBean("person", Person.class);
        System.out.println(person1);

        //一个问题:容器中的person对象是什么时候创建的?
        //是用的时候创建的吗?Person person1 = context.getBean("person", Person.class);
        //: 容器中的对象在容器创建之前就已经把对象创建好了!!!

        //验证
        //(见Person类)在Person类中创建一个空构造器,里面写一句话
        //然后,在测试类中,只留第20行代码,然后运行 —— 得以验证。

        //最后,
        //其实,上面这种配置去创建对象的方式不是特别好,写起来比较麻烦。
        //见第二种方法:—— 使用maven的方式来构建项目
    }
}

配置文件:ioc.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" xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">

    <!--注释:-->
    <!--bean标签表示要创建的bean对象
        id:bean的唯一标识,为了跟其他的bean区分(即不能有两个相同的bean)
        class:表示要创建的bean的完全限定名(即:从包名到类名)
     -->
    <bean id="person" class="com.zhoulz.bean.Person">
        <!--给属性赋值使用property标签
        name:表示属性的名称
        value:表示具体的属性值
        -->
        <property name="id" value ="1"></property>
        <property name="name" value ="zhangsan"></property>
        <property name="age" value ="20"></property>
        <property name="gender" value ="男"></property>
    </bean>

    
</beans>

其中,少的 jar包:commons.logging.LogFactory 

下载地址为:

https://mvnrepository.com/artifact/commons-logging/commons-logging/1.2

spring jar包转成android spring导入jar包_spring_05

其中:

 ApplicationContext context = new ClassPathXmlApplicationContext("ioc.xml");

解释:见链接中的 — 1.1、1.2https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans

即:

org.springframework.beans和org.springframework.contextt包是Spring Framework的IoC容器的基础。BeanFactory接口提供了一种高级的配置机制,能够管理任何类型的对象。ApplicationContext是BeanFactory的子接口。它补充道:

更容易与Spring的AOP特性集成

消息资源处理(用于国际化)