当前位置: 首页 > article >正文

Bean的加载方式

Bean的加载方式

文章目录

  • Bean的加载方式
    • bean的xml方式声明
    • bean的加载方式二:XML+注解当时声明bean
    • bean的加载方式三:注解方式声明配置类
    • bean加载方式扩展——FactoryBean

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

<!--    xml方式声明自己开发的bean-->
    <bean id="cat" class="com.itheima.bean.Cat"/>
    <bean class="com.itheima.bean.Dog"/>
<!--    xml声明第三方开发的Bean-->
    <bean  id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"/>
    <bean  class="com.alibaba.druid.pool.DruidDataSource"/>
    <bean  class="com.alibaba.druid.pool.DruidDataSource"/>
</beans>

bean的加载方式二:XML+注解当时声明bean

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
        ">


<!--    指定加载Bean的位置,Component-->
    <context:component-scan base-package="com.itheima.bean,com.itheima.config"/>

</beans>

在这里插入图片描述

//@Component
@Configuration
public class DbConfig {
    @Bean
    public DruidDataSource dataSource(){
        DruidDataSource ds = new DruidDataSource();
        return ds;
    }
}

bean的加载方式三:注解方式声明配置类

在这里插入图片描述

@ComponentScan({"com.itheima.bean","com.itheima.config"})
public class SpringConfig3 {
}

要使用AnnotationConfigApplicationContext加载bean了

public class App3 {
    public static void main(String[] args) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig3.class);
        String[] names = ctx.getBeanDefinitionNames();
        for (String name : names) {
            System.out.println(name);
        }
    }
}

bean加载方式扩展——FactoryBean

在这里插入图片描述

public class DogFactoryBean implements FactoryBean<Dog> {
    @Override
    public Dog getObject() throws Exception {
        return new Dog();
    }

    @Override
    public Class<?> getObjectType() {
        return Dog.class;
    }

    @Override
    public boolean isSingleton() {
        return FactoryBean.super.isSingleton();
    }
}

http://www.kler.cn/news/155310.html

相关文章:

  • 利用vue3SeamlessScroll简单实现列表的无限循环滚动
  • V8引擎类型转换(VIP课程)
  • ConvBNReLU的作用
  • mac 聚焦搜索不显示
  • 三十六、seata的部署和集成
  • 前端面试JS—map 和 forEach 的区别
  • MxL3706-AQ-R 2.0通道绑定同轴网络集成电路特性
  • 模块 A:web理论测试
  • 「Python编程基础」第5章:列表
  • C语言/C++实战项目雷霆飞机(代码改进)
  • 【Unity动画】状态机中层的融合原理与用法详解
  • 如何在Rocky Linux中安装nmon
  • Oracle(2-7)Instance and Media Recovery Structures
  • DAPP【】nodejs安装与npm路径更换
  • redis.conf官方配置文件及sentinel.conf官方配置文件
  • 基于Python实现的滑动验证码自动识别工具源码
  • 【Vue3】源码解析-虚拟DOM
  • Vue2生命周期、Vue3生命周期及其对比
  • LeetCode的几道题
  • 程序员の养生之道
  • map优化对象数组
  • ThinkPHP 5 中,你可以使用定时任务调度器(TaskScheduler)来执行其他定时任务
  • Linux:动态查看服务器磁盘IO使用情况(IOTOP)
  • 若依框架分页
  • 栈和队列算法总结
  • springboot 2.4.4集成 hikari连接池多数据源实例
  • React-hook-form-mui (二):表单数据处理
  • 拥抱变化,良心AI工具推荐
  • 【物联网无线通信技术】ZigBee从理论到实践(CC2530)
  • Docker下安装MySQL