SPRING IN ACTION 第4版笔记-第二章-003-以Java形式注入Bean、@Bean的用法
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的用法的更多相关文章
- SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-006-当构造函数有集合时的注入
一.当构造函数有集合时,只能用<CONSTRUCTOR-ARG>,不能用C-NAMESPACE 二. 1. package soundsystem.collections; import ...
- SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-008-在Java配置文件中引入xml配置文件@Import、@ImportResource
1. package soundsystem; import org.springframework.beans.factory.annotation.Autowired; public class ...
- SPRING IN ACTION 第4版笔记-第二章-004-Bean是否单例
spring的bean默认是单例,加载容器是会被化,spring会拦截其他再次请求bean的操作,返回spring已经创建好的bean. It appears that the CompactDisc ...
- SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-008-在XML配置文件中引入JAVA配置文件 <import> 、<bean>
一.在xml中引入xml,用<import> <?xml version="1.0" encoding="UTF-8"?> <be ...
- SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-007-以set方法注入<property>\p-namespace\util-space
一.注入简单属性 package soundsystem.properties; import org.springframework.beans.factory.annotation.Autowir ...
- SPRING IN ACTION 第4版笔记-第二章Wiring Beans-005-<constructor-arg>和c-namespace
1. package soundsystem; public class SgtPeppers implements CompactDisc { private String title = &quo ...
- SPRING IN ACTION 第4版笔记-第二章-002-@ComponentScan、@Autowired的用法
一.@ComponentScan 1. @Configuration //说明此类是配置文件 @ComponentScan //开启扫描,会扫描当前类的包及其子包 public class CDPla ...
- SPRING IN ACTION 第4版笔记-第二章-001-用@Autowired\@ComponentScan、@Configuration、@Component实现自动装载bean
1. package soundsystem; import org.springframework.context.annotation.ComponentScan; import org.spri ...
- 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 ...
随机推荐
- FxCop
下载地址:http://www.microsoft.com/en-us/download/details.aspx?id=8279 FxCop是一个代码分析工具,和StyleCop(代码规范)一起配合 ...
- spring mvc 3.1的自动注入参数遇到的问题
在网上下载了xheditor作为页面的编辑器,编辑内容后post到后台保存,后台方法用spring mvc的自动注入的方式接收参数. 这种方式在各个浏览器下运行良好,但是在ie11下发现,从word. ...
- Linux云主机安装JDK,配置hadoop的详细方式
云主机我使用的是青云的,还有好多其他品牌,比如阿里云 unitedstack 等等. 注册完青云后,会有试用券发到账户,可以利用此券试用其服务. 1 首先创建好一个主机,按照提示选择好系统,创建好一个 ...
- C#中params使用
1.参数被params修饰即为可变参数,params只能修饰一维数组. 2.给可变参数赋值的时候,可以直接传递数组的元素. 3.在调用的时候,会自动将这些元素封装为一个数组,并将数组传递. 4.可变参 ...
- MVC小系列(十八)【给checkbox和radiobutton添加集合的重载】
mvc对DropDownListFor的重载很多,但对checkbox和radiobutton没有对集合的重载 所以该讲主要针对集合的扩展: #region 复选框扩展 /// <summary ...
- Jquery为下拉列表动态赋值与取值,取索引
接触前端也不久对jquery用的也只是皮毛,写过去感觉能复用的发出来,大家指点下 1.下拉列表动态赋值 function initddlYear() { var mydate = new Date() ...
- ubuntu安装mariadb
参考网址:https://downloads.mariadb.org/mariadb/repositories/ 以ubuntu12.04安装mariadb10为例.具体其他的可以参考给出的参考网址H ...
- 利用ORACLE ADV 功能完成SQL TUNING 调优(顾问培训) “让DBA失业还是解脱?”
oracle自动判断SQL性能功能. 11G的ADV,建议.SNAPSHOT,数据集合, 存储在oracle sys $_开头的表(10几条). 创建SNAPSHOT时选择天数, 默认14天. sq ...
- JAVA IDE下载地址
Subclipse 各版本官网下载地址: http://subclipse.tigris.org/servlets/ProjectDocumentList?folderID=2240 Eclipse ...
- 查内网虚拟机映射的公网IP
1.访问ip138.com 2.curl ifconfig.me