• 2- 创建Spring项目—— 方式1


    导包:

    地址:https://repo.spring.io/ui/native/libs-release-local/org/springframework/spring/

    如:包名:spring-5.2.3.RELEASE-dist.zip

    将其下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

    参考:

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

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

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

    导包步骤

    从File —> Project Structure 进入:

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

    导包成功后:

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

    代码目录结构:

     代码示例:

    Person类:

    1. package com.zhoulz.bean;
    2. /**
    3. * @Auther: zhoulz
    4. * @Description: com.zhoulz.bean
    5. * @version: 1.0
    6. */
    7. public class Person {
    8. private int id;
    9. private String name;
    10. private int age;
    11. private String gender;
    12. public Person() {
    13. System.out.println("person被创建了!");
    14. }
    15. public int getId() {
    16. return id;
    17. }
    18. public void setId(int id) {
    19. this.id = id;
    20. }
    21. public String getName() {
    22. return name;
    23. }
    24. public void setName(String name) {
    25. this.name = name;
    26. }
    27. public int getAge() {
    28. return age;
    29. }
    30. public void setAge(int age) {
    31. this.age = age;
    32. }
    33. public String getGender() {
    34. return gender;
    35. }
    36. public void setGender(String gender) {
    37. this.gender = gender;
    38. }
    39. @Override
    40. public String toString() {
    41. return "Person{" +
    42. "id=" + id +
    43. ", name='" + name + '\'' +
    44. ", age=" + age +
    45. ", gender='" + gender + '\'' +
    46. '}';
    47. }
    48. }

    测试类:

    1. package com.zhoulz.test;
    2. import com.zhoulz.bean.Person;
    3. import org.springframework.context.ApplicationContext;
    4. import org.springframework.context.support.ClassPathXmlApplicationContext;
    5. /**
    6. * @Auther: zhoulz
    7. * @Description: com.zhoulz.test
    8. * @version: 1.0
    9. */
    10. public class MyTest {
    11. public static void main(String[] args) {
    12. //原:
    13. /*Person person = new Person();
    14. //person.set 的方式进行赋值——下面的输出才会有值
    15. System.out.println(person);*/
    16. //现用一个较高端的方式
    17. ApplicationContext context = new ClassPathXmlApplicationContext("ioc.xml");
    18. //context 表示容器,
    19. //然后从当前容器获取对象,对象就是bean
    20. //Object person = context.getBean("person");//Object不合适——转换为Person类
    21. Person person = (Person) context.getBean("person");//强制类型转换
    22. System.out.println(person);
    23. //这样直接运行会报错:
    24. //Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
    25. //即:少一个jar包:commons.logging.LogFactory——加上即可
    26. //总结:
    27. //发现,自己没有new对象,但是现在用对象的时候却有了,
    28. //说明,现在是交由spring容器来帮我们进行整体对象的创建了
    29. //上面代码的注释:
    30. /*
    31. * applicationContext:表示IOC容器的入口,想要获取对象的话,必须要创建该类
    32. * 该类有两个读取配置文件的实现类
    33. * 1)ClassPathXmlApplicationContext:表示从classpath中读取数据
    34. * 2)FileSystemXmlApplicationContext:表示从当前文件系统读取数据
    35. * */
    36. //然后是:
    37. //Person person = (Person) context.getBean("person");
    38. //这是获取具体的bean实例对象,需要进行强制类型转换
    39. //上面用到了强转
    40. //下面还有一种方式:——获取对象的时候不需要进行强制类型转换
    41. //context.getBean ——> 选择:getBean(String s, Class aClass) —— 重载的函数
    42. //即:
    43. Person person1 = context.getBean("person", Person.class);
    44. System.out.println(person1);
    45. //一个问题:容器中的person对象是什么时候创建的?
    46. //是用的时候创建的吗?Person person1 = context.getBean("person", Person.class);
    47. //: 容器中的对象在容器创建之前就已经把对象创建好了!!!
    48. //验证
    49. //(见Person类)在Person类中创建一个空构造器,里面写一句话
    50. //然后,在测试类中,只留第20行代码,然后运行 —— 得以验证。
    51. //最后,
    52. //其实,上面这种配置去创建对象的方式不是特别好,写起来比较麻烦。
    53. //见第二种方法:—— 使用maven的方式来构建项目
    54. }
    55. }

    配置文件:ioc.xml

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
    4. 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">
    5. <bean id="person" class="com.zhoulz.bean.Person">
    6. <property name="id" value ="1">property>
    7. <property name="name" value ="zhangsan">property>
    8. <property name="age" value ="20">property>
    9. <property name="gender" value ="男">property>
    10. bean>
    11. beans>

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

    下载地址为:

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

    其中:

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

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

    即:

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

    更容易与Spring的AOP特性集成

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

    事件发布

    应用层特定的上下文,例如在web应用程序中使用的WebApplicationContext。

    简而言之,BeanFactory提供了配置框架和基本功能,而ApplicationContext添加了更多特定于企业的功能。ApplicationContext是BeanFactory的一个完整的超集,在本章描述Spring的IoC容器时专门使用。有关使用BeanFactory而不是ApplicationContext的更多信息,请参阅有关BeanFactory API的部分。

  • 相关阅读:
    Redis-03持久化
    redis - 实现周期性数据无上报检测
    Linux守护进程
    基于Python机器学习实现的花卉识别
    plumelog日志框架的搭建与使用
    全平台高速下载器Gopeed
    【LeetCode每日一题】——72.编辑距离
    小程序获取支付api
    Leetcode力扣 MySQL数据库 1892 页面推荐
    《2024年网络钓鱼现状全球报告》解读
  • 原文地址:https://blog.csdn.net/zhoulizhengZ/article/details/127575563