1.导入jar包

2.创建对应的类

public interface HelloService {

    public void sayHello();
}
/**
 * @Component(value="helloService") 相当于 <bean id="helloService" class="com.spring.demo1.HelloSeviceImpl"/>
 * @author NEWHOM
 *
 */
@Component(value="helloService")
public class HelloSeviceImpl implements HelloService {

    @Override
    public void sayHello() {
        // TODO Auto-generated method stub
        System.out.println("Hello Spring !!");
    }

}

3.在applicationContext.xml中引入约束

        <beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:context="http://www.springframework.org/schema/context"        xsi:schemaLocation="
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

        </beans>

4.在applicationContext.xml中开启组件扫描

<context:component-scan base-package="com.spring.demo1" />

5.在HelloServiceImpl上添加注解

@Component(value="helloService") 相当于 <bean id="helloService" class="com.spring.demo1.HelloSeviceImpl"/>

6.编写测试类

public class Demo1 {

    /**
     * 测试注解方式的IOC
     */
    @Test
    public void m01(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        HelloService helloService = (HelloService) ac.getBean("helloService");

        helloService.sayHello();
    }

}

注意:

Spring中提供@Component的三个衍生注解:(功能目前来讲是一致的)
    * @Controller       -- 作用在WEB层
    * @Service          -- 作用在业务层
    * @Repository       -- 作用在持久层

最新文章

  1. 局域网内利用gitlab,jenkins自动生成gitbook并发布(nginx)
  2. mybatis+MySQL--CRUD
  3. iOS 模拟器上录制Demo视频
  4. #笔记#JavaScript进阶篇二
  5. QLabel添加图片链接
  6. ecshop常用语句
  7. 【iOS基础】NSURLConnection
  8. IE9 浏览器无法捕获中键事件
  9. SeaJS之shim插件:解决非cmd规范的插件与sea的区别
  10. 将Maven的Web项目部署到windows的Tomcat里
  11. Redis进阶实践之八Lua的Cjson在Linux下安装、使用和用C#调用Lua脚本
  12. Linux高级运维 第五章 Vim编辑器和恢复ext4下误删除的文件-Xmanager工具
  13. 漫谈hashcode
  14. 系统运维|SqlServer2008|数据库日志文件过大需要清理的操作攻略
  15. AC自动机-HDU2222-模板题
  16. 2017-2018-2 20165225『Java程序设计』课程 结对编程练习_四则运算
  17. JoyOI1940 创世纪
  18. information_schema系列十一
  19. 判断一个对象是否为真 __nonzero__ 方法和 __len__方法
  20. Spring boot --- Spring Oauth(三)

热门文章

  1. 关于jquery ajax项目总结
  2. phonegap 开发指南系列----开始之前(1)
  3. Python代码中func(*args, **kwargs)
  4. Scala 方法与函数简单记录
  5. matlab中乘法和点乘以及除法和点除的联系是什么?
  6. 剑指Offer-14:输入一个链表,输出该链表中倒数第k个结点。
  7. MySql查询分页数据
  8. 关于Unity中的物理
  9. ubuntu install redis/mongo 以及 监控安装
  10. 依赖注入(DI)