关于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 - 基于注解的组件扫描的更多相关文章

  1. Unit02: 参数值注入 、 基于注解的组件扫描

    Unit02: 参数值注入 . 基于注解的组件扫描 (4)IOC (Inversion Of Controll 控制反转) 什么是IOC? 对象之间的依赖关系由容器来建立. 什么是DI? (Depen ...

  2. Spring IOC基础回顾 — 组件扫描和装配

    目录 注解形式配置应用IOC 1. 组件自动扫描 2. 组件依赖:为bean添加注解,实现自动注入 3. Spring IOC应用小结 注解形式配置应用IOC 在类定义.方法定义.成员变量定义前使用, ...

  3. Spring基于注解及SpringMVC

    1.使用注解 (1)组件扫描 指定一个包路径,Spring会自动扫描该包 及其子包所有组件类,当发现组件类定义前有 特定的注解标记时,就将该组件纳入到Spring 容器.等价于原有XML配置中的< ...

  4. 阶段3 2.Spring_08.面向切面编程 AOP_9 spring基于注解的AOP配置

    复制依赖和改jar包方式 src下的都复制过来. 复制到新项目里了 bean.xml里面复制上面一行代码到下面.把aop改成context. 配置spring容器创建时要扫描的包 Service的配置 ...

  5. Spring基于注解的Cache支持

    Spring为我们提供了几个注解来支持Spring Cache.其核心主要是@Cacheable和@CacheEvict.使用@Cacheable标记的方法在执行后Spring Cache将缓存其返回 ...

  6. Spring 基于注解零配置开发

    本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除. 原文网址:< Spring 基于注解零配置开发 > 一:搜索Bean 再也不用在XML文件里写什么配置信息了. Sprin ...

  7. Spring IoC 源码分析 (基于注解) 之 包扫描

    在上篇文章Spring IoC 源码分析 (基于注解) 一我们分析到,我们通过AnnotationConfigApplicationContext类传入一个包路径启动Spring之后,会首先初始化包扫 ...

  8. Spring基于注解自动装配

    前面我们介绍Spring IoC装载的时候,使用XML配置这种方法来装配Bean,这种方法可以很直观的看到每个Bean的依赖,但缺点也很明显:写起来非常繁琐,每增加一个组件,就必须把新的Bean配置到 ...

  9. (spring-第4回【IoC基础篇】)spring基于注解的配置

    基于XML的bean属性配置:bean的定义信息与bean的实现类是分离的. 基于注解的配置:bean的定义信息是通过在bean实现类上标注注解实现. 也就是说,加了注解,相当于在XML中配置了,一样 ...

随机推荐

  1. .net 更新数据 ado.net parameter

    UPDATE yborder_ordernotes SET recoder400= @FileAddress, havefile400 = 1 WHERE id = @OrderID Maticsof ...

  2. 转:pack URI in WPF

    一开始看到WPF里面经常用如下语句来构造资源文件Uri: Uri uri = new Uri("/AssemblyName;component/image.png"); 我还以为这 ...

  3. nginx解析php请求为404

    刚搭建了lnmp环境,发现如果讲我的程序放在某个文件夹的时候,然后nginx进行代理的时候竟然会是404: nginx配置如下: # pass the PHP scripts to FastCGI s ...

  4. Confluence部署攻略 [转]

    一.软件介绍 AtlassianConfluence(简称Confluence)是一个专业的wiki程序.它是一个知识管理的工具,通过它可以实现团队成员之间的协作和知识共享.Confluence不是一 ...

  5. Centos7 Apache 2.4.18编译安装

    安装环境:CentOS Linux release 7.0.1406 (Core) 0x01 到官网http://httpd.apache.org/download.cgi#apache24下载apa ...

  6. Windows 设置扩展投影鼠标移出方向

    1. 连接数据线,按下 “WINDOWS” + P 按钮,选择“扩展投影”: 2.更改鼠标移出屏幕的方向:桌面右键选择“屏幕分辨率” , 移动“更改显示器外观”中两个图的相对方向即可:

  7. eclipse里面设置JVM参数的问题

    在run----run configuration---Agruments里面设置JVM的参数:  -Xms256m   -Xmx1024m 肯定还有别的方式设置,今天就先写这一种方法.待续...

  8. FreeBSD_11-系统管理——{Part_3-网络}

    一.Network Servers DNS unbound/local_unbound # /etc/rc.conf local_unbound_enable="YES" # 测试 ...

  9. 斯坦福第十二课:支持向量机(Support Vector Machines)

    12.1  优化目标 12.2  大边界的直观理解 12.3  数学背后的大边界分类(可选) 12.4  核函数 1 12.5  核函数 2 12.6  使用支持向量机 12.1  优化目标 到目前为 ...

  10. Remoting首次用时偏长问题

    先说我遇到的问题,我需要访问多个服务器的一个相同的Remoting方法,根据方法返回的结果决定优先使用某个服务器. var _remoteFacade = Activator.GetObject(ty ...