@Configuration 和 @Bean
1. @Bean:
1.1 定义
从定义可以看出,@Bean只能用于注解方法和注解的定义。
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
1.2 spring文档中对 @Bean的说明
The @Bean annotation is used to indicate that a method instantiates, configures and initializes a new object to be managed by the Spring IoC container.
For those familiar with Spring’s <beans/> XML configuration the @Bean annotation plays the same role as the <bean/> element.
用@Bean注解的方法:会实例化、配置并初始化一个新的对象,这个对象会由spring IoC 容器管理。
实例:
@Configuration
public class AppConfig { @Bean
public MyService myService() {
return new MyServiceImpl();
} }
相当于在 XML 文件中配置
<beans>
<bean id="myService" class="com.acme.services.MyServiceImpl"/>
</beans>
1.3 生成对象的名字:默认情况下用@Bean注解的方法名作为对象的名字。但是可以用 name属性定义对象的名字,而且还可以使用name为对象起多个名字。
@Configuration
public class AppConfig { @Bean(name = "myFoo")
public Foo foo() {
return new Foo();
} }
@Configuration
public class AppConfig { @Bean(name = { "dataSource", "subsystemA-dataSource", "subsystemB-dataSource" })
public DataSource dataSource() {
// instantiate, configure and return DataSource bean...
} }
1.4 @Bean 一般和 @Component或者@Configuration 一起使用。
@Component和@Configuration不同之处
(1)This method of declaring inter-bean dependencies only works when the @Bean method is declared within a @Configuration class. You cannot declare inter-bean dependencies using plain @Component classes.
在 @Component 注解的类中不能定义 类内依赖的@Bean注解的方法。@Configuration可以。
@Configuration
public class AppConfig { @Bean
public Foo foo() {
return new Foo(bar());
} @Bean
public Bar bar() {
return new Bar();
} }
2. @Configuration:
2.1 定义
从定义看,用于注解类、接口、枚举、注解的定义。
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
2.2 spring 文档说明
@Configuration is a class-level annotation indicating that an object is a source of bean definitions. @Configuration classes declare beans via public @Bean annotated methods.
@Configuration用于类,表明这个类是beans定义的源。
@Configuration 和 @Bean的更多相关文章
- Springboot@Configuration和@Bean详解
Springboot@Configuration和@Bean详解 一.@Configuration @Target({ElementType.TYPE}) @Retention(RetentionPo ...
- Spring的Java配置方式—@Configuration和@Bean实现Java配置
Java配置是Spring4.x推荐的配置方式,可以完全替代xml配置. 1.@Configuration 和 @BeanSpring的Java配置方式是通过 @Configuration 和 @Be ...
- @Configuration与@Bean作用
Spring的Java配置方式是通过@Configuration和@Bean这两个注解来实现 @Configuration可以作用在任意类上,表示该类是一个配置类,其实就相当于一个xml配置文件. @ ...
- Spring @Configuration 和 @Bean 注解
@Configuration 和 @Bean 注解 带有 @Configuration 的注解类表示这个类可以使用 Spring IoC 容器作为 bean 定义的来源.@Bean 注解告诉 Spri ...
- @Configuration和@Bean的用法和理解
spring Boot提倡约定优于配置,如何将类的生命周期交给spring 1.第一种自己写的类,Controller,Service. 用@controller @service即可 2.第二种,集 ...
- @Configuration和@Bean
@Configuration可理解为用spring的时候xml里面的标签 @Bean可理解为用spring的时候xml里面的标签 Spring Boot不是spring的加强版,所以@Configur ...
- 0006SpringBoot中@Configuration与@Bean联合使用
需求:将某个普通类做为组件注册到容器中,可通过如下办法 1.定义HelloService类 package springboot_test.springboot_test.service; publi ...
- @Configuration与@Bean
1,@Configuration与@Bean @Configuration: 告诉Spring这是一个配置类,配置类==配置文件. @Configuration==beans.xml @Bean: ...
- @Configuration结合@Bean实现对象的配置
@Configuration结合@Bean实现对象的配置 前提:最近项目中需要做支付接口,支付宝以及微信支付,本文并不介绍如何写支付接口,而是通过这个示例讲解配置应该怎么写,项目中使用的是Kotlin ...
随机推荐
- 浅谈new operator、operator new和placement new 分类: C/C++ 2015-05-05 00:19 41人阅读 评论(0) 收藏
浅谈new operator.operator new和placement new C++中使用new来产生一个存在于heap(堆)上对象时,实际上是调用了operator new函数和placeme ...
- morhia解决BigDecimal映射问题
错误信息:morphia No usable constructor for java.math.BigDecimal 解决办法:增加自定义类型转换器,代码如下 package cn.huilink. ...
- MongoDB快速上手
1. MongoDB简介 MongoDB是一个跨平台的基于Key_Value键值对形式保存数据的NoSQL文档类型数据库. NoSQL(not only sql)数据库,泛指非关系型数据库. 1.1 ...
- 只适用于HTML的DOM对象
前面,已经学习了如何对树结构进行遍历,并添加.删除.修改任何XML文档中的节点.但是,还有一些对象和属性石只适用于HTML的.列如,document.body就是一个纯HTML对象.只要HTML文档中 ...
- java maven诡异的错误no class found
从服务器下载一个java web项目,启动老提示no class found,查看maven依赖库,相关的jar包都已经引入.同样一个项目,在别的机器都可以运行,唯独在我本机运行出错. 为了排错,将其 ...
- 数据结构&算法-双向链表
1.引言 双向链表原理和单链表差不多,就是操作相对繁琐一些,简单画了几个图,把思想弄清楚就好. 2.正文 这里介绍一下插入和删除的原理,相信这两个操作搞清楚的话,其他操作也没什么问题. 2.1插入节点 ...
- Mysql备份迁移——Mysqldump(.NET调用Mysqldump.exe方式)——(解决视图嵌视图报错)
利用Mysqldump备份和迁移,我想很多人都用过,具体参数不介绍了,这里主要讲.NET调用Mysqldump进行备份和.NET调用Mysql.exe进行导入数据. 这里使用的是5.1版的Mysqld ...
- AX Dynamic 2012 SSRS 按行数分页
按行数分页 1. Create a new Row Group with the following grouping expression: =Ceiling(RowNumber(Nothing)/ ...
- shutdown immediate时 hang住 (转载)
shutdown immediate 经常关库时hang住,在alert中有 License high water mark = 4All dispatchers and shared servers ...
- IE请求访问的设置
1:问题 目前有个项目采用AngularJs发送Restful风格的请求的方式来实现前后端的通信,测试人员在使用IE浏览器进行测试的时候发现,有时候请求发送不到后台,后来经查找发现是由于IE浏览器的默 ...