先引入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. Codeforces Round #280 (Div. 2)E Vanya and Field(简单题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 本场题目都比较简单,故只写了E题. E. Vanya and Field Vany ...

  2. for循环执行顺序

    for循环的执行顺序用如下表达式: for(expression1;expression2;expression3) { expression4; } 执行的顺序应该是: 1)第一次循环,即初始化循环 ...

  3. 监听SWT文本框只能输入数字

    在SWT开发中,很多时候需要文本框只能输入数字(当输入字母或者其他字符时为无效),这个时候需要给文本框设置监听VerifyListener, code 如下: text.addVerifyListen ...

  4. 修改Linux中的用户名

    需要修改2个文件: /etc/hosts /etc/sysconfig/network 然后重启 1.修改/etc/sysconfig/network NETWORKING=yes HOSTNAME= ...

  5. Hadoop2.4.1入门实例:MaxTemperature

    注意:以下内容在2.x版本与1.x版本同样适用,已在2.4.1与1.2.0进行测试. 一.前期准备 1.创建伪分布Hadoop环境,请参考官方文档.或者http://blog.csdn.net/jed ...

  6. 【Nutch2.2.1基础教程之2.1】集成Nutch/Hbase/Solr构建搜索引擎之一:安装及运行【单机环境】

    1.下载相关软件,并解压 版本号如下: (1)apache-nutch-2.2.1 (2) hbase-0.90.4 (3)solr-4.9.0 并解压至/usr/search 2.Nutch的配置 ...

  7. 关于LayoutParams

    每一个布局均有一个叫LayoutParams的内部类,如: LinearLayout.LayoutParams  RelativeLayout.LayoutParams  AbsoluteLayout ...

  8. 使用SQL脚本访问操作远程数据库

    USE [Hik] GO /****** Object: StoredProcedure [dbo].[sp_test] Script Date: 08/21/2015 09:55:21 ****** ...

  9. Django学习(六) 模板

    下面是一个新闻的模板:mysite/news/templates/news/year_archive.html mysite/news/templates/news/year_archive.html ...

  10. WebGL 初探

    官方网站:http://webglfundamentals.org/ WebGL是一种3D绘图标准,这种绘图技术标准允许把JavaScript和OpenGL ES 2.0结合在一起,通过增加OpenG ...