@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 ...
随机推荐
- 转--Oracle 审计和测试操作
http://blog.itpub.net/21605631/viewspace-759640/转 Oracle 审计和测试操作 :: 分类: Linux 1.1 相关参数 AUDIT_SYS_OPE ...
- input file上传文件扩展名限制
方法一(不推荐使用):用jS获获取扩展名进行验证: <script type="text/javascript" charset="utf-8"> ...
- The error occurred while setting parameters--索引 3 超出范围 sqlserver2008
这个问题不是jar包冲突,是表的问题,表里的ID设置成自动增加1,就可以了!
- 每日学习心得:SharePoint 为列表中的文件夹添加子项(文件夹)、新增指定内容类型的子项、查询列表中指定的文件夹下的内容
前言: 这里主要是针对列表中的文件下新增子项的操作,同时在新建子项时,可以为子项指定特定的内容类型,在某些时候需要查询指定的文件夹下的内容,针对这些场景都一一给力示例和说明,都是一些很小的知识点,希望 ...
- asp 实现域名转向
<% host=lcase(request.servervariables("HTTP_HOST")) select CASE host CASE "aa.cn&q ...
- 字符数组,字符指针,字符串常量,以及sizeof的一些总结
1.以字符串形式出现的,编译器都会为该字符串自动添加一个\0作为结尾 如在代码中写"abc",编译器帮你存储的是"abc\0". 2.数组的类型是由该数组所存放 ...
- 一个简单的CORBA例子
因为对CORBA分析的需要,这里写一个简单的CORBA例子.从JDK1.2开始,JDK中集成了ORB的实现,本例子使用了JDK1.7,对于JDK1.2+应该都没有问题.这个例子实现一个简单的加减乘除的 ...
- 一些实用的linux命令
一直在用linux,可linux下的命令还是用得不是很熟悉,记录一下比较有用命令: ``和$()是一样的,都是用指令的执行结果来替换. linux下 echo hello world => ...
- libpcap和WinPcap
能从物理上访问网络上的流量后,你需要用软件把它记录下来.这里,我们探究记录.解析和分析被捕获的数据包中最常用的软件库:libpcap和WinPcap.也将介绍包括tcpdump.Wireshark等基 ...
- ubuntu下安装php memcache扩展
memcached 安装sudo apt-get install memcached memcached 参数说明memcached -d -m 50 -p 11211 -u root-m 指定使用多 ...