首页 > 其他分享 >spring的依赖注入

spring的依赖注入

时间:2022-09-21 10:22:14浏览次数:61  
标签:依赖 spring jar 332 maven bean jdks 注入

依赖注入

1、依赖:指bean对象创建于容器,bean对象创建依赖于资源

2、注入:指bean对象所依赖的资源,由容器来设置和装配

  • 构造器注入
  • set注入

DI依赖注入

​ IOC的一个重点是在系统运行中,动态的向某个对象提供它所需要的其他对象。这一点是通过DI(Dependency Injection,依赖注入)来实现的。在生产bean的过程中,需要解决bean之间的依赖问题,才引入了依赖注入(DI)这种技术。也就是说依赖注入是beanFactory生产bean时为了解决bean之间的依赖的一种技术而已。

对象如何生成(BeanFactory和ApplicationContext的区别)
刚说的IOC就是由Spring来接手对象的创建、生命周期和对象间的关系。

Spring的本质就是一个bean工厂或者说bean容器,它按照我们的需求创作出各种各样的bean。
beanFactory会在bean的生命周期的各个阶段中对bean进行各种管理,并且spring将这些阶段通过各种接口暴露给我们,让我们可以对bean进行各种处理,我们只要让bean实现对应的接口,那么spring就会在bean的生命周期调用我们实现的接口来处理该bean。

BeanFactory和ApplicationContext
在 spring 容器中,BeanFactory 接口是 IOC 容器要实现的最基础的接口,定义 了管理 bean 的最基本的方法。主要是面向spring框架本身,不支持AOP和web功能. 在获取bean时才会创建bean 。ApplicationContext 继承BeanFactory接口,对功能进行扩展. 支持aop,web等插件,面向开发层面, 可以在框架启动时,就可以创建bean。如果说BeanFactory是心脏,那么ApplicationContext就是躯体。

案例

1、创建一个Student类

public class Student {
}

2、创建一个School类

import java.util.*;

public class School {
    private String name;
    private Student student;
    private String[] array;
    private List<String> list;
    private Map<String,String> map;
    private Set<String> set;
    private Properties info;

    public String getName() {
        return name;
    }

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

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    public String[] getArray() {
        return array;
    }

    public void setArray(String[] array) {
        this.array = array;
    }

    public List<String> getList() {
        return list;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

    public Map<String, String> getMap() {
        return map;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

    public Set<String> getSet() {
        return set;
    }

    public void setSet(Set<String> set) {
        this.set = set;
    }

    public Properties getInfo() {
        return info;
    }

    public void setInfo(Properties info) {
        this.info = info;
    }

    @Override
    public String toString() {
        return "School{" +
                "name='" + name + '\'' +
                ", student=" + student +
                ", array=" + Arrays.toString(array) +
                ", list=" + list +
                ", map=" + map +
                ", set=" + set +
                ", info=" + info +
                '}';
    }
}

3、配置bean.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <bean id="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
    </bean>
    
    <!-- more bean definitions go here -->

</beans>

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="student" class="pojo.Student">
    <property name="student" value="华晨宇"></property>
</bean>

    <bean id="school" class="pojo.School">
        <property name="name" value="鞠婧祎"></property>

        <property name="student" ref="student"></property>

        <property name="array">
            <array>
                <value>湖北</value>
                <value>湖南</value>
                <value>北京</value>
            </array>
        </property>

        <property name="list">
            <list>
                <value>天门</value>
                <value>潜江</value>
                <value>仙桃</value>
            </list>
        </property>

        <property name="map">
            <map>
                <entry key="天" value="123"></entry>
                <entry key="地" value="456"></entry>
            </map>
        </property>

        <property name="set">
            <set>
                <value>Java</value>
                <value>python</value>
                <value>Android</value>
            </set>
        </property>

        <property name="info">
            <props>
                <prop key="driver">com.mysql.cj.jdbc.Driver</prop>
                <prop key="url">jdbc:mysql://localhost:3306/mybatis?useUnicode=true&amp;characterEncoding=utf8&amp;serverTimezone=GMT%2B8&amp;useSSL=false</prop>
                <prop key="username">root</prop>
                <prop key="password">root</prop>
            </props>
        </property>
    </bean>
</beans>

4、测试

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pojo.School;

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        School school = (School) context.getBean("school");
        System.out.println(school.toString());
    }
}

5、测试结果

D:\.jdks\corretto-1.8.0_332\bin\java.exe "-javaagent:F:\IntelliJ IDEA Community Edition 20222.3\ideaIU-2020.2.3.win\lib\idea_rt.jar=55096:F:\IntelliJ IDEA Community Edition 20222.3\ideaIU-2020.2.3.win\bin" -Dfile.encoding=UTF-8 -classpath D:\.jdks\corretto-1.8.0_332\jre\lib\charsets.jar;D:\.jdks\corretto-1.8.0_332\jre\lib\ext\access-bridge-64.jar;D:\.jdks\corretto-1.8.0_332\jre\lib\ext\cldrdata.jar;D:\.jdks\corretto-1.8.0_332\jre\lib\ext\dnsns.jar;D:\.jdks\corretto-1.8.0_332\jre\lib\ext\jaccess.jar;D:\.jdks\corretto-1.8.0_332\jre\lib\ext\jfxrt.jar;D:\.jdks\corretto-1.8.0_332\jre\lib\ext\localedata.jar;D:\.jdks\corretto-1.8.0_332\jre\lib\ext\nashorn.jar;D:\.jdks\corretto-1.8.0_332\jre\lib\ext\sunec.jar;D:\.jdks\corretto-1.8.0_332\jre\lib\ext\sunjce_provider.jar;D:\.jdks\corretto-1.8.0_332\jre\lib\ext\sunmscapi.jar;D:\.jdks\corretto-1.8.0_332\jre\lib\ext\sunpkcs11.jar;D:\.jdks\corretto-1.8.0_332\jre\lib\ext\zipfs.jar;D:\.jdks\corretto-1.8.0_332\jre\lib\jce.jar;D:\.jdks\corretto-1.8.0_332\jre\lib\jfr.jar;D:\.jdks\corretto-1.8.0_332\jre\lib\jfxswt.jar;D:\.jdks\corretto-1.8.0_332\jre\lib\jsse.jar;D:\.jdks\corretto-1.8.0_332\jre\lib\management-agent.jar;D:\.jdks\corretto-1.8.0_332\jre\lib\resources.jar;D:\.jdks\corretto-1.8.0_332\jre\lib\rt.jar;F:\Spring\Spring_Maven\target\test-classes;F:\Spring\Spring_Maven\target\classes;F:\Maven\apache-maven-3.6.3-bin\apache-maven-3.6.3\maven-repo\org\springframework\spring-webmvc\5.3.22\spring-webmvc-5.3.22.jar;F:\Maven\apache-maven-3.6.3-bin\apache-maven-3.6.3\maven-repo\org\springframework\spring-aop\5.3.22\spring-aop-5.3.22.jar;F:\Maven\apache-maven-3.6.3-bin\apache-maven-3.6.3\maven-repo\org\springframework\spring-beans\5.3.22\spring-beans-5.3.22.jar;F:\Maven\apache-maven-3.6.3-bin\apache-maven-3.6.3\maven-repo\org\springframework\spring-context\5.3.22\spring-context-5.3.22.jar;F:\Maven\apache-maven-3.6.3-bin\apache-maven-3.6.3\maven-repo\org\springframework\spring-core\5.3.22\spring-core-5.3.22.jar;F:\Maven\apache-maven-3.6.3-bin\apache-maven-3.6.3\maven-repo\org\springframework\spring-jcl\5.3.22\spring-jcl-5.3.22.jar;F:\Maven\apache-maven-3.6.3-bin\apache-maven-3.6.3\maven-repo\org\springframework\spring-expression\5.3.22\spring-expression-5.3.22.jar;F:\Maven\apache-maven-3.6.3-bin\apache-maven-3.6.3\maven-repo\org\springframework\spring-web\5.3.22\spring-web-5.3.22.jar;F:\Maven\apache-maven-3.6.3-bin\apache-maven-3.6.3\maven-repo\junit\junit\4.12\junit-4.12.jar;F:\Maven\apache-maven-3.6.3-bin\apache-maven-3.6.3\maven-repo\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar MyTest
School{name='鞠婧祎', student=Student{student=华晨宇}, array=[湖北, 湖南, 北京], list=[天门, 潜江, 仙桃], map={天=123, 地=456}, set=[Java, python, Android], info={password=root, url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false, driver=com.mysql.cj.jdbc.Driver, username=root}}
User{name='胡九年', age=56}

Process finished with exit code 0

标签:依赖,spring,jar,332,maven,bean,jdks,注入
From: https://www.cnblogs.com/lovesz/p/16714682.html

相关文章

  • Spring Boot - Undertow容器启动
    Undertow简介Undertow是红帽公司开发的一款基于NIO的高性能Web嵌入式服务器Untertow的特点:轻量级:它是一个Web服务器,但不像传统的Web服务器有容器概念,它由两......
  • springboot逆向工程
    通过逆向工程少写很多代码generatorConfig.xml<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEgeneratorConfigurationPUBLIC"-//mybatis.org//DTDMyBati......
  • Spring Boot 两种部署到服务器的方式
    jar包(官方推荐)jar包方式启动,也就是使用springboot内置的tomcat运行。服务器上面只要你配置了jdk1.8及以上,就ok。不需要外置tomcat1.打成jar包2.将jar包放到任意目录......
  • 微服务网关 Spring Cloud Gateway
    什么是网关假设你现在要做一个电商应用,前端是移动端的APP,后端是各种微服务。那你可能某个页面需要调用多个服务的数据来展示。如果没有网关,你的系统看起来就是这个样子的:......
  • SpringIOC的理解
    IOC(InversionOfControl):控制反转控制:即对资源(如一个Java类)的获取方式获取方式可以分为两种主动获取在Spring之前我们想要获取一个类都是自己创建,即new出这个类......
  • Spring-01:初学资源准备及初始化spring项目
    1资源准备在学习Spring框架前,你需要先准备好相应的学习资源,以帮助更好地掌握基础知识,快速解决过程中遇到的问题。①spring官方文档https://spring.io/这里你可以快速......
  • SpringMVC学习笔记(五)
    注解配置MVC使用配置类和注解联合使用的方式代替xml配置文件 在Servlet3.0环境中,容器会在类路径中查找实现javax.servlet.ServletContainerInitializer接口的类,如果找......
  • Spring框架
    Spring框架Java应用最广泛的框架,它的成功来源于理念,而不是技术本身IOC:控制反转AOP:面向切面编程DI:依赖注入非侵入式设计:不需要继承框架提供的任何一个类,即使跟换框......
  • 2022-9-20 Spring学习笔记
    目录1.Spring1.1JavaBean1.2Spring的优势1.3将对象放入IOC容器配置类赋值的方法根据不同类型的赋值作用域自动装配注解1.4类型转换1.SpringSpring框架是Java应用最广......
  • vue.js下载依赖包node_modules
    https://blog.csdn.net/qq_36509946/article/details/118878026node_modules  npm加载的项目依赖模块 在很多时候,我们要拷贝或上传一份vue的代码时,通常会删掉......