@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 ...
随机推荐
- C++资料收集&整理
类别 地址 ZLib c++ zlib(qt)压缩与解压缩 STL STL学习之map容器(2)_insert Basic std::nothrow new (nothrow) T() 的原理 C++ ...
- 移动POS机
1.怎么识别手刷机所属公司是否是二清公司,甚至是多清 去银行,手机银行,网上银行查询该笔款是哪里汇出的,如果是银行或合法支付公司汇出,一般为一清机,如果是个人或无支付牌照的支付公司,一般为二清机: 已 ...
- Servlet与JSP的区别
一.基本概念 1.1 Servlet Servlet是一种服务器端的Java应用程序,具有独立于平台和协议的特性,可以生成动态的Web页面.它担当客户请求(Web浏览器或其他HTTP客户程序)与服务器 ...
- 使用它tshark分析pcap的例子以及scapy下载地址
转一篇cisco工作人员使用tshark分析pcap的文章,以及scapy的下载地址 http://blogs.cisco.com/security/finding-a-needle-in-a-pca ...
- KVO/KVC 实现机理分析
来源:http://blog.csdn.net/dqjyong/article/details/7672865 Objective-C里面的Key-Value Observing (KVO)机制,非常 ...
- js 生成 yyyy-mm-dd 格式的逼格姿势
关于 js 生成 yyyy-mm-dd 格式,往往都会采取手动拼接,一般不愿意为了小功能而去动用 momentjs 之类的插件. ps: 只分享简单方法,网上有 N 多 dateformat 代码,这 ...
- 在此页上的ActiveX控件和本页上的其他部分的交互可能不安全,你想允许这种交互吗
转自 http://www.cnblogs.com/zdxster/archive/2011/01/27/1945868.html 在EOS6的项目中,如果采用VC++开发的ActiveX,那么第一次 ...
- Oracle死锁产生的原因和解决办法
如果有两个会话,每个会话都持有另一个会话想要的资源,此时就会发生死锁.用下面实验来说明死锁的产生原因和解决办法.SESSION1:SQL> create table t2 as select * ...
- 剑指offer习题集2
1.把数组排成最小的数 class Solution { public: static bool compare(const string& s1, const string& s2) ...
- 创建eclipse针对NDK的联合编译环境。
警告, 这篇文章是老的配置方式, 随着goolge工具的完善,有了更高级的配置方式,参考文章: 1.http://jingyan.baidu.com/article/3ea51489e7a9bd52e ...