1.

package soundsystem;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class CDPlayerConfig { @Bean
public CompactDisc compactDisc() {
return new SgtPeppers();
} @Bean
public CDPlayer cdPlayer(CompactDisc compactDisc) {
return new CDPlayer(compactDisc);
} }

The @Bean annotation tells Spring that this method will return an object that should
be registered as a bean in the Spring application context. The body of the method
contains logic that ultimately results in the creation of the bean instance.
By default, the bean will be given an ID that is the same as the @Bean -annotated
method’s name. In this case, the bean will be named compactDisc . If you’d rather it
have a different name, you can either rename the method or prescribe a different
name with the name attribute:

2.可以指定bean名称

@Bean(name="lonelyHeartsClubBand")
public CompactDisc sgtPeppers() {
return new SgtPeppers();
}

3.利用@Bean根据条件注入不同的依赖

@Bean
public CompactDisc randomBeatlesCD() {
int choice = (int) Math.floor(Math.random() * 4);
if (choice == 0) {
return new SgtPeppers();
} else if (choice == 1) {
return new WhiteAlbum();
} else if (choice == 2) {
return new HardDaysNight();
} else {
return new Revolver();
}
}

4.表面上似乎是调用同一个配置文件的函数

@Bean
public CDPlayer cdPlayer() {
return new CDPlayer(sgtPeppers());
}

这种配置表面上似乎是调用同一个配置文件的函数,但bean默认情况下是单例的,

It appears that the CompactDisc is provided by calling sgtPeppers , but that’s not
exactly true. Because the sgtPeppers() method is annotated with @Bean , Spring will
intercept any calls to it and ensure that the bean produced by that method is returned
rather than allowing it to be invoked again.

5.spring会自动找id为“compactDisc”的bean注入

@Bean
public CDPlayer cdPlayer(CompactDisc compactDisc) {
return new CDPlayer(compactDisc);
}

6.不但可以通过构造函数,也可通过set访求注入

@Bean
public CDPlayer cdPlayer(CompactDisc compactDisc) {
CDPlayer cdPlayer = new CDPlayer(compactDisc);//这里的构造函数是不是不用传参数????
cdPlayer.setCompactDisc(compactDisc);
return cdPlayer;
}

SPRING IN ACTION 第4版笔记-第二章-003-以Java形式注入Bean、@Bean的用法的更多相关文章

  1. SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-006-当构造函数有集合时的注入

    一.当构造函数有集合时,只能用<CONSTRUCTOR-ARG>,不能用C-NAMESPACE 二. 1. package soundsystem.collections; import ...

  2. SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-008-在Java配置文件中引入xml配置文件@Import、@ImportResource

    1. package soundsystem; import org.springframework.beans.factory.annotation.Autowired; public class ...

  3. SPRING IN ACTION 第4版笔记-第二章-004-Bean是否单例

    spring的bean默认是单例,加载容器是会被化,spring会拦截其他再次请求bean的操作,返回spring已经创建好的bean. It appears that the CompactDisc ...

  4. SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-008-在XML配置文件中引入JAVA配置文件 <import> 、<bean>

    一.在xml中引入xml,用<import> <?xml version="1.0" encoding="UTF-8"?> <be ...

  5. SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-007-以set方法注入<property>\p-namespace\util-space

    一.注入简单属性 package soundsystem.properties; import org.springframework.beans.factory.annotation.Autowir ...

  6. SPRING IN ACTION 第4版笔记-第二章Wiring Beans-005-<constructor-arg>和c-namespace

    1. package soundsystem; public class SgtPeppers implements CompactDisc { private String title = &quo ...

  7. SPRING IN ACTION 第4版笔记-第二章-002-@ComponentScan、@Autowired的用法

    一.@ComponentScan 1. @Configuration //说明此类是配置文件 @ComponentScan //开启扫描,会扫描当前类的包及其子包 public class CDPla ...

  8. SPRING IN ACTION 第4版笔记-第二章-001-用@Autowired\@ComponentScan、@Configuration、@Component实现自动装载bean

    1. package soundsystem; import org.springframework.context.annotation.ComponentScan; import org.spri ...

  9. SPRING IN ACTION 第4版笔记-第九章Securing web applications-001-SpringSecurity简介(DelegatingFilterProxy、AbstractSecurityWebApplicationInitializer、WebSecurityConfigurerAdapter、@EnableWebSecurity、@EnableWebMvcS)

    一.SpringSecurity的模块 At the least, you’ll want to include the Core and Configuration modules in your ...

随机推荐

  1. modelsim脚本文件的编写

    第一章 ModelSim介 绍 本指南是为 ModelSim5.5f版本编写的,该版本运行于UNIX和Microsoft Windows 95/98/Me/NT/2000的操作系统环境中.本指南覆盖了 ...

  2. VS中使用sqlite静态连接

    说明 最近写的文章有些多,懒得打字了,就直接上文章.这里说明一下,我说的是VS,不是指定的哪一个VS版本.先下载官方的源文件:sqlite-amalgamation-3071502.zip 下载下来的 ...

  3. <s:iterator></s:iterator>循环指定输出,(status的方法使用)

    list集合中的实体的一个属性是另一个实体的集合(如下) public class PetInfo { private int petId; private String private Set< ...

  4. 管理员把我的admin权限去掉了,那么如何获得jdk zip安装呢?这篇可以帮你。

    JDK is not available as a portable zip unfortunately. However, you can: Create working JDK directory ...

  5. ECMA5.1中关于encodeURI,decodeURI 和encodeComponentURI,decodeComponentURI的区别

    The encodeURI and decodeURI functions are intended to work with complete URIs; theyassume that any r ...

  6. #添加屏蔽IP LINUX

    netfilter/iptables 的最大优点是它可以配置有状态的防火墙,这是 ipfwadm 和 ipchains 等以前的工具都无法提供的一种重要功能.有状态的防火墙能够指定并记住为发送或接收信 ...

  7. 京东电话面试——PHP开发

    1.学过<数据结构>吗?你学过的计算机相关课程有哪些? 2.web操作中,当你输入一个url到看到页面,描述一下这中间发生了什么? 3.TCP/IP的请求方式都有哪些? 4.POST和GE ...

  8. 安装mod_deflate模块启用apache的GZIP压缩

    安装mod_deflate模块启用apache的GZIP压缩 操作系统:Linux Cent OS 5 / Max OS X 10.6 snow leopard相关环境:Apache httpd 2. ...

  9. alibaba的COBAR真是强大.

    近好不容易抽空研究了下Cobar,感觉这个产品确实很不错(在文档方面比Amoeba强多了),特此推荐给大家.Cobar是阿里巴巴研发的关系型数据 的分布式处理系统,该产品成功替代了原先基于Oracle ...

  10. zzuli oj 1167逆转数(指针专题)

    Description 任意给你一个整数,这个数可能很大(最长不超过100位),你能求出它的逆转数吗?  逆转数定义如下:  1.一个末尾没有0的整数,它的逆转数就是各位数字逆序输出:  2.一个负数 ...