一、@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. 获取select下拉列表选中的值

    html: <select id="resultList"> <option >1班</option> <option >2班< ...

  2. SQL的update from 理解

    学习了sql的语句都有快3年,工作上使用都一年半的,最近突然想起update from语句,感觉好像很模糊,虽然语法上使用一直正确,一直都是这样使用,但是就好像不是很明白里面的深处意思. 今天特意测试 ...

  3. csqlite编译相关配置问题

    csqlite是非常好用的数据库,同时该数据库是开源的,基于一定原因可能需要编译自己需要的csqlite版本,那么下面介绍内容也会你就会感兴趣了. 这里要实现的目标是使用VS工具能够正确编译csqli ...

  4. Sdut 2409 The Best Seat in ACM Contest(山东省第三届ACM省赛 H 题)(模拟)

    题目描述 Cainiao is a university student who loves ACM contest very much. It is a festival for him once ...

  5. 九度OJ 1066 字符串排序

    题目地址:http://ac.jobdu.com/problem.php?pid=1066 题目描述: 输入一个长度不超过20的字符串,对所输入的字符串,按照ASCII码的大小从小到大进行排序,请输出 ...

  6. 九度OJ 1107 搬水果 -- 哈夫曼树 2011年吉林大学计算机研究生机试真题

    题目地址:http://ac.jobdu.com/problem.php?pid=1107 题目描述: 在一个果园里,小明已经将所有的水果打了下来,并按水果的不同种类分成了若干堆,小明决定把所有的水果 ...

  7. 第一个wxWidgets程序

    wxWidgets的安装方法网上有一大堆,可以参照http://wiki.codeblocks.org/index.php?title=WxWindowsQuickRef 这里解压并编译 ,也可以参照 ...

  8. python的一个表达式的计算(超简单)

    运行的过程如下: 输入计算表达式:3+5 计算结果:8 然后再次显示计算表达式,等待输入完成后,再次显示结果,依此循环.   作为初学者再适合不过,代码也简单,如下所示: #!/usr/bin/env ...

  9. 浅谈.prop() 和 attr() 的区别

    今天编码时遇到一个问题,通过后台查询的数据设置前端checkbox的选中状态,设置选中状态为.attr('checked','true');没有问题,但是当数据重新加载时,checkbox应清空即所有 ...

  10. ubuntu 下安装软件,卸载,查看已经安装的软件

    参考网址:http://wiki.ubuntu.org.cn/UbuntuSkills 一般的安装程序用三种: .deb 和.rpm 这两种安装文件 .bundle 这是二进制的安装文件 而 tar. ...