Spring @Bean @Scope @Qualifier
Bean
作用:用于把当前方法的返回值作为bean对象存入spring的ioc容器中
属性:
name:用于指定bean的id。当不写时,默认值是当前方法的名称
细节:
当我们使用注解配置方法时,如果方法有参数,spring框架会去容器中查找有没有可用的bean对象。
查找的方式和Autowired注解的作用是一样的
列子:
@Bean(name="runner")
@Scope("prototype")
public QueryRunner createQueryRunner(@Qualifier("ds2") DataSource dataSource){//Qualifier用于autowired多个相同类型时return new QueryRunner(dataSource);
}
等同于
<bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
<!--注入数据源-->
<constructor-arg name="ds" ref="dataSource"></constructor-arg>
</bean>
Spring @Bean @Scope @Qualifier相关推荐
- spring bean scope作用域及多线程安全问题场景分析
2019独角兽企业重金招聘Python工程师标准>>> Scope作用域 在 Spring IoC 容器中具有以下几种作用域: singleton:单例模式,在整个Spring Io ...
- Spring bean - scope详解
Scope是定义Spring如何创建bean的实例的. 在创建bean的时候可以带上scope属性,scope有下面几种类型. Singleton 这也是Spring默认的scope,表示Spri ...
- Spring Bean Scope 有状态的Bean 无状态的Bean
在Spring的Bean配置中,存在这样两种情况: [xhtml] view plaincopy <bean id="testManager" class="com ...
- 一分钟弄懂Spring @Bean @Autowired @Qualifier 的常见异常 以及命名规范
情况1:创建的bean类型和自动装配字段的类型不同-[此操作无效] @Autowired BCryptPasswordEncoder passwordEncoder;@Bean PasswordEnc ...
- spring bean属性scope
<bean id="role" class="spring.chapter2.maryGame.Role" scope="singleton ...
- Spring Bean 的scope什么时候设置为prototype,什么时候设置为singleton
Java代码 <bean id = "testAction" class = "com.kewen.xxxAction" scope = " ...
- Spring Bean 的 scope属性为何要设置为prototype
<bean id = "testAction" class = "com.kewen.xxxAction" scope = "prototype ...
- spring bean作用域_Spring面试知识点,这是我见过最全面的 - 知识铺
知识铺: 致力于打造轻知识点,持续更新每次的知识点较少,阅读不累.不占太多时间,不停地来唤醒记忆深处的知识点. Q1.什么是Spring框架? Spring是最流行的企业应用程序框架之一.Spring ...
- Spring(3)——装配 Spring Bean 详解
装配 Bean 的概述 前面已经介绍了 Spring IoC 的理念和设计,这一篇文章将介绍的是如何将自己开发的 Bean 装配到 Spring IoC 容器中. 大部分场景下,我们都会使用 Appl ...
最新文章
- 【Linux探索之旅】第二部分第五课:用户和权限,有权就任性
- leetcode—Valid Parentheses
- 二分于最大流之间的关系
- java五子棋用到的类_JAVA五子棋用到的知识点以及方法类有哪些?
- eclipse mysql5.7_MySQL5.7、Navicate、jdk、Tomcat、eclipse全套配置及安装(win10)-Go语言中文社区...
- python华为面试题10个人数3获取下标
- elasticsearch Java API 之Delete By Query API
- linux/bsd四大防火墙我们选择谁
- leetcode 144. Binary Tree Preorder Traversal ----- java
- git rebase基础
- CodeForces - 982C Cut 'em all!
- jpeg 转rgb c 语言_威刚推出SE770G移动固态硬盘 主打高速传输和RGB灯效
- 关于Meminfo中MemAvailable 理解
- Sql取出各科分数前三名的学生,Sql各科成绩前三的学生
- AI上推荐 之 隐语义模型(LFM)和矩阵分解(MF)
- 用各种编程语言写七夕情书
- erp系统的开发工具
- 深圳大学丽湖校区iPad连校园网WiFi
- 灰度图转换成彩色图和彩虹图
- Vue3 使用moment进行时间转换