首页 > 其他分享 >spring自动注入中byName和byType

spring自动注入中byName和byType

时间:2024-01-05 20:57:22浏览次数:27  
标签:byType name 配置文件 spring byName id student public String

spring自动注入中byName和byType
1,byName:
其实byName根据被注入的名称作为bean名称作为依赖查找,并将对象设置到该属性。(根据bean的id进行查找)

首先创建Student类:

public class Student {
    private String name;
    private String id;

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

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

    public void setId(String id) {
        this.id = id;
    }
}
然后创建School类:

public class School {
    private String name;
    private String addrees;

    private Student student;

    @Override
    public String toString() {
        return "School{" +
                "name='" + name + '\'' +
                ", addrees='" + addrees + '\'' +
                ", student=" + student +
                '}';
    }

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

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

    public void setAddrees(String addrees) {
        this.addrees = addrees;
    }
}
创建.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 class="java01.Student" id="student">
<property name="name" value="小明"/>
    <property name="id" value="022300190405"/>
</bean>

    <bean class="java01.School" id="school" autowire="byName">
        <property name="name" value="北师大"/>
        <property name="addrees" value="北京"/>
    </bean>
</beans>

创建测试类:

public class tests02 {
    @Test
    public void test(){
        ApplicationContext ioc = new ClassPathXmlApplicationContext("test01/teach-ioc.xml");
        Object bean01 = ioc.getBean("school");
    System.out.println(bean01);
    }
}

2,byType:
byType通过属性的类型查找javabean依赖的对象并为其注入

为应用指定多个 Spring 配置文件
在实际应用里,随着应用规模的增加,系统中 Bean 数量也大量增加,导致配置文件变 得非常庞大、臃肿。为了避免这种情况的产生,提高配置文件的可读性与可维护性,可以将 Spring 配置文件分解成多个配置文件。

包含关系的配置文件:
多个配置文件中有一个总文件,总配置文件将各其它子文件通过引入。在 Java 代码中只需要使用总配置文件对容器进行初始化即可。

 

创建新的.xml配置文件(命名tatle.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">
<import resource="classpath*:test01/spring-school.xml"/>
    <import resource="classpath*:/test01/spring-student.xml"/><!--导入配置文件-->
</beans>

创建测试类:

public class tests03 {
    @Test
    public void test()
    {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("test01/tatle.xml");
        Object bean = ioc.getBean("student");//这里的school是spring-student.xml中的bean
    System.out.println(bean);
    }
}

此时ioc.getbean调用的是spring-student中的id为student的bean。

运行结果:

 

当然也有可能会有小错误:

 

这是因为:

 

tatle.xml之前不要忘了它所在的包名。

 

标签:byType,name,配置文件,spring,byName,id,student,public,String
From: https://www.cnblogs.com/lidabo/p/17948057

相关文章

  • springMVC的常见注解,以及注解的作用。@Controller,@RestController,@RequestMapping,@
    目录注:使用注解,必须要开启注解包扫描1.@Controller2.@RequestMapping3.@PathVariable4.@RequestParam5.@RequestHeader6.@CookieValue7.@RequestBody该注解的作用8.@ResponseBody9.@RestController注:使用注解,必须要开启注解包扫描在MVC核心配置中开启注解包扫描<!--  配置包......
  • springmvc的五个常用注解?
    1、@Controller在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ,然后再把该Model 返回给对应的View 进行展示。在SpringMVC 中提供了一个非常简便的定义Controller 的方法,你无需继承特......
  • 时代变了,Spring 官方抛弃了 Java 8!
    先容许我吐槽一句:Spring官方,窝草nm!原谅我很愤怒!最近编程导航星球和群友们反复问一个问题:为啥用IDEA创建SpringBoot项目时,不能选择Java8了?我本来以为是IDEA版本更新导致的Bug,开始还没在意。直到我今天自己初始化项目时才发现:卧槽,Java8真没了?!具体一点,应该是使用IDEA......
  • Spring总结
    Spring框架1、简介Spring:春天---->给软件行业带来了春天2002:首次推出了Spring框架的雏形spring框架即以interface21框架为基础,经过重新设计,并不断丰富其内涵,于2004年3月24日发布1.0正式版.RodJohnson,springframework创始人,很难想象这个人的学历,他学音乐学Sprin......
  • springboot 打包本地jar包或外部依赖打不进去问题
    分为两种情况一、打war包的情况引入依赖<dependency><groupId>com.xxxx</groupId><artifactId>xxxxx</artifactId><version>1.0</version><scope>system</scope><systemPath>${basedir}/lib/xxxxx.jar&l......
  • Spring学习记录之手写Spring框架
    Spring学习记录之手写Spring框架前言这篇文章是我第二次学习b站老杜的spring相关课程所进行的学习记录,算是对课程内容及笔记的二次整理,以自己的理解方式进行二次记录,其中理解可能存在错误,欢迎且接受各位大佬们的批评指正;关于本笔记,只是我对于相关知识遗忘时快速查阅了解使用,至......
  • Spring cloud No spring.config.import property has been defined
    *[解决SpringCloud2021.0.5版本,使用nacos做配置中心,报Nospring.config.importpropertyhasbeendefined的问题\_addaspring.config.import=nacos:propertytoyour-CSDN博客](https://blog.csdn.net/weixin_44951259/article/details/127929284)*[SpringCloud202......
  • Springboot 2.7 open api:swagger | knife4j | spring doc
    *[集成SpringDoc接口文档和knife4j|SpringBoot2.7.2实战基础-掘金](https://juejin.cn/post/7201195677128687674)*[Springboot2.7集成Swagger增强版接口框架Knife4j4.3+springdocOpenApi3.0\_knife4jspringboot2.7-CSDN博客](https://blog.csdn.net/Mrqi......
  • 记录Springboot中向企业微信指定人员发送含链接的消息
    背景:从海康智能门禁获取到了进入教室的人脸信息,由此得到一批用户List,等会儿就要实时向这批用户发送消息“***,您已进入**教室,请填写使用情况表<ahref="****">”。  过程:读了微信的开发者文档,摸索着写了测试代码。在debug时,发现微信传来的是{"errcode":60020,"errmsg":"not......
  • 用Spring Boot 3.2虚拟线程搭建静态文件服务器有多快?
    SpringBoot3.2于2023年11月大张旗鼓地发布,标志着Java开发领域的一个关键时刻。这一突破性的版本引入了一系列革命性的功能,包括:虚拟线程:利用ProjectLoom的虚拟线程释放可扩展性,从而减少资源消耗并增强并发性。NativeImage支持:通过NativeImage编译制作速度极快的应......