Spring - 基于注解的组件扫描
关于Spring的书籍都会花很大篇幅来讲解Spring如何注入各种bean的问题,非常令人头疼,自己在工作中还从来没有用到过。
所以就要跳过那些篇章,直接学习基于注解的组件扫描。

发现spring2是不支持组件自动扫描的,所以上一篇spring的代码要做改动
1重新从myeclipse导入spring3的library
2更改applicationContext.xml,支持spring3,且填入一句<context:component-scan base-package="org.example"/>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
"
default-lazy-init="true"> <context:component-scan base-package="org.example"/> </beans>
3加入新的测试方法:
/**
* @Title: testComponentScan
* @Description: 测试组件扫描机制
* @param 设定文件
* @return void 返回类型
* @throws
*/
@Test
public void testComponentScan(){
String conf = "applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
ExampleBean example = ac.getBean("exampleBean",ExampleBean.class);
example.Execute();
}
4输出结果

目录截图:

Spring - 基于注解的组件扫描的更多相关文章
- Unit02: 参数值注入 、 基于注解的组件扫描
Unit02: 参数值注入 . 基于注解的组件扫描 (4)IOC (Inversion Of Controll 控制反转) 什么是IOC? 对象之间的依赖关系由容器来建立. 什么是DI? (Depen ...
- Spring IOC基础回顾 — 组件扫描和装配
目录 注解形式配置应用IOC 1. 组件自动扫描 2. 组件依赖:为bean添加注解,实现自动注入 3. Spring IOC应用小结 注解形式配置应用IOC 在类定义.方法定义.成员变量定义前使用, ...
- Spring基于注解及SpringMVC
1.使用注解 (1)组件扫描 指定一个包路径,Spring会自动扫描该包 及其子包所有组件类,当发现组件类定义前有 特定的注解标记时,就将该组件纳入到Spring 容器.等价于原有XML配置中的< ...
- 阶段3 2.Spring_08.面向切面编程 AOP_9 spring基于注解的AOP配置
复制依赖和改jar包方式 src下的都复制过来. 复制到新项目里了 bean.xml里面复制上面一行代码到下面.把aop改成context. 配置spring容器创建时要扫描的包 Service的配置 ...
- Spring基于注解的Cache支持
Spring为我们提供了几个注解来支持Spring Cache.其核心主要是@Cacheable和@CacheEvict.使用@Cacheable标记的方法在执行后Spring Cache将缓存其返回 ...
- Spring 基于注解零配置开发
本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除. 原文网址:< Spring 基于注解零配置开发 > 一:搜索Bean 再也不用在XML文件里写什么配置信息了. Sprin ...
- Spring IoC 源码分析 (基于注解) 之 包扫描
在上篇文章Spring IoC 源码分析 (基于注解) 一我们分析到,我们通过AnnotationConfigApplicationContext类传入一个包路径启动Spring之后,会首先初始化包扫 ...
- Spring基于注解自动装配
前面我们介绍Spring IoC装载的时候,使用XML配置这种方法来装配Bean,这种方法可以很直观的看到每个Bean的依赖,但缺点也很明显:写起来非常繁琐,每增加一个组件,就必须把新的Bean配置到 ...
- (spring-第4回【IoC基础篇】)spring基于注解的配置
基于XML的bean属性配置:bean的定义信息与bean的实现类是分离的. 基于注解的配置:bean的定义信息是通过在bean实现类上标注注解实现. 也就是说,加了注解,相当于在XML中配置了,一样 ...
随机推荐
- JQuery中ajax请求写法
$.ajax({ type: "POST", url: "ygdwController.do?getonygdw", data : "id=" ...
- centos7.2下编译安装git
centos最新的7.2版本,git居然是1.8,而最新的git版本是2.9 差的太多了,何况git2.0后有大更新.于是,我决定编译安装.中间有一点小破折,记录一下,备忘. 1,下载最新的源码,网址 ...
- iOS button 里边的 字体的 摆放
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; button.titleEdgeInsets ...
- ZT 趋势移动安全apk
趋势移动安全 应用截图 应用简介 趋势移动安全( Mobile Security) 是一款专业的Android移动安全软件.利用趋势科技世界领先的云安全技术,保护用户避免被移动恶意程序骚扰,避免个 ...
- javascript 的 clientX用法
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- good excel website
http://www.codematic.net/excel-tools/free-excel-tools.htm
- openstack cinder-volume 的高可用(HA)
http://blog.csdn.net/LL_JCB/article/details/51879378 为了保证云平台的稳定性,需要做很多部分的高可用.比如控制节点高可用.计算节点高可用.网络节点高 ...
- [Shell]Bash变量:变量测试与内容替换
--------------------------------------------------------------------------------------------- 变量测试是通 ...
- HTTP请求响应对照表
1)请求(客户端->服务端[request]) GET(请求的方式) /books/java.html(请求的目标资源) HTTP/1.1(请求采用的协议和版本号) Accept: */*(客户 ...
- meta http-equiv='refresh' 解读
页面定期刷新,如果加url的,则会重新定向到指定的网页,content后面跟的是时间(单位秒), 把这句话加到指定网页的<head></head>里 一般也用在实时性很强的应用 ...