DoubleLi

qq: 517712484 wx: ldbgliet

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  4737 随笔 :: 2 文章 :: 542 评论 :: 1615万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

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之前不要忘了它所在的包名。

 

posted on   DoubleLi  阅读(197)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2022-01-05 AV_PIX_FMT_YUV420P与AV_PIX_FMT_YUVJ420P
2015-01-05 Darwin Streaming Server Relay Setting
2015-01-05 Darwin Streaming Server 6.0.3安装、订制、插件或模块
2015-01-05 shell脚本中常见的一些特殊符号和作用详解
2012-01-05 JavaScript:prototype属性使用方法
2012-01-05 关于JavaScript中apply与call的用法意义及区别
2012-01-05 Javascript的this用法总结
点击右上角即可分享
微信分享提示