java spring boot 导入bean 的四种方式

1 Import导入bean的四种方式

2 代码

2.1 要导入的bean

package com.example.demo;public class MyUser {
}
package com.example.demo;public class MyRow {
}

2.2 各种方式的代码

2.2.1 @Import(MyUser.class)

package com.example.demo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import com.example.demo.MyUser;
import com.example.demo.UserConfig;
import org.springframework.context.annotation.Import;import java.util.Map;@SpringBootApplication
@Import(MyUser.class)
//@Import(UserConfig.class)
//@Import(MyImportSelecter.class)
//@Import(MyImportBeanDefinitionRegistrar.class)
public class Demo2Application {public static void main(String[] args) {ConfigurableApplicationContext configurableApplicationContext = SpringApplication.run(Demo2Application.class, args);//User user = (User) configurableApplicationContext.getBean("user");//System.out.println(user);//MyUser myUser = (MyUser) configurableApplicationContext.getBean("myUser2");MyUser myUser = (MyUser) configurableApplicationContext.getBean("myUser");System.out.println(myUser);//Map<String,MyUser> map = configurableApplicationContext.getBeansOfType(MyUser.class);//System.out.println(map);}}

2.2.2 @Import(UserConfig.class)

package com.example.demo;import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class UserConfig {@Beanpublic MyUser myUser(){return  new MyUser();}@Beanpublic MyRow myRow(){return  new MyRow();}
}
package com.example.demo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import com.example.demo.MyUser;
import com.example.demo.UserConfig;
import org.springframework.context.annotation.Import;import java.util.Map;@SpringBootApplication
//@Import(MyUser.class)
@Import(UserConfig.class)
//@Import(MyImportSelecter.class)
//@Import(MyImportBeanDefinitionRegistrar.class)
public class Demo2Application {public static void main(String[] args) {ConfigurableApplicationContext configurableApplicationContext = SpringApplication.run(Demo2Application.class, args);//User user = (User) configurableApplicationContext.getBean("user");//System.out.println(user);//MyUser myUser = (MyUser) configurableApplicationContext.getBean("myUser2");MyUser myUser = (MyUser) configurableApplicationContext.getBean("myUser");System.out.println(myUser);//Map<String,MyUser> map = configurableApplicationContext.getBeansOfType(MyUser.class);//System.out.println(map);}}

2.2.3 @Import(MyImportSelecter.class)

package com.example.demo;import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;public class MyImportSelecter implements ImportSelector {@Overridepublic String[] selectImports(AnnotationMetadata importingClassMetadata) {return new String[]{"com.example.demo.MyUser","com.example.demo.MyRow"};}
}
package com.example.demo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import com.example.demo.MyUser;
import com.example.demo.UserConfig;
import org.springframework.context.annotation.Import;import java.util.Map;@SpringBootApplication
//@Import(MyUser.class)
//@Import(UserConfig.class)
@Import(MyImportSelecter.class)
//@Import(MyImportBeanDefinitionRegistrar.class)
public class Demo2Application {public static void main(String[] args) {ConfigurableApplicationContext configurableApplicationContext = SpringApplication.run(Demo2Application.class, args);//User user = (User) configurableApplicationContext.getBean("user");//System.out.println(user);//MyUser myUser = (MyUser) configurableApplicationContext.getBean("myUser2");MyUser myUser = (MyUser) configurableApplicationContext.getBean("myUser");System.out.println(myUser);//Map<String,MyUser> map = configurableApplicationContext.getBeansOfType(MyUser.class);//System.out.println(map);}}

2.2.4 @Import(MyImportBeanDefinitionRegistrar.class)

package com.example.demo;import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(MyUser.class).getBeanDefinition();registry.registerBeanDefinition("myUser2",beanDefinition);}
}
package com.example.demo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import com.example.demo.MyUser;
import com.example.demo.UserConfig;
import org.springframework.context.annotation.Import;import java.util.Map;@SpringBootApplication
//@Import(MyUser.class)
//@Import(UserConfig.class)
//@Import(MyImportSelecter.class)
@Import(MyImportBeanDefinitionRegistrar.class)
public class Demo2Application {public static void main(String[] args) {ConfigurableApplicationContext configurableApplicationContext = SpringApplication.run(Demo2Application.class, args);//User user = (User) configurableApplicationContext.getBean("user");//System.out.println(user);MyUser myUser = (MyUser) configurableApplicationContext.getBean("myUser2");//MyUser myUser = (MyUser) configurableApplicationContext.getBean("myUser");System.out.println(myUser);Map<String,MyUser> map = configurableApplicationContext.getBeansOfType(MyUser.class);System.out.println(map);}}

3 运行结果 仅以2.2.4的运行结果为例,其他结果都大概相同。

C:\Users\ThinkPad\.jdks\openjdk-21.0.2\bin\java.exe -XX:TieredStopAtLevel=1 -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-Dmanagement.endpoints.jmx.exposure.include=*" "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2023.3.2\lib\idea_rt.jar=54699:D:\Program Files\JetBrains\IntelliJ IDEA 2023.3.2\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath C:\Users\ThinkPad\IdeaProjects\untitled7\demo2\target\classes;C:\Users\ThinkPad\.m2\repository\org\springframework\boot\spring-boot-starter-data-redis\3.2.2\spring-boot-starter-data-redis-3.2.2.jar;C:\Users\ThinkPad\.m2\repository\org\springframework\boot\spring-boot-starter\3.2.2\spring-boot-starter-3.2.2.jar;C:\Users\ThinkPad\.m2\repository\org\springframework\boot\spring-boot\3.2.2\spring-boot-3.2.2.jar;C:\Users\ThinkPad\.m2\repository\org\springframework\spring-context\6.1.3\spring-context-6.1.3.jar;C:\Users\ThinkPad\.m2\repository\org\springframework\spring-expression\6.1.3\spring-expression-6.1.3.jar;C:\Users\ThinkPad\.m2\repository\io\micrometer\micrometer-observation\1.12.2\micrometer-observation-1.12.2.jar;C:\Users\ThinkPad\.m2\repository\io\micrometer\micrometer-commons\1.12.2\micrometer-commons-1.12.2.jar;C:\Users\ThinkPad\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\3.2.2\spring-boot-autoconfigure-3.2.2.jar;C:\Users\ThinkPad\.m2\repository\org\springframework\boot\spring-boot-starter-logging\3.2.2\spring-boot-starter-logging-3.2.2.jar;C:\Users\ThinkPad\.m2\repository\ch\qos\logback\logback-classic\1.4.14\logback-classic-1.4.14.jar;C:\Users\ThinkPad\.m2\repository\ch\qos\logback\logback-core\1.4.14\logback-core-1.4.14.jar;C:\Users\ThinkPad\.m2\repository\org\apache\logging\log4j\log4j-to-slf4j\2.21.1\log4j-to-slf4j-2.21.1.jar;C:\Users\ThinkPad\.m2\repository\org\apache\logging\log4j\log4j-api\2.21.1\log4j-api-2.21.1.jar;C:\Users\ThinkPad\.m2\repository\org\slf4j\jul-to-slf4j\2.0.11\jul-to-slf4j-2.0.11.jar;C:\Users\ThinkPad\.m2\repository\jakarta\annotation\jakarta.annotation-api\2.1.1\jakarta.annotation-api-2.1.1.jar;C:\Users\ThinkPad\.m2\repository\org\yaml\snakeyaml\2.2\snakeyaml-2.2.jar;C:\Users\ThinkPad\.m2\repository\io\lettuce\lettuce-core\6.3.1.RELEASE\lettuce-core-6.3.1.RELEASE.jar;C:\Users\ThinkPad\.m2\repository\io\netty\netty-common\4.1.105.Final\netty-common-4.1.105.Final.jar;C:\Users\ThinkPad\.m2\repository\io\netty\netty-handler\4.1.105.Final\netty-handler-4.1.105.Final.jar;C:\Users\ThinkPad\.m2\repository\io\netty\netty-resolver\4.1.105.Final\netty-resolver-4.1.105.Final.jar;C:\Users\ThinkPad\.m2\repository\io\netty\netty-buffer\4.1.105.Final\netty-buffer-4.1.105.Final.jar;C:\Users\ThinkPad\.m2\repository\io\netty\netty-transport-native-unix-common\4.1.105.Final\netty-transport-native-unix-common-4.1.105.Final.jar;C:\Users\ThinkPad\.m2\repository\io\netty\netty-codec\4.1.105.Final\netty-codec-4.1.105.Final.jar;C:\Users\ThinkPad\.m2\repository\io\netty\netty-transport\4.1.105.Final\netty-transport-4.1.105.Final.jar;C:\Users\ThinkPad\.m2\repository\io\projectreactor\reactor-core\3.6.2\reactor-core-3.6.2.jar;C:\Users\ThinkPad\.m2\repository\org\reactivestreams\reactive-streams\1.0.4\reactive-streams-1.0.4.jar;C:\Users\ThinkPad\.m2\repository\org\springframework\data\spring-data-redis\3.2.2\spring-data-redis-3.2.2.jar;C:\Users\ThinkPad\.m2\repository\org\springframework\data\spring-data-keyvalue\3.2.2\spring-data-keyvalue-3.2.2.jar;C:\Users\ThinkPad\.m2\repository\org\springframework\data\spring-data-commons\3.2.2\spring-data-commons-3.2.2.jar;C:\Users\ThinkPad\.m2\repository\org\springframework\spring-tx\6.1.3\spring-tx-6.1.3.jar;C:\Users\ThinkPad\.m2\repository\org\springframework\spring-beans\6.1.3\spring-beans-6.1.3.jar;C:\Users\ThinkPad\.m2\repository\org\springframework\spring-oxm\6.1.3\spring-oxm-6.1.3.jar;C:\Users\ThinkPad\.m2\repository\org\springframework\spring-aop\6.1.3\spring-aop-6.1.3.jar;C:\Users\ThinkPad\.m2\repository\org\springframework\spring-context-support\6.1.3\spring-context-support-6.1.3.jar;C:\Users\ThinkPad\.m2\repository\redis\clients\jedis\5.2.0-alpha2\jedis-5.2.0-alpha2.jar;C:\Users\ThinkPad\.m2\repository\org\slf4j\slf4j-api\2.0.11\slf4j-api-2.0.11.jar;C:\Users\ThinkPad\.m2\repository\org\apache\commons\commons-pool2\2.12.0\commons-pool2-2.12.0.jar;C:\Users\ThinkPad\.m2\repository\org\json\json\20231013\json-20231013.jar;C:\Users\ThinkPad\.m2\repository\com\google\code\gson\gson\2.10.1\gson-2.10.1.jar;C:\Users\ThinkPad\IdeaProjects\untitled7\demo1\target\classes;C:\Users\ThinkPad\.m2\repository\org\springframework\spring-core\6.1.3\spring-core-6.1.3.jar;C:\Users\ThinkPad\.m2\repository\org\springframework\spring-jcl\6.1.3\spring-jcl-6.1.3.jar com.example.demo.Demo2Application.   ____          _            __ _ _/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/  ___)| |_)| | | | | || (_| |  ) ) ) )'  |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot ::                (v3.2.2)2024-01-30T22:17:31.449+08:00  INFO 6600 --- [           main] com.example.demo.Demo2Application        : Starting Demo2Application using Java 21.0.2 with PID 6600 (C:\Users\ThinkPad\IdeaProjects\untitled7\demo2\target\classes started by ThinkPad in C:\Users\ThinkPad\IdeaProjects\untitled7)
2024-01-30T22:17:31.452+08:00  INFO 6600 --- [           main] com.example.demo.Demo2Application        : No active profile set, falling back to 1 default profile: "default"
2024-01-30T22:17:31.906+08:00  INFO 6600 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode
2024-01-30T22:17:31.909+08:00  INFO 6600 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2024-01-30T22:17:31.932+08:00  INFO 6600 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 10 ms. Found 0 Redis repository interfaces.
2024-01-30T22:17:32.585+08:00  INFO 6600 --- [           main] com.example.demo.Demo2Application        : Started Demo2Application in 1.575 seconds (process running for 2.359)
com.example.demo.MyUser@27df95e
{myUser=com.example.demo.MyUser@b46e103, myUser2=com.example.demo.MyUser@27df95e}Process finished with exit code 0

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/656865.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

低功耗蓝牙(BLE) 和 经典蓝牙(SPP) 的区别

低功耗蓝牙(BLE) vs 经典蓝牙(SPP) 区别项低功耗蓝牙(BLE)经典蓝牙(SPP 串行端口协议)蓝牙版本蓝牙版本 > 4.0&#xff0c;又称蓝牙低功耗、蓝牙智能经典蓝牙2.0 或更早版本&#xff0c;经典配对模式在两台蓝牙设备之间建立虚拟串口数据连接&#xff0c;提供一种简单而直接…

DML的原理:一篇文章让你豁然开朗

推荐阅读 给软件行业带来了春天——揭秘Spring究竟是何方神圣&#xff08;一&#xff09; 给软件行业带来了春天——揭秘Spring究竟是何方神圣&#xff08;二&#xff09; 文章目录 推荐阅读DML 数据操纵语言INSERT语句UPDATE语句DELETE语句SELECT语句 DML 数据操纵语言 DML是…

【前端】防抖和节流

防抖 防抖用于限制连续触发的事件的执行频率。当一个事件被触发时,防抖会延迟一定的时间执行对应的处理函数。如果在延迟时间内再次触发了同样的事件,那么之前的延迟执行将被取消,重新开始计时。 总结:在单位时间内频繁触发事件,只有最后一次生效 场景 :用户在输入框输…

消息中间件RabbitMQ介绍

一、基础知识 1. 什么是RabbitMQ RabbitMQ是2007年发布&#xff0c;是一个在AMQP(高级消息队列协议)基础上完成的&#xff0c;简称MQ全称为Message Queue, 消息队列&#xff08;MQ&#xff09;是一种应用程序对应用程序的通信方法&#xff0c;由Erlang&#xff08;专门针对于大…

sqli-labs部署及sqli-labs靶场第一关

部署 一、环境安装 1.下载phpstudy&#xff0c;下载链接&#xff1a;小皮面板(phpstudy) - 让天下没有难配的服务器环境&#xff01; &#xff0c;傻瓜式的安装过后打开软件进入如下界面&#xff0c;我们开启nginx和mysql &#xff01;&#xff01;&#xff01;&#xff0…

金蝶云星空AppDesigner.AppDesignerService.RecordCurDevCodeInfo RCE漏洞

免责声明&#xff1a;文章来源互联网收集整理&#xff0c;请勿利用文章内的相关技术从事非法测试&#xff0c;由于传播、利用此文所提供的信息或者工具而造成的任何直接或者间接的后果及损失&#xff0c;均由使用者本人负责&#xff0c;所产生的一切不良后果与文章作者无关。该…

第38期 | GPTSecurity周报

GPTSecurity是一个涵盖了前沿学术研究和实践经验分享的社区&#xff0c;集成了生成预训练Transformer&#xff08;GPT&#xff09;、人工智能生成内容&#xff08;AIGC&#xff09;以及大型语言模型&#xff08;LLM&#xff09;等安全领域应用的知识。在这里&#xff0c;您可以…

RuoYi微服务部署运行报错

根据官网的部署文档&#xff0c;进行部署&#xff0c; 1、创建数据库、 2、修改工程里面的配置文件 3、修改nacos服务器里面的连接mysql的配置 4、修改nacos配置中心的配置&#xff0c;主要是改连mysql、redis之类的配置 启动getway 报Client not connected, current sta…

STL-priority_queue

文档 目录 1.关于priority_queued1的定义 2.priority_queue的使用 1.关于priority_queued1的定义 1. 优先队列是一种容器适配器&#xff0c;根据严格的弱排序标准&#xff0c;它的第一个元素总是它所包含的元素中最大的。 2. 此上下文类似于堆&#xff0c;在堆中可以随时插入元…

uniapp将方法挂载到全局

前言 首先需要有一个自己封装的方法,话不多说,直接上代码! 方法文件(common.js) const getnav (page, type, param token) > {// type 判断是否 需要验证登录if (!page) return uni.showModal({title: 提示,content: 功能暂未开通~,showCancel: false})let user uni.g…

什么是数据API接口,数据API有哪些应用?

​自2020年4月“数据”正式被纳入生产要素范围以来&#xff0c;已经和其它生产要素一起融入经济价值创造过程&#xff0c;近年来我国数据交易市场规模迅速增长&#xff0c;数据需求逐年扩增&#xff0c;“数据”日益成为推动数字中国建设和加快数字经济发展的重要战略资源。 作…

JAVA和C#怎么开发SECS/GEM:recipe配方处理 S7F1、S7F19

recipe是什么内容呢&#xff1f; recipe是机台加工不同产品时的对应程式&#xff0c;指的是由制造工程师提前在机台上设置&#xff0c;并且EAP控制生产时会自动根据货的类型选择并控制机台按照制造工程师提前设置的方式进行加工。 recipe也称为配方&#xff0c;配方是怎么来的…

自学C语言-7

第7章 循环控制 生活中总会有许多简单而重复的工作&#xff0c;为完成这些重复性工作&#xff0c;需要花费很多时间。使用循环语句来处理程序开发中简单、重复性的工作是最好不过的了。 本章致力于使读者了解while、do…while和for3种循环结构的特点&#xff0c;以及转移语句的…

python绘制螺旋线

看书看到的&#xff0c;好有意思 import turtle colors [red,purple,blue,green,yellow,orange] t turtle.Pen() turtle.bgcolor(black) for x in range(360):t.pencolor(colors[x % 6])t.width(x/1001)t.forward(x)t.left(59) 这是效果图&#xff0c;真好看 import turtle …

React和Vue实现路由懒加载

在React和Vue中&#xff0c;实现路由懒加载&#xff08;Lazy Loading&#xff09;的方法和代码示例如下&#xff1a; React 在React中&#xff0c;你可以使用React.lazy和Suspense组件来实现路由懒加载。下面是一个简单的示例&#xff1a; import React, { Suspense } from …

新建VM虚拟机-安装centOS7-连接finalshell调试

原文 这里有问题 首先进入/etc/sysconfig/network-scripts/目录 cd /etc/sysconfig/network-scripts/ 然后编辑文件 ifcfg-ens33 vi ifcfg-ens33

时间复杂度解释

时空复杂度概述 首先o(1), o(n), o(logn), o(nlogn)是用来表示对应算法的时间复杂度,这是算法的时间复杂度的表示。不仅仅用于表示时间复杂度&#xff0c;也用于表示空间复杂度。 算法复杂度分为时间复杂度和空间复杂度。其作用&#xff1a; 时间复杂度是指执行这个算法所需要…

力扣712. 两个字符串的最小ASCII删除和

动态规划 思路&#xff1a; 假设 dp[i][j] 是 s1 长度 i 和 s2 长度 j 两个字符串的最小 ASCII 删除和&#xff1b;dp[i][j] 可以由&#xff1a; 如果 s1 的第 i 个字符&#xff08;s1[i - 1]&#xff09;和 s2 的第 j 个字符&#xff08;s2[j - 1]&#xff09;不相等&#xf…

Python中的递归函数是什么

Python 递归函数 递归的特性&#xff1a; 1.调用自身函数 2.有一个结束条件 3.递归效率不高&#xff0c;可能会导致栈溢出(函数调用是通过栈这种数据结构实现的&#xff0c;每进入一个函数调用&#xff0c;栈就会增加一层栈帧&#xff0c;函数每返回&#xff0c;栈就会减少…

idea项目如何上传gitee

1.先创建仓库 2.从gitee上面clone下来 3.配置一下git 4.在idea里面安装Gitee插件&#xff08;安装完插件重启一下&#xff09; 5.将项目提交到远程仓库 git->add->✔ 完后点击↗ 在码云如何获取token&#xff1f; 注&#xff1a;没有解决&#xff0c;有时间在继续研究