SPRING IN ACTION 第4版笔记-第二章-002-@ComponentScan、@Autowired的用法
一、@ComponentScan
1.
@Configuration //说明此类是配置文件
@ComponentScan //开启扫描,会扫描当前类的包及其子包
public class CDPlayerConfig {
}
2.
@ComponentScan(basePackages={"soundsystem", "video"})//扫描多个包
public class CDPlayerConfig {
}
3.
@ComponentScan(basePackageClasses={CDPlayer.class,AAA.class})//指定要扫描的类
public class CDPlayerConfig {
}
二、@Autowired
1.可以在构造方法中用
@Component
public class CDPlayer implements MediaPlayer {
private CompactDisc cd; @Autowired
//@Inject
public CDPlayer(CompactDisc cd) {
this.cd = cd;
} public void play() {
cd.play();
} }
2.在set方法中
@Autowired
public void setCompactDisc(CompactDisc cd) {
this.cd = cd;
}
3.在一般的方法中
@Autowired
public void insertDisc(CompactDisc cd) {
this.cd = cd;
}
4.如果依赖不是必需的,可设置属性
@Autowired(required=false)
public CDPlayer(CompactDisc cd) {
this.cd = cd;
}
5.可用@Inject替代
package soundsystem;
import javax.inject.Inject;
import javax.inject.Named;
@Named
public class CDPlayer {
...
@Inject
public CDPlayer(CompactDisc cd) {
this.cd = cd;
}
...
}
SPRING IN ACTION 第4版笔记-第二章-002-@ComponentScan、@Autowired的用法的更多相关文章
- 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版笔记-第二章WIRING BEANS-006-当构造函数有集合时的注入
一.当构造函数有集合时,只能用<CONSTRUCTOR-ARG>,不能用C-NAMESPACE 二. 1. package soundsystem.collections; import ...
- SPRING IN ACTION 第4版笔记-第二章-004-Bean是否单例
spring的bean默认是单例,加载容器是会被化,spring会拦截其他再次请求bean的操作,返回spring已经创建好的bean. It appears that the CompactDisc ...
- SPRING IN ACTION 第4版笔记-第二章-003-以Java形式注入Bean、@Bean的用法
1. package soundsystem; import org.springframework.context.annotation.Bean; import org.springframewo ...
- 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版笔记-第二章-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-011-把敏感信息请求转为https(requiresChannel())
1.把包含敏感信息的请求转为https请求,则较为安全,但如何只把有需要安全的请求转为https,而不是不加分辩就把所有请求都转为https呢?可以用requiresChannel() @Overri ...
随机推荐
- Python之路【第二十二篇】:Django之Model操作
Django之Model操作 一.字段 AutoField(Field) - int自增列,必须填入参数 primary_key=True BigAutoField(AutoField) - bi ...
- win8 64位操作系统 Microsoft Visual Studio 2010在IIS上调试 “此任务要求应用程序具有提升的权限”等问题
很少在IIS上调试程序,因系统原因,所以不得不在IIS上预览项目和调试项目(因为只能在IIS上预览项目才能看到项目里的数据). 1.附加到进程(注意附加到进程前必须预览项目) 2.选择调试项 需要注意 ...
- 数据结构与算法JavaScript 读书笔记
由于自己在对数组操作这块比较薄弱,然后经高人指点,需要好好的攻读一下这本书籍,原本想这个书名就比较高深,这下不好玩了.不过看着看着突然觉得讲的东西都比较基础.不过很多东西,平时还是没有注意到,故写出读 ...
- jsp 页面获取xml的内容
<c:out value="${history.xml}" escapeXml="true" />
- react native android 开发,基础配置笔记。
一.React-native-device-info https://github.com/rebeccahughes/react-native-device-info 二.修改App名称 三.定位权 ...
- CustomEditor的文件要放在Assets/Editor目录下
using UnityEditor; using UnityEngine; [CustomEditor(typeof(test))] public class testEditor : Editor ...
- TFTPD32, 3CDaemon, FlashFxp
TFTPD32, 3CDaemon, FlashFxp ——各种网络传输下载工具简介—— 一.将3CDaemon.exe作为TFTP服务端,开发板作为TFTP客户端 1.如上图所示,设置好3CDaem ...
- <s:iterator></s:iterator>循环指定输出,(status的方法使用)
list集合中的实体的一个属性是另一个实体的集合(如下) public class PetInfo { private int petId; private String private Set< ...
- ASP.NET中的母版页
添加一个"母版页",使用<asp:ContentPlaceHolder>挖坑,新建的母版页已经自动设置了两个ContentPlaceHolder创建使用母版页的具体页面 ...
- Netty源码阅读(一) ServerBootstrap启动
Netty源码阅读(一) ServerBootstrap启动 转自我的Github Netty是由JBOSS提供的一个java开源框架.Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速 ...