1、在注解注入方式中,首先要在xml中引入如下的红线的命名空间:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
  3. <context:annotation-config />
  4. </<beans>
同时要引入apache开源组织的common-annotation.jar (JSR-250中的注解)。
2、在注解中应用到的注解:
@AutoWired和@Resource
(1)@AutoWired和@Resource的区别
@AutoWired默认按类型进行装配。
@Resource默认情况下按名称进行装配,当按名称搜素不到的情况下,再按类型进行装配。但是如果指定了name属性则就只按名称进行装配(@Resource(name="personDao"))。
3、按注解进行装配的例子
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. - <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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
  3. <context:annotation-config />
  4. <bean id="personDao" class="com.cvicse.dao.impl.PersonDaoBean" />
  5. <bean id="personService" class="com.cvicse.service.impl.PersonServiceBean"/>
  6. </beans>
 在程序中的应用
  1. public class PersonServiceBean implements PersonService {
  2. @Resource private PersonDao personDao;
  3. private String name;
  4. public void setPersonDao(PersonDao personDao) {
  5. this.personDao = personDao;
  6. }
  7. public PersonServiceBean(){}
  8. public PersonServiceBean(PersonDao personDao, String name) {
  9. this.personDao = personDao;
  10. this.name = name;
  11. }
  12. public void save(){
  13. personDao.add();
  14. }
  15. }

 在程序中标注了@Resource后,spring容器会默认根据名字进行在xml中搜素id为personDao的bean,如果搜索不到则按PersonDao 的接口去搜索相应的实现类型com.cvicse.dao.impl.PersonDaoBean。

@Autowired注解和前面的@resource用法是一样的。如果要将@Autowired的注解按名称来进行搜素,则需要如下的设置:
@Autowired@Qualifier("personDao")
另外,@Autowired有一个required的属性:
@Autowired(required=false) @Qualifier("personDao"):表示当在配置文件中搜索不到时就把该属性注入为null。
@Autowired(required=true) @Qualifier("personDao"):表示当在配置文件中搜索不到时抛异常。
4、自动装配
      即在配置文件中设置相应的装配类型由spring容器去进行装配。在spring的开发中不建议使用自动装配,因为自动装配会发生预想不到的未知的错误。
Autowired属性取值如下: 
 byType: 按照类型自动装配,可以根据属性的类型,在容器中寻找跟类型匹配的bean。如果发现多个,那么会抛出异常。如果没有找到,即属性值为null. 
 byName: 按照名称装配,可以根据属性的名称,在容器中寻找跟该属性名相同的bean,如果没有找到,即属性值为null。 
 constructor : 与byType的方式类似,不同之处在于它应用于构造器参数。如果在人那个其中没有找 
到,即属性值为null。 
 autodetect :通过bean类的自省机制(introspection)来决定是使用constructor还是byType方式进行自动装配。(如果发现默认的构造器,那么将使用byType方式) 
  1. public class PersonServiceBean implements PersonService {
  2. private PersonDao personDao;
  3. private String name;
  4. public PersonServiceBean() {
  5. }
  6. public void save()
  7. {
  8. personDao.save();
  9. }
  10. }

 配置文件的配置如下:

  1. <bean id="personDao" class="com.cvicse.dao.PersonDao"/>
  2. <bean id="personServiceBean"
  3. class="com.cvicse.service.impl.PersonServiceBean" autowire="byType"/>
5、自动扫描方式注入组件
      前面我们都是使用XML的bean定义来配置组件。在一个稍大的项目中,通常会有上百个组件,如果这些组件采用xml的bean定义来配置,显然会增加配置文件的体积,查找及维护起来也不太方便。Spring2.5为我们引入了组件自动扫描机制,它可以在类路径下寻找标注了@Component、@Service、@Controller、@Repository注解的类,并把这些类纳入到spring的容其中管理。它的作用和在xml文件中使用bean节点配置组件是一样的。要使用自动扫描机制,我们需要打开一下配置信息:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. 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-2.5.xsd
  4. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
  5. <context:component-scan base-package="com.cvicse"/>
  6. </bean>

其中,base-package就是要扫描的包以及子包 

@Service用于标注业务层组件 
@Controller用于标注控制层组件(如struts中的action) 
@Repository用于标注数据访问层组件,即DAO 
@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。

  1. import org.springframework.stereotype.Repository;
  2. @Repository
  3. public class PersonDao {
  4. public void save() {
  5. System.out.println("save");
  6. }
  7. }

这样,当容器扫描到此类的时候就纳入了容器里。 
  1. import org.springframework.stereotype.Service;
  2. import com.cvicse.dao.PersonDao;
  3. import com.cvicse.service.PersonService;
  4. @Service
  5. public class PersonServiceBean implements PersonService {
  6. @Resource private PersonDao personDao;
  7. public void setPersonDao(PersonDao personDao) {
  8. this.personDao = personDao;
  9. }
  10. public void save()
  11. {
  12. personDao.save();
  13. }
  14. }

这样我们如何调用此bean?也没有配置文件里的id.spring规范这样规定:在自动扫描的情况下,要得到一个容器管理的bean,可以提供bean的全名,但是第一个字符小写!
  1. ApplicationContext ctx = new
  2. ClassPathXmlApplicationContext("beans.xml");
  3. PersonService personService =
  4. (PersonService)ctx.getBean("personServiceBean ");
  5. personService.save();
当然,可以自己指定被调用的时候用的名称(默认都是单例模式,即scope=singleton)
  1. @Service(“personService”)
  2. public class PersonServiceBean implements PersonService

如果要改成原型模式怎么做呢?这样
  1. @Service(“personService”) @Scope(“prototype”)
  2. public class PersonServiceBean implements PersonService
但是这样如何实现指定初始化和销毁方法呢?spring采用的是注解方式!

Spring学习 Ioc篇(三)的更多相关文章

  1. Spring学习 Ioc篇(一 )

    一直以来忙于项目的开发,Spring虽然不用,一直想系统地学习一下,想看看它的源码,都没有时间,这段时间比较充裕,就索性先把Spring学习下,熟悉各个功能再去探究它内部的实现.就从Ioc篇开始学习. ...

  2. Spring学习 Ioc篇(二 )

    5.spring依赖注入的方式 方法一:使用构造器方式进行注入 1.dao的类和接口 package com.cvicse.dao.impl; import com.cvicse.dao.Person ...

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

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

  4. 死磕Spring之IoC篇 - 开启 Bean 的加载

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

  5. 死磕Spring之IoC篇 - Bean 的创建过程

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

  6. 死磕Spring之IoC篇 - @Autowired 等注解的实现原理

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

  7. 死磕Spring之IoC篇 - Spring 应用上下文 ApplicationContext

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

  8. 死磕Spring之IoC篇 - 文章导读

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

  9. 死磕Spring之IoC篇 - BeanDefinition 的加载阶段(XML 文件)

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

随机推荐

  1. Gradient Boost Decision Tree(GBDT)中损失函数为什么是对数形式

    由于最近要经常用到XGBOOST的包,不免对相关的GBDT的原理又重新学习了一遍, 发现其中在考虑损失函数的时候,是以对数log进行度量的,囿于误差平方和函数的印象 那么为什么是对数呢?可能是下面的原 ...

  2. 解决ssh链接服务器超时自动断开的问题

    为了安全性:ssh默认的连接超时时间很短:经常就是发个呆就断开了:事实上是可以修改超时时间的. 示例环境: 服务器:centos6.5 1:[root@iZ28qa8jt4uZ /]cp /etc/s ...

  3. SQL Server Profiler教程

    SQL Server Profiler是SQL Server企业版自带的一个sql 语句跟踪和分析工具,功能十分强大.熟练地使用它,对我们分析数据库性能问题很有帮助,比如当数据访问使用EF等ORM框架 ...

  4. AspNetPager分页

    1.页面部分 <%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefi ...

  5. ASIHTTPRequest下载示例(支持断点续传)

    一.创建网络请求队列 首先,创建网络请求队列,如下: ASINetworkQueue   *que = [[ASINetworkQueue alloc] init]; self.netWorkQueu ...

  6. Ubuntu下安装Java环境

    1 Java 8 下载地址 http://www.oracle.com/technetwork/cn/java/javase/downloads/jdk8-downloads-2133151-zhs. ...

  7. Python开发入门与实战10-事务

    1. 事务 本章我们将通过一个例子来简要的说明“事务”,这个开发实战里经常遇到的名词.事务是如何体现在一个具体的业务和系统的实现里. 事务是通过将一组相关操作组合为一个,要么全部成功要么全部失败的单元 ...

  8. Python的平凡之路(8)

    (本文是对平凡之路(7)的补充等) 一.动态导入模块 import importlib __import__('import_lib.metaclass') #这是解释器自己内部用的 #importl ...

  9. java中的包以及内部类的介绍

    1:形式参数和返回值的问题(理解)    (1)形式参数:        类名:需要该类的对象        抽象类名:需要该类的子类对象        接口名:需要该接口的实现类对象    (2)返 ...

  10. mysql、pymysql、SQLAlchemy

    1.MySQL介绍 http://www.cnblogs.com/wupeiqi/articles/5699254.html,基础操作参见此文章,此处不赘述. 安装:yum install mysql ...