企业级开发框架  

  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. 使用内网映射工具Holer将本地的Web应用映射到公网上访问

    Holer exposes local servers behind NATs and firewalls to the public internet over secure tunnels. Su ...

  2. Proto3:Techniques

    本文描述处理Protocol Buffer常用到的一些设计模式.你也可以给Protocol Buffers discussion group发送设计或使用问题. 流式多条消息 如果你想将多个消息写入到 ...

  3. HTTP Continuation or non-HTTP traffic

    发现一个 HTTP      Continuation or non-HTTP traffic的数据包,之前没有碰到过.不懂其意义,一看长度,显示1460,与TCP segment of a reas ...

  4. Hexo搭建个人博客(一)— 前期准备

    最近几个月自学python的过程中,搜索爬虫资料的时候关注了xlzd的博客,为我开启了一片新世界,之后慢慢收藏了各方高人的博客.搭建一个自己博客的萌芽也悄然种下,也许是命运使然,在逛知乎的时候偶然间看 ...

  5. Dizcuz站点部署-包教会

      Dizcuz站点部署-包教会-有需要请联系小编! 小编微信号:wvqusrtg

  6. 软工 实验一 Git代码版本管理

    实验目的: 1)了解分布式版本控制系统的核心机理: 2)   熟练掌握git的基本指令和分支管理指令: 实验内容: 1)安装git 2)初始配置git ,git init git status指令 3 ...

  7. Python——6切片

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

  8. Git私服搭建

    Git私服搭建 一.Git服务器搭建方式 GIT是一个分布式版本管理系统,既然是分布那么必定会涉及远程通信,那么GIT是采用什么协议进行远程通信? Git支持的四种通信协议: Local(本地协议) ...

  9. [红日安全]Web安全Day5 - 任意文件上传实战攻防

    本文由红日安全成员: MisakiKata 编写,如有不当,还望斧正. 大家好,我们是红日安全-Web安全攻防小组.此项目是关于Web安全的系列文章分享,还包含一个HTB靶场供大家练习,我们给这个项目 ...

  10. 优秀的Spring Cloud开源项目整理推荐

    无论是对于初学者,还是有一定工作经验的程序员来讲,Spring Cloud开源项目都是一笔宝贵的财富.下面给大家整理了十个优秀的开源项目,分别是spring-cloud-examples.spaasc ...