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

常见的Bean工厂后置处理器

此代码在jdk11上测试通过,SpringBoot版本为2.7.14

1.上代码

导入坐标

<dependencies><!-- spring数据坐标 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-rest</artifactId></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.4</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.16</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.33</version></dependency>
</dependencies>

1.测试类代码

public class Dome05Application {public static void main(String[] args) {GenericApplicationContext genericApplicationContext = new GenericApplicationContext();genericApplicationContext.registerBean("config", Config.class);// Bean工厂后置处理器// 可以识别@ComponentScan,@Been,@Import等注解genericApplicationContext.registerBean(ConfigurationClassPostProcessor.class);// 这个是解析@MapperScanner注解// genericApplicationContext.registerBean(MapperScannerConfigurer.class);genericApplicationContext.refresh();for (String beanDefinitionName : genericApplicationContext.getBeanDefinitionNames()) {System.out.println(beanDefinitionName);}genericApplicationContext.close();}
}

2.创建Bean01

public class Bean01 {}

3.创建ComponentBean

@Component
public class ComponentBean {public ComponentBean() {System.out.println("ComponentBean 构造方法====被Spring加载了");}
}

4.创建配置类

@Configuration
@ComponentScan("com.andy.xxx.xxx")  // 第三段代码ComponentBean 所在的包
public class Config {@Beanpublic Bean01 bean01() {return new Bean01();}@Beanpublic SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) {SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();sqlSessionFactoryBean.setDataSource(dataSource);return sqlSessionFactoryBean;}@Bean(initMethod = "init")public DruidDataSource dataSource() {DruidDataSource druidDataSource = new DruidDataSource();druidDataSource.setUrl("jdbc:mysql://127.0.0.1:3306/study_test");druidDataSource.setUsername("root");druidDataSource.setPassword("root");return druidDataSource;}
}

2.运行结果

在这里插入图片描述

http://www.lryc.cn/news/254835.html

相关文章:

  • 代码随想录算法训练营第四十二天| 416 分割等和子集
  • memmove 和 memcpy的区别
  • C实现的双向链表队列
  • 自适应中值滤波器的python代码实现-----冈萨雷斯数字图像处理
  • Python作业答疑_6.22~6.25
  • Uber Go 语言编码规范
  • UniRepLKNet:用于音频、视频、点云、时间序列和图像识别的通用感知大内核ConvNet
  • Http协议与Tomcat
  • Spring AOP从入门到精通
  • Tap虚拟网卡
  • 【数电笔记】53-与非门构成的基本RS触发器
  • kubernetes(k8s)容器内无法连接同所绑定的Service ClusterIP问题记录
  • Hadoop入门学习笔记
  • 堆栈,BSS,DATA,TEXT
  • Java八股文面试全套真题【含答案】-JSON篇
  • 数据库管理-第119期 记一次迁移和性能优化(202301130)
  • 【云原生-K8s】镜像漏洞安全扫描工具Trivy部署及使用
  • 【Docker】Swarm的ingress网络
  • gcc安全特性之FORTIFY_SOURCE
  • 【JUC】二十、volatile变量的特点与使用场景
  • 软件工程期末复习(2)
  • [vue3] 使用 vite 创建vue3项目的详细流程
  • #HarmonyOS:软件安装window和mac预览Hello World
  • nginx 一键切换停机维护页面 —— 筑梦之路
  • Python作业答疑
  • 计算机网络实用工具之Hydra
  • AUTOSAR 入门
  • 新版IDEA中,module模块无法被识别,类全部变成咖啡杯无法被识
  • vue.js el-table 动态单元格列合并
  • word模板导出word文件