一、@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的用法的更多相关文章

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

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

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

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

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

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

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

    1. package soundsystem; import org.springframework.context.annotation.Bean; import org.springframewo ...

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

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

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

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

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

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

  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-011-把敏感信息请求转为https(requiresChannel())

    1.把包含敏感信息的请求转为https请求,则较为安全,但如何只把有需要安全的请求转为https,而不是不加分辩就把所有请求都转为https呢?可以用requiresChannel() @Overri ...

随机推荐

  1. (转)OpenVPN使用HTTP代理连接服务器

    原文地址:http://www.365mini.com/page/18.htm 在一些公司或者其他受限的网络环境中,使用的是HTTP代理服务器上网.在这种情况下,使用OpenVPN客户端可能无法连接服 ...

  2. git研究2

    git也有发布版本时期的tag,不过这个tag,主要是作为一个标记而存在的,或者说在某个commit上面再打一个标记,表明版本是多少.这个和SVN上面的不太一样,SVN感觉有多份保存,似乎没有git方 ...

  3. git github 使用教程

    参考文章:文章地址: http://wuyuans.com/2012/05/github-simple-tutorial/ github是一个基于git的代码托管平台,付费用户可以建私人仓库,我们一般 ...

  4. 写漂亮C#代码的小技巧

    第一次写博客,不知道代码用什么编辑,直接截图了,哈哈哈.... 我自己不喜欢看随便复制粘贴过来一堆代码的博客,所以,用些简单点的例子吧,希望对大家有帮助...  ------------------- ...

  5. UVA 11300 Spreading the Wealth (数学推导 中位数)

    Spreading the Wealth Problem A Communist regime is trying to redistribute wealth in a village. They ...

  6. Linux Install Node.js

    1.下载node.js安装包,请参考网址:http://nodejs.org/download/ 在这个网址里面提供了几种node.js安装的方式 https://github.com/joyent/ ...

  7. java特殊运算符(转)

    原码:符号位用0表示正号,用1表示负号,数值一般用二进制形式表示 反码:机器数的反码可由原码得到.如果机器数是正数,则该机器数的反码与原码一样:如果机器数是负数,则该机器数的反码是对它的原码(符号位除 ...

  8. Linux下Docker安装

    1 在 CentOS 6.4 上安装 docker   docker当前官方只支持Ubuntu,所以在 CentOS 安装Docker比较麻烦(Issue #172).   docker官方文档说要求 ...

  9. ASP.NET 优化 check list

    看到一个蛮有意思的网站,里面有针对asp.net方方面面优化的罗列: 点击打开链接http://webdevchecklist.com/asp.net/performance/ 点击打开链接http: ...

  10. django 学习点滴

    django连接数据库要安装第三方包,比如mysql的就是 python-mysqldb, 用apt-cache search python-mysql 搜索一下. django的project可以放 ...