先引入jar包,common-annotations.jar

接着上代码:

1、dao接口
package com.dao;

public interface OkpDao {
public void save();
}

  

2、dao实现
package com.dao.impl;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component; import com.dao.OkpDao;
@Component
public class OkpDaoImpl implements OkpDao{ public void save() {
System.out.println("OkpDaoImpl.save()");
}
}

  

@Component注解:将该类加载为spring容器的组件,相当于在spring配置文件文件中的<bean name="okpDaoImpl" class="com.dao.Impl.OkpDaoImpl"/>,@Component注解格式为
@Component("name") 手动设置bean配置的name,如果不设置会自动设为 "类名首字母小写" 。
这四个注解用法基本一样,只是明确的分出哪个处理的层。@controller 控制器(注入服务) @service 服务(注入dao) @repository dao(实现dao访问) @component ,将其他几个注解标签替换上述代码的@Component标签会有同样的效果。

3、service
package com.service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource; import org.springframework.stereotype.Component; import com.dao.OkpDao;
@Component
public class OkpService {
private OkpDao okpDao; public OkpDao getOkpDao() {
return okpDao;
}
@Resource(name="okpDaoImpl")
public void setOkpDao(OkpDao okpDao) {
this.okpDao = okpDao;
}
public void save(){
this.okpDao.save();
}
@PostConstruct
public void init(){ System.out.println("容器创建前执行");
} @PreDestroy
public void destory(){
System.out.println("容器销毁后执行");
}
}

@Component也是将name="okpService"的bean加入到spring容器 
@Resource将name="okpDaoImpl"的bean 注入到okpDao,该注解先根据ByName找相应的bean,如果没有找到就会根据ByType查找,现在推荐使用该注解,不推荐使用@Autowired(根据ByTpe装配)以及@Qualifier(根据ByName装配)---该两个注解一般联合使用

@Autowired
public void setOkpDao(@Qualifier("okpDaoImpl")OkpDao okpDao) {
this.okpDao = okpDao;
}

  


  因为根据ByType进行查找有可能会有多个bean,到处出错,所以还需要@Qualifier
  @PostConstruct 标记的方法代表容器创建之前执行的方法
  @PreDestory标记的方法代表容器销毁之后执行的方法
  还有一个不太常用的 @Scope代表该类的作用范围 value默认为Singleton单例模式,proptotype原型(每次都会创建一个新的对象)。
4、Service测试类
package com.service;

import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class OkpServiceTest { @Test
public void testSave() {
BeanFactory bf=new ClassPathXmlApplicationContext("applicationContext.xml");
OkpService okp=(OkpService) bf.getBean("okpService");
okp.save();
} }
  5、spring配置文件  applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="com"></context:component-scan>
</beans>
  标签 <context:annotation-config/> 使用注解
  <context:component-scan base-package="com"></context:component-scan> 使用@Component注解 要加上这句,他会根据base-package的包名,扫描他下面所有的包,以及包中用@component注解的类 注册到Spring容器。
 
  由于刚开始学习Spring 注解,有什么理解有误的地方,求大婶告知。

Spring的IOC注解学习的更多相关文章

  1. Spring的IOC注解开发入门1

    基本知识点如下: 引入注解约束,配置组件扫描 类上的注解: @Conponent  @Controller @Service @Repository 普通属性的注解   @value 对象属性的注解  ...

  2. Spring的IOC容器学习笔记

    (一)Spring的IOC学习 在applicationContext.xml来配置bean,通过该接口,在主程序中,可以指定初始化的对象,不需要在进行赋值操作,直接在xml里配置好. 接下来分享的是 ...

  3. Spring的IOC注解开发入门2

    注解方式设置属性的值 在我们IOC基于xml属性注入的方式中有(一般推荐set方法) 构造方法注入普通值:<constructor-arg>的使用 set方法注入普通值:<prope ...

  4. Spring框架 IOC注解

    Spring框架的IOC之注解方式的快速入门        1. 步骤一:导入注解开发所有需要的jar包        * 引入IOC容器必须的6个jar包        * 多引入一个:Spring ...

  5. Spring使用ioc注解方式配置bean

    context层 : 上下文环境/容器环境 applicationContext.xml 具体示例: 现在ioc容器中添加context层支持: 包括添加xmlns:context.xsi:schem ...

  6. spring学习笔记三:Component注解(把POJO类实例化到spring的IOC容器中)

    Component注解:把普通的POJO 类实例化到spring的IOC容器中,就是定义成<bean id="" class=""> 项目目录树: ...

  7. Spring框架学习(6)使用ioc注解方式配置bean

    内容源自:使用ioc注解方式配置bean context层 : 上下文环境/容器环境 applicationContext.xml 1 ioc注解功能 注解 简化xml文件配置 如 hibernate ...

  8. Spring入门IOC和AOP学习笔记

    Spring入门IOC和AOP学习笔记 概述 Spring框架的核心有两个: Spring容器作为超级大工厂,负责管理.创建所有的Java对象,这些Java对象被称为Bean. Spring容器管理容 ...

  9. 死磕Spring之IoC篇 - BeanDefinition 的解析过程(面向注解)

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读 Spring 版本:5.1. ...

随机推荐

  1. (转) Special members

    原地址:http://www.cplusplus.com/doc/tutorial/classes2/   Special members [NOTE: This chapter requires p ...

  2. C# String 与 byte 互转

    String转换为byte数组用byte[] arr = System.Text.Encoding.Default.GetBytes("abcde") byte数组转换为Strin ...

  3. spring3+hibernate3+(dbcp+oracle+拦截器事务配置)整合(一)

    1.applicationContext-base.xml文件 <?xml version="1.0" encoding="UTF-8"?>< ...

  4. 一些常用Linux命令简记

    1.重命名文件夹 mv xxx/ yyy/  将xxx文件夹重命名为yyy(前提是当前目录没有yyy文件夹,否则就移进去了!) 2.数据盘重新挂载 一.# umount /mnt(卸载硬盘已挂载的mn ...

  5. Web框架-Django基础

    一.django简介 Django是一个开放源代码的Web应用框架,由Python写成.采用了MVC的软件设计模式,即模型M,视图V和控制器C.它最初是被开发来用于管理劳伦斯出版集团旗下的一些以新闻内 ...

  6. ipython的notebook

    ipython是增强的python交互式shell.而notebook是在浏览器上运行ipython ubuntu下安装: sudo apt-get install ipython3 sudo apt ...

  7. eclipse 使用mvn模块化开发

    1.创建maven父工程步骤:new-->other-->选择maven project-->next-->勾选create a simple project-->nex ...

  8. JS封装cookie操作函数实例(设置、读取、删除)

    本文实例讲述了JS封装cookie操作函数.分享给大家供大家参考,具体如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ...

  9. Kyoya and Colored Balls(组合数)

    Kyoya and Colored Balls time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  10. SPOJ GCDEX (数论)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 题意:求sigma (gcd (i , j))  ...