企业级开发框架  

  Spring Framework 是整个 Spring 生态的基础,各个模块都是基于 Spring Framework 衍生出来的。

  Spring 的两大核心机制 IOC 控制翻转、AOP 面向切面编程。

    IOC 对象创建不再由开发者完成,而是容器自动创建,开发者直接取出来用即可。

1、新建maven项目

2、添加依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.sunjian</groupId>
    <artifactId>springPrac</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.2.RELEASE</version>
        </dependency>
    </dependencies>

</project>

3、新建实体类

Student

package com.sunjian.entity;

/**
 * @author sunjian
 * @date 2020/3/14 9:13
 */
public class Student {
    private String id;

    public String getId() {
        return id;
    }

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

    private String name;
    private String age;

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

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

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

Student2

package com.sunjian.entity;

/**
 * @author sunjian
 * @date 2020/3/14 10:05
 */
public class Student2 {
    private String id;
    private String name;
    private String age;

    public Student2(String id, String name, String age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public String getId() {
        return id;
    }

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

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

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

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

Student3

package com.sunjian.entity;

/**
 * @author sunjian
 * @date 2020/3/14 10:05
 */
public class Student3 {
    private String id;
    private String name;
    private String age;
    private Student4 student4;

    public Student4 getStudent4() {
        return student4;
    }

    public void setStudent4(Student4 student4) {
        this.student4 = student4;
    }

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

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

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

Student4

package com.sunjian.entity;

/**
 * @author sunjian
 * @date 2020/3/14 10:05
 */
public class Student4 {
    private String id;
    private String name;
    private String age;

    public String getId() {
        return id;
    }

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

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

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

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

Students

package com.sunjian.entity;

/**
 * @author sunjian
 * @date 2020/3/14 12:05
 */
public class Students {
    private String id;
    private String name;
    private String age;
    private Classes classes;

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

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

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

    public Classes getClasses() {
        return classes;
    }

    public void setClasses(Classes classes) {
        this.classes = classes;
    }
}

Classes

package com.sunjian.entity;

import java.util.List;

/**
 * @author sunjian
 * @date 2020/3/14 12:02
 */
public class Classes {
    private String id;
    private String name;
    private List<Students> students;

    public List<Students> getStudents() {
        return students;
    }

    public void setStudents(List<Students> students) {
        this.students = students;
    }

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "Classes{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", students=" + students +
                '}';
    }
}

4、在resources目录下新建spring.xml文件,添加bean

<?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 id="stu" class="com.sunjian.entity.Student">
        <property name="id" value="1"></property>
        <!--<property name="name" value="sunjian"></property>-->
        <!--特殊字符处理-->
        <property name="name">
            <value>
                <![CDATA[<孙健>]]>
            </value>
        </property>
        <property name="age" value="32"></property>
    </bean>

    <!-- 有参构造方式 1-->
    <bean id="stu_1" class="com.sunjian.entity.Student2">
        <constructor-arg index="0" value="1"></constructor-arg>
        <constructor-arg index="1" value="孙健"></constructor-arg>
        <constructor-arg index="2" value="31"></constructor-arg>
    </bean>
    <!-- 有参构造方式 2 常用 -->
    <bean id="stu_2" class="com.sunjian.entity.Student2">
        <constructor-arg name="id" value="1"></constructor-arg>
        <constructor-arg name="name" value="孙健"></constructor-arg>
        <constructor-arg name="age" value="32"></constructor-arg>
    </bean>
    <!-- 有参构造方式 3-->
    <bean id="stu_3" class="com.sunjian.entity.Student2">
        <constructor-arg value="1"></constructor-arg>
        <constructor-arg value="孙健"></constructor-arg>
        <constructor-arg value="33"></constructor-arg>
    </bean>

    <!-- 两个bean的级联方式 一对一-->
    <bean id="stu3" class="com.sunjian.entity.Student3">
        <property name="id" value="1"></property>
        <property name="age" value="33"></property>
        <property name="name" value="sunjian stu3"></property>
        <property name="student4" ref="stu4"></property>
    </bean>
    <bean id="stu4" class="com.sunjian.entity.Student4">
        <property name="id" value="1"></property>
        <property name="age" value="34"></property>
        <property name="name" value="sunjian stu4"></property>
    </bean>

    <!-- bean对象之间的集合注入 一对多-->
    <bean id="classes" class="com.sunjian.entity.Classes">
        <property name="id" value="1"></property>
        <property name="name" value="243班"></property>
        <property name="students">
            <list>
                <ref bean="stus1"></ref>
                <ref bean="stus2"></ref>
                <ref bean="stus3"></ref>
            </list>
        </property>
    </bean>
    <bean id="stus1" class="com.sunjian.entity.Students">
        <property name="id" value="1"></property>
        <property name="name" value="张三"></property>
        <property name="age" value="30"></property>
    </bean>
    <bean id="stus2" class="com.sunjian.entity.Students">
        <property name="id" value="2"></property>
        <property name="name" value="李四"></property>
        <property name="age" value="31"></property>
    </bean>
    <bean id="stus3" class="com.sunjian.entity.Students">
        <property name="id" value="3"></property>
        <property name="name" value="赵四"></property>
        <property name="age" value="32"></property>
    </bean>

</beans>

5、测试类

package com.sunjian.test;

import com.sunjian.entity.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author sunjian
 * @date 2020/3/14 9:14
 */
public class Test {
    public static void main(String[] args) {
        // 加载IOC容器
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");

        System.out.println("-------------无参构造方式-------------------------");

        // 根据bean的id去拿
//        Student student = (Student) applicationContext.getBean("stu");
        // 根据类型去拿 (注意:spring.xml中只能有一个class="com.sunjian.entity.Student")
        Student student = applicationContext.getBean(Student.class);
        System.out.println(student);

        System.out.println("-------------有参构造方式-------------------------");

//        Student2 student2 = (Student2) applicationContext.getBean("stu_2");
//        Student2 student2 = (Student2) applicationContext.getBean("stu_3");
        Student2 student2 = (Student2) applicationContext.getBean("stu_1");
        System.out.println(student2);

        System.out.println("--------------两个bean的级联方式------------------------");

        Student3 student3 = (Student3) applicationContext.getBean("stu3");
        System.out.println(student3);

        System.out.println("---------------bean对象之间的集合注入-----------------------");
        Classes classes = (Classes) applicationContext.getBean("classes");
        System.out.println(classes);
    }
}

运行结果

-------------无参构造方式-------------------------
Student{id='1', name='
                <孙健>
            ', age='32'}
-------------有参构造方式-------------------------
Student{id='1', name='孙健', age='31'}
--------------两个bean的级联方式------------------------
Student3{id='1', name='sunjian stu3', age='33', student4=Student{id='1', name='sunjian stu4', age='34'}}
---------------bean对象之间的集合注入-----------------------
Classes{id='1', name='243班', students=[Students{id='1', name='张三', age='30'}, Students{id='2', name='李四', age='31'}, Students{id='3', name='赵四', age='32'}]}

6、注意事项

  • 实体类中,必须有无参构造。
  • 成员变量必须有setter方法。

  IOC实例化对象的过程,通过反射 + XML解析的方式对spring.xml进行处理,反射拿到无参构造函数,调用创建对象,同时获取setter方法,调用完成成员变量的赋值。

OK.

Spring框架——IOC 容器的创建与使用的更多相关文章

  1. Spring框架IOC容器和AOP解析 非常 有用

    Spring框架IOC容器和AOP解析   主要分析点: 一.Spring开源框架的简介  二.Spring下IOC容器和DI(依赖注入Dependency injection) 三.Spring下面 ...

  2. Spring框架IOC容器和AOP解析

    主要分析点: 一.Spring开源框架的简介  二.Spring下IOC容器和DI(依赖注入Dependency injection) 三.Spring下面向切面编程(AOP)和事务管理配置  一.S ...

  3. spring框架--IOC容器,依赖注入

    思考: 1. 对象创建创建能否写死? 2. 对象创建细节 对象数量 action  多个   [维护成员变量] service 一个   [不需要维护公共变量] dao     一个   [不需要维护 ...

  4. SpringMVC系列(十五)Spring MVC与Spring整合时实例被创建两次的解决方案以及Spring 的 IOC 容器和 SpringMVC 的 IOC 容器的关系

    一.Spring MVC与Spring整合时实例被创建两次的解决方案 1.问题产生的原因 Spring MVC的配置文件和Spring的配置文件里面都使用了扫描注解<context:compon ...

  5. Spring的IOC容器第一辑

    一.Spring的IOC容器概述 Spring的IOC的过程也被称为依赖注入(DI),那么对象可以通过构造函数参数,工厂方法的参数或在工厂方法构造或返回的对象实例上设置的属性来定义它们的依赖关系,然后 ...

  6. spring的Ioc容器与AOP机制

    为什么要使用Spring的Ioc容器? 1.首先,spring是一个框架,框架存在的目的就是给我们的编程提供简洁的接口,可以使得我们专注于业务的开发,模块化,代码简洁,修改方便. 通过使用spring ...

  7. spring之IOC容器创建对象

    1.术语了解 1.1组件/框架设计 侵入式设计 引入了框架,对现有的类的结构有影响:即需要实现或继承某些特定类. 例如: Struts框架非侵入式设计 引入了框架,对现有的类结构没有影响. 例如:Hi ...

  8. Spring的IoC容器(转)BeanFactory

    Spring的IoC容器 Spring读书笔记-----Spring的Bean之Bean的基本概念 加菲猫 Just have a little faith. Spring的IoC容器 (用户持久化类 ...

  9. [转载]Spring下IOC容器和DI(依赖注入) @Bean及@Autowired

    Spring下IOC容器和DI(依赖注入) @Bean及@Autowired自动装配 bean是什么 bean在spring中可以理解为一个对象.理解这个对象需要换一种角度,即可将spring看做一门 ...

随机推荐

  1. 前端js代码以备不时之需

    //获取id元素信息let getId = (args) => { return document.getElementById(args);} //获取类名元素let getClassName ...

  2. node新人

    node  使用   http和express创建服务器环境 如 apache  iis等 不需要配置一堆文件  为啥使用node  省事  v8引擎  异步js 不影响浏览者浏览网站 redis   ...

  3. Kubelet

    Kubelet 相关博客 Kubelet组件深度解析 Kubelet组件解析 Kubelet运行机制分析 Kubelet与apiserver通信 ___ Kubelet组件运行在Node节点上,维持运 ...

  4. ndk-stack使用方法(转)

    最近在mac上编译android 版本,各种崩溃让人蛋疼,网上学习了下ndk-stack使用方法. 自己备忘下: 1.运行终端. 跳转到你android sdk 目录 因为你的adb 在里面. 如 c ...

  5. LISTAGG函数

    官网进入 该函数作用是可以实现对列值得拼接: 根据官网介绍,可以对列值排序进行拼接,也可以分组拼接 1.1运行结果 1.2运行结果 2运行结果 注意该函数提供的 over( partition by ...

  6. 强制迁移、合区 APP太强势伤害用户同时是否违法?

    APP太强势伤害用户同时是否违法?" title="强制迁移.合区 APP太强势伤害用户同时是否违法?"> 对于经常混迹在国内各大手游的玩家来说,"合区& ...

  7. 4——PHP比较&&复制运算符

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...

  8. win10环境下VS2019配置NTL库

    win10环境下VS2019配置NTL库 1.下载 WINNTL库文件     https://www.shoup.net/ntl/download.html 2.创建静态库 文件->新建-&g ...

  9. Centos 7 中 部署 asp.net core 3.0 + nginx + mongodb 的一些新手简单入门,非docker

    目录 零.准备工作 一.部署Mongodb 1.安装Mongodb 2.创建mongodb的数据目录 3.设置目录权限 4.设置mongodb启动 5.修改mongodb的配置文件 6.启动Mongo ...

  10. CMSampleBufferRef解析

    CMTime:64位的value,32位的scale, media的时间格式 CMVideoFormatDesc:video的格式,包括宽高.颜色空间.编码格式.SPS.PPS CVPixelBuff ...