广告
返回顶部
首页 > 资讯 > 精选 >Spring bean有哪几种注入方式
  • 273
分享到

Spring bean有哪几种注入方式

2023-06-20 16:06:22 273人浏览 八月长安
摘要

这篇文章主要讲解了“spring bean有哪几种注入方式”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Spring bean有哪几种注入方式”吧!目录一、Set方式注入pojo层:xml

这篇文章主要讲解了“spring bean有哪几种注入方式”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Spring bean有哪几种注入方式”吧!

目录
  • 一、Set方式注入

    • pojo层:

    • xml 文件

    • test测试

  • 二、构造函数方式注入

    • pojo层

    • xml文件

    • test测试

  • 三、注解注入

    • pojo层

    • xml文件

    • test测试

  • 四、JavaConfig 方式注入

    • pojo层

    • JavaConfig 类

    • xml文件 扫描包

    • 测试:

  • 五、Service层注入详解

    • service

    • serviceImpl

    • xml配置文件

  • 总结

    一、Set方式注入

    pojo层:

    public class Student1 {    public String name;    public String school;    public void setName(String name) {        this.name = name;    }    public void setSchool(String school) {        this.school = school;    }    @Override    public String toString() {        return "Student1{" +                "name='" + name + '\'' +                ", school='" + school + '\'' +                '}';    }}

    1.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 http://www.springframework.org/schema/beans/spring-beans.xsd">    <!--set方式注入        id是注入bean中的名字        class 是全限定类名        property 是按照set方式注入    -->    <bean id="student1" class="com.crush.pojo.Student1">        <property name="name" value="wyh2"/>        <property name="school" value="hngy1"/>    </bean></beans>

    test测试

     @Test    public void student1(){        ApplicationContext context = new ClassPathXmlApplicationContext("student1.xml");        Student1 student1 = context.getBean("student1", Student1.class);        System.out.println(student1);    }

    二、构造函数方式注入

    pojo层

    public class Student2 {    private String name;    private String school;    public Student2(String name, String school) {        this.name = name;        this.school = school;    }    @Override    public String toString() {        return "Student2{" +                "name='" + name + '\'' +                ", school='" + school + '\'' +                '}';    }}

    2.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 http://www.springframework.org/schema/beans/spring-beans.xsd">    <!--set方式注入        id是注入bean中的名字        class 是全限定类名        constructor 是按照构造方式注入        index 是按照成员变量在构造函数中的参数的第几个        name 表示成员变量名        type 表示类型        value 表示值        ref 表示引用 可引用另外一个注入到Spring的中的值    -->    <bean id="student2" class="com.crush.pojo.Student2">        <constructor-arg index="0"  name="name" type="java.lang.String" value="wyh3"/>        <constructor-arg name="school" value="hngy2"/>    </bean></beans>

    test测试

       @Test    public void student2(){        ApplicationContext context = new ClassPathXmlApplicationContext("student2.xml");        Student2 student2 = context.getBean("student2", Student2.class);        System.out.println(student2);    }

    三、注解注入

    pojo层

    @Componentpublic class Student3 {    @Value("wyh4")    private String name;    @Value("hngy3")    private String school;    @Override    public String toString() {        return "Student3{" +                "name='" + name + '\'' +                ", school='" + school + '\'' +                '}';    }}

    3.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:context="http://www.springframework.org/schema/context"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">    <!--注解方式注入        需要扫描注解在的包 注解才会生效    -->    <context:component-scan base-package="com.crush.pojo"/></beans>

    test测试

       @Test    public void student3(){        ApplicationContext context = new ClassPathXmlApplicationContext("student3.xml");        Student3 student3 = context.getBean("student3", Student3.class);        System.out.println(student3);    }

    四、JavaConfig 方式注入

    pojo层

    public class Student4 {    @Value("wyh5")    private String name;    @Value("hngy4")    private String school;    @Override    public String toString() {        return "Student4{" +                "name='" + name + '\'' +                ", school='" + school + '\'' +                '}';    }}

    JavaConfig 类

    @Configurationpublic class Student4Config {    @Bean    public Student4 student4(){        return new Student4();    }}

    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:context="http://www.springframework.org/schema/context"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">    <context:component-scan base-package="com.crush.config"/></beans>

    测试:

    @Test    public void student4(){        ApplicationContext context = new ClassPathXmlApplicationContext("student4.xml");        Student4 student4 = context.getBean("student4", Student4.class);        System.out.println(student4);    }

    五、Service层注入详解

    service

    public interface StudentService1 {    void test();}

    serviceImpl

    public class StudentService1Impl implements StudentService1{    @Override    public void test() {        System.out.println("===StudentDao1Impl===");    }}

    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 http://www.springframework.org/schema/beans/spring-beans.xsd">    <bean id="studentService1" class="com.crush.dao.StudentService1" /></beans>

    感谢各位的阅读,以上就是“Spring bean有哪几种注入方式”的内容了,经过本文的学习后,相信大家对Spring bean有哪几种注入方式这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!

    --结束END--

    本文标题: Spring bean有哪几种注入方式

    本文链接: https://www.lsjlt.com/news/298200.html(转载时请注明来源链接)

    有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

    本篇文章演示代码以及资料文档资料下载

    下载Word文档到电脑,方便收藏和打印~

    下载Word文档
    猜你喜欢
    • Spring bean有哪几种注入方式
      这篇文章主要讲解了“Spring bean有哪几种注入方式”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Spring bean有哪几种注入方式”吧!目录一、Set方式注入pojo层:xml ...
      99+
      2023-06-20
    • spring注入bean的方式有哪些
      Spring注入Bean的方式有以下几种:1. 构造器注入:通过构造器参数来注入依赖的Bean。2. Setter方法注入:通过调用...
      99+
      2023-09-05
      spring bean
    • spring框架依赖注入方式有哪几种
      Spring框架的依赖注入方式主要有三种:1. 构造函数注入(Constructor Injection):通过构造函数实现依赖注入...
      99+
      2023-08-08
      spring
    • Spring bean 四种注入方式详解
      目录一、Set方式注入pojo层:1.xml 文件test测试二、构造函数方式注入pojo层2.xml文件test测试三、注解注入pojo层3.xml文件test测试四、JavaCo...
      99+
      2022-11-12
    • Spring Bean三种注入方式详解
      在Spring容器中为一个bean配置依赖注入有三种方式:  使用属性的setter方法注入  这是最常用的方式;  使用构造器注入;  使用Filed注入(用于注解方式).Field注入是最常...
      99+
      2023-05-30
      spring bean 注入
    • Spring Bean属性注入的方式有哪些
      这篇文章主要介绍“Spring Bean属性注入的方式有哪些”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Spring Bean属性注入的方式有哪些”文章能帮助大家解决问题。属性...
      99+
      2023-07-02
    • SpringBoot通过注解注入Bean的几种方式解析
      目录1、背景xml扫描包的方式2、通过注解注入的一般形式2.1、Bean类2.2、Configuration类2.3、Test类3、通过构造方法注入Bean3.1、Bean类3.2、...
      99+
      2022-11-13
    • Spring中获取Bean对象的注入方式有哪些
      本篇内容主要讲解“Spring中获取Bean对象的注入方式有哪些”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Spring中获取Bean对象的注入方式有哪些”吧!前言获取 bean 对象也叫做对...
      99+
      2023-07-05
    • Spring中获取Bean对象的三种注入方式与两种注入方法详解
      目录前言获取Bean对象的三种注入方式属性注⼊构造⽅法注⼊Setter 注⼊属性注⼊、构造⽅法注⼊和Setter 注⼊有什么区别呢?两种注入方法总结前言 获取 bean 对象也叫做对...
      99+
      2023-03-08
      spring什么时候注入bean spring注入的几种方式 注入bean
    • Spring容器注入bean的方法有哪些
      本文小编为大家详细介绍“Spring容器注入bean的方法有哪些”,内容详细,步骤清晰,细节处理妥当,希望这篇“Spring容器注入bean的方法有哪些”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。@Compon...
      99+
      2023-07-05
    • spring的加载方式有哪几种
      Spring的加载方式有三种:基于XML的配置加载、基于注解的配置加载和基于Java配置的加载。1. 基于XML的配置加载:通过编写...
      99+
      2023-09-28
      spring
    • Spring创建bean实例的几种方式分享
      目录前言环境通过bean的class属性创建实例(带参构造器)工厂方法(静态工厂方法)工厂方法(实例工厂方法)工厂bean总结前言 Spring常见的创建bean实例的方式有: 1....
      99+
      2022-11-13
    • Springboot依赖注入Bean的三种方式,final+构造器注入Bean
      文章目录 Springboot依赖注入Bean的方式一、Field 注入/属性注入二、set注入三、构造器注入 Springboot依赖注入Bean的方式 一、Field 注入/属性注入 @Autowired注解的一大使用场...
      99+
      2023-08-20
      spring boot java spring注入Bean final+构造方法
    • Spring注入方式有哪些
      本篇内容介绍了“Spring注入方式有哪些”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!Spring的三种注入方式属性(filed)注入这种...
      99+
      2023-06-25
    • Spring创建bean的几种方式及使用场景
      目录1、@Configuration注解2、@Bean注解3、@Import注解3.1、导入标记有@Configuration的配置类3.2、导入ImportSelector的实现类...
      99+
      2023-05-18
      Spring创建bean Spring bean
    • 详解Spring通过@Value注解注入属性的几种方式
      场景假如有以下属性文件dev.properties, 需要注入下面的tagtag=123通过PropertyPlaceholderConfigurer<bean class="org.springframework.beans.fac...
      99+
      2023-05-31
      spring value 注解
    • spring中bean实例化的三种方式 -- Spring入门(二)
      文章目录 前言1.Bean实例化简介2.bean的实例化 -- 构造方法3.bean的实例化 -- 静态工厂实例化4.bean实例化 -- 实例工厂和FactoryBean5.三种bean实例化方式的区别 总结 前言 为了...
      99+
      2023-08-19
      spring java 后端
    • spring的注入方式有哪些
      Spring的注入方式有三种:1. 构造器注入(Constructor Injection):通过构造方法来注入依赖对象。2. Se...
      99+
      2023-09-27
      spring
    • Spring IOC注入方式有哪些
      今天小编给大家分享一下Spring IOC注入方式有哪些的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。Spring IOC三...
      99+
      2023-06-03
    • Spring依赖注入的几种方式分享梳理总结
      目录环境准备设值注入构造注入总结环境 Ubuntu 22.04IntelliJ IDEA 2022.1.3JDK 17.0.3Spring 5.3.21 准备 创建Maven项目 t...
      99+
      2022-11-13
    软考高级职称资格查询
    编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
    • 官方手机版

    • 微信公众号

    • 商务合作