SpringBoot配置两个一样的Bean,区分两个配置类——@Primary
1、@Primary
作用:
- 指定默认bean。
- 当没有根据名字显示要注入哪个bean的时候,默认使用打了@Primary标签的bean
2、配置两个一样的bean
@Configuration
public class MyThymeLeafConfig {
@Resource
private ApplicationContext applicationContext;
/** 自定义的bean(默认)
* @return SpringTemplateEngine
* @Primary :<li>作用:指定使用名为“myTemplateEngine”的bean作为默认bean。</li>
* <li>这样,当您在需要使用SpringTemplateEngine的地方没有指定@Qualifier注释时,Spring将使用该默认bean。</li>
* <li>使用@Resource时,可直接设置名字。不用使用@Qualifier注释</li>
*/
@Bean(name = "myTemplateEngine")
@Primary
public SpringTemplateEngine myTemplateEngine(){
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setEnableSpringELCompiler(true);
return templateEngine;
}
//----------------------------------------------
/** 自定义的bean2
* @return SpringTemplateEngine
*/
@Bean(name = "myTemplateEngine2")
public SpringTemplateEngine myTemplateEngine2(){
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setEnableSpringELCompiler(true);
return templateEngine;
}
}
3、使用
- 可以使用 @Resource 也可以使用 @Autowired + @Qualifier
- 推荐使用 @Resource
- @Resource和@Autowired + @Qualifier区别见:https://www.cnblogs.com/kakarotto-chen/p/17408564.html
- 建议注入的时候都加上bean的名字,这样速度快
@Resource(name = "myTemplateEngine")
private SpringTemplateEngine springTemplateEngine1Html;
@Resource(name = "myTemplateEngine2")
private SpringTemplateEngine springTemplateEngine2Xml;
SpringBoot配置两个一样的Bean,区分两个配置类——@Primary的更多相关文章
- 【玩转SpringBoot】用好条件相关注解,开启自动配置之门
自动配置隐含两层含义,要搞清楚 上帝让程序员的发量减少,是为了让他变得更聪明,如果有一天聪明到了极点,那就是绝顶聪明. 据说在大脑高速运转下,这样更有利于散热,不至于核心温度过高而产生告警. 聪明的大 ...
- springboot开发人员工具(自动重启及相关的配置)
导入依赖: <dependencies> <dependency> <groupId>org.springframework.boot</groupId> ...
- @Configuration结合@Bean实现对象的配置
@Configuration结合@Bean实现对象的配置 前提:最近项目中需要做支付接口,支付宝以及微信支付,本文并不介绍如何写支付接口,而是通过这个示例讲解配置应该怎么写,项目中使用的是Kotlin ...
- springboot在eclipse中运行使用开发配置,打包后运行使用生产环境默认配置
java命令运行springboot jar文件,指定配置文件可使用如下两个参数中其中一个 --spring.config.location=配置文件路径 -Dspring.profiles.acti ...
- 7 -- Spring的基本用法 -- 5... Spring容器中的Bean;容器中Bean的作用域;配置依赖;
7.5 Spring容器中的Bean 7.5.1 Bean的基本定义和Bean别名 <beans.../>元素是Spring配置文件的根元素,该元素可以指定如下属性: default-la ...
- spring 注入bean的两种方式
我们都知道,使用spring框架时,不用再使用new来实例化对象了,直接可以通过spring容器来注入即可. 而注入bean有两种方式: 一种是通过XML来配置的,分别有属性注入.构造函数注入和工厂方 ...
- Spring学习(六)bean装配详解之 【通过注解装配 Bean】【基础配置方式】
通过注解装配 Bean 1.前言 优势 1.可以减少 XML 的配置,当配置项多的时候,XML配置过多会导致项目臃肿难以维护 2.功能更加强大,既能实现 XML 的功能,也提供了自动装配的功能,采用了 ...
- 【Spring】Spring中的Bean - 1、Baen配置
Bean配置 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean 什么是Spring中的Bean? Spring可以被看作是一个 ...
- spring_07使用spring的特殊bean、完成分散配置
一. 前言 分散配置思路:创建properties文件,添加数据,在beans文件中先配置properties文件,再在bean中使用占位符引用数据 对于bean的生命周期中的很多处理接口,处 ...
- SpringBoot拦截器中无法注入bean的解决方法
SpringBoot拦截器中无法注入bean的解决方法 在使用springboot的拦截器时,有时候希望在拦截器中注入bean方便使用 但是如果直接注入会发现无法注入而报空指针异常 解决方法: 在注册 ...
随机推荐
- MySQL备份还原工具
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- KingbaseES特殊权限介绍
用户需求:新建一个用户B,需要能够查询A用户的所有表,并且对以后新建的表也要有select权限. 对于现有的表可以通过动态sql批量进行授权,但是未来新建的表要如何进行授权呢? 查询了帮助文档发现通过 ...
- KingbaseES Json 系列三:Json数据操作函数一
KingbaseES Json 系列三--Json数据操作函数一(JSONB_EACH,JSONB_EACH_TEXT,JSONB_OBJECT_KEYS,JSONB_EXTRACT_PATH,JSO ...
- 企业数据清洗项目实践day1
今天先把国标excel表的数据在Python里转化成了字典类型, 暂时定共分为四层,层层分类. 代码 1 def std_excel(): 2 dict={"A":{"0 ...
- Python爬虫爬取ECVA论文标题作者摘要关键字等信息并存储到mysql数据库
网站截图: 源代码: 1 import re 2 import requests 3 import pymysql 4 from bs4 import BeautifulSoup 5 import l ...
- .NET Core 多线程的用法,以及用例
1.使用 Thread 类 Thread 类是 .NET 中最基本的多线程操作方式之一,可以使用它创建并启动新线程.以下是一个简单的例子,创建一个新的线程并运行: using System; usin ...
- Java也可以像python般range出连续集合
Java lamada:IntStream --range(int startInclusive, int endExclusive):前包后不包 IntStream.range(0, 10).for ...
- 【开源三方库】Aki:一行代码极简体验JS&C++跨语言交互
开源项目 OpenHarmony 是每个人的 OpenHarmony 一.简介 OpenAtom OpenHarmony(以下简称"OpenHarmony")的前端开发语言是A ...
- 在Centos 8 服务器用tmux多开窗口
在 CentOS 服务器上使用 tmux 来多开窗口是一个高效的方式.tmux 是一个终端复用器,它允许你在一个终端窗口中打开多个终端会话,还可以在会话之间轻松切换,非常适合长时间运行程序或多任务操作 ...
- OpenCV一句话将彩色图片(Mat)转换为灰度
auto in_gray=cv::imdecode(in, cv::IMREAD_GRAYSCALE);