新建java project工程,建src、conf、test源码文件夹,导入相关包,需要spring的相关jar包和common-logging相关jar包

接口Service:

  1. package com.spring;
  2.  
  3. public interface DogPetService {
  4. public void queryAllDogPets();
  5. }

实现类ServiceImpl:

  1. package com.spring.service.impl;
  2.  
  3. import java.util.List;
  4.  
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.beans.factory.annotation.Qualifier;
  7.  
  8. import com.spring.DogPetService;
  9. import com.spring.dao.DogPetDAO;
  10. import com.spring.model.DogPet;
  11.  
  12. public class DogPetServiceImpl implements DogPetService{
  13.  
  14. private DogPetDAO dogPetDAO;
  15.  
  16. public DogPetDAO getDogPetDAO() {
  17. return dogPetDAO;
  18. }
  19.  
  20. @Autowired
  21. public void setDogPetDAO(@Qualifier("dogPetDAO111") DogPetDAO dogPetDAO) {
  22. this.dogPetDAO = dogPetDAO;
  23. }
  24.  
  25. @Override
  26. public void queryAllDogPets() {
  27. List<DogPet> list = dogPetDAO.queryAllDogPets();
  28. if(list != null)
  29. {
  30. for(DogPet d:list)
  31. {
  32. System.out.println(d.toString());
  33. }
  34. }
  35. }
  36.  
  37. }

Service调用的DAO类:

  1. package com.spring.dao;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import com.spring.model.DogPet;
  7.  
  8. public class DogPetDAO {
  9.  
  10. public List<DogPet> queryAllDogPets()
  11. {
  12. List<DogPet> list = new ArrayList<DogPet>();
  13.  
  14. DogPet d1 = new DogPet();
  15. d1.setId(1111);
  16. d1.setName("dog1");
  17. d1.setAge(4);
  18. d1.setKind("buladuo");
  19. d1.setSex("B");
  20. d1.setHealth("good");
  21. DogPet d2 = new DogPet();
  22. d2.setId(2222);
  23. d2.setName("dog2");
  24. d2.setAge(3);
  25. d2.setKind("buladuo");
  26. d2.setSex("G");
  27. d2.setHealth("good");
  28.  
  29. list.add(d1);
  30. list.add(d2);
  31.  
  32. return list;
  33. }
  34. }

配置文件beans.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"
  3. xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
  8. http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
  9. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
  10. <context:annotation-config/>
  11.  
  12. <bean id="dogPetService" class="com.spring.service.impl.DogPetServiceImpl">
  13.  
  14. </bean>
  15.  
  16. <bean id="dogPetDAO111" class="com.spring.dao.DogPetDAO">
  17.  
  18. </bean>
  19. </beans>

test类,需要引入junit4的相关jar包:

  1. package com.spring.test;
  2.  
  3. import org.junit.Test;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6.  
  7. import com.spring.DogPetService;
  8.  
  9. public class AutoWiredTest {
  10.  
  11. @Test
  12. public void queryAllDogPets()
  13. {
  14. ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
  15. DogPetService dogPetService = (DogPetService)ctx.getBean("dogPetService");
  16. dogPetService.queryAllDogPets();
  17. }
  18. }

spring_150801_autowired_qualifier的更多相关文章

随机推荐

  1. Javascript是一个事件驱动语言

    面向原型这种说法我没在网上找到

  2. iOS 中使用md5加密

    #import <CommonCrypto/CommonDigest.h> @implementation MD5Util +(NSString *)encode:(NSString *) ...

  3. 001--VS2013 c++ 游戏框架

    头文件:MainClass.h 内容: #include <Windows.h> //全局函数声明LRESULT CALLBACK WndProc(HWND hwnd, UINT mess ...

  4. plist 读取 swift

    // // ViewController.swift // plist读写 // // Created by mac on 15/7/13. // Copyright (c) 2015年 fangyu ...

  5. GCC常用命令行一览表

    GCC常用命令行一览表 这些常用的 gcc/g++ 命令行参数,你都知道么?1. gcc -E source_file.c-E,只执行到预编译.直接输出预编译结果. 2. gcc -S source_ ...

  6. android开发 无预览定时拍照

    demo实现功能: 打开主页面自动启动定时拍照,10s拍一次. 注意事项,初始化摄像头之后不能立即拍照,否则无效,必须等待几秒后才能拍.这里用的是Handler进行延时处理拍照消息的. package ...

  7. MySQL 字符串截取相关函数

    MySQL 字符串截取相关函数 在工作中,可能需要将某些字段按某个分割符组成一个字符串作为字段值存取到数据库表中,比如某个任务对应三个结果,分别存储在不同的数据表中,这时可以将这三个不同表的主键按照约 ...

  8. Codeforces Round #278 (Div. 2)

    题目链接:http://codeforces.com/contest/488 A. Giga Tower Giga Tower is the tallest and deepest building ...

  9. 【CoreData】parent-child关系ManagedObjectContext应用

    当我们一开始使用CoreData框架和唯一的MOC进行应用的数据持久化的时候,如果创建项目的时候选择了“使用CoreData”,这会是XCode自动生成的模板代码的样子. 同时,配合NSFetched ...

  10. .NET设计模式(3):抽象工厂模式(Abstract Factory)(转)

    概述 在软件系统中,经常面临着“一系列相互依赖的对象”的创建工作:同时由于需求的变化,往往存在着更多系列对象的创建工作.如何应对这种变化?如何绕过常规的对象的创建方法(new),提供一种“封装机制”来 ...