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

接口Service:

package com.spring;

public interface DogPetService {
public void queryAllDogPets();
}

实现类ServiceImpl:

package com.spring.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier; import com.spring.DogPetService;
import com.spring.dao.DogPetDAO;
import com.spring.model.DogPet; public class DogPetServiceImpl implements DogPetService{ private DogPetDAO dogPetDAO; public DogPetDAO getDogPetDAO() {
return dogPetDAO;
} @Autowired
public void setDogPetDAO(@Qualifier("dogPetDAO111") DogPetDAO dogPetDAO) {
this.dogPetDAO = dogPetDAO;
} @Override
public void queryAllDogPets() {
List<DogPet> list = dogPetDAO.queryAllDogPets();
if(list != null)
{
for(DogPet d:list)
{
System.out.println(d.toString());
}
}
} }

Service调用的DAO类:

package com.spring.dao;

import java.util.ArrayList;
import java.util.List; import com.spring.model.DogPet; public class DogPetDAO { public List<DogPet> queryAllDogPets()
{
List<DogPet> list = new ArrayList<DogPet>(); DogPet d1 = new DogPet();
d1.setId(1111);
d1.setName("dog1");
d1.setAge(4);
d1.setKind("buladuo");
d1.setSex("B");
d1.setHealth("good");
DogPet d2 = new DogPet();
d2.setId(2222);
d2.setName("dog2");
d2.setAge(3);
d2.setKind("buladuo");
d2.setSex("G");
d2.setHealth("good"); list.add(d1);
list.add(d2); return list;
}
}

配置文件beans.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config/> <bean id="dogPetService" class="com.spring.service.impl.DogPetServiceImpl"> </bean> <bean id="dogPetDAO111" class="com.spring.dao.DogPetDAO"> </bean>
</beans>

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

package com.spring.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.spring.DogPetService; public class AutoWiredTest { @Test
public void queryAllDogPets()
{
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
DogPetService dogPetService = (DogPetService)ctx.getBean("dogPetService");
dogPetService.queryAllDogPets();
}
}

spring_150801_autowired_qualifier的更多相关文章

随机推荐

  1. Windows PowerShell ISE

    Windows PowerShell 集成脚本环境 (ISE) 是 Windows PowerShell 的主机应用程序.在 Windows PowerShell ISE 中,可以在单一 Window ...

  2. Go语言类型switch

    switch还可以用于判断变量类型.使用方式为T.(type),即在变量后加上.(type).见代码: package main import ( "fmt" ) func mai ...

  3. iOS学习之C语言分支结构

    一.BOOL类型 返回值:真:YES   假:NO 定义一个布尔类型的变量 YES == 1, NO == 0 计算机在识别时,YES就替换成1,NO就替换成0 BOOL isGirl = YES; ...

  4. EF4.1之贪婪加载和延迟加载

    默认情况下,EF4.1是只查询到涉及到的数据对象,但是EF4.1支持两种方法进行控制其加载: 1.贪婪加载 2.延迟加载 使用的表还是上次使用的Order 和 OrderDetails两张表来举例说明 ...

  5. VHDL----基础知识1

    摘要: 打算分几篇,来理清VHDL的基础知识 ----------------------------------------------------------------------------- ...

  6. mysql 字段编码该为utf8mb4

    alter table c_comment modify column content varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unico ...

  7. 基于PBOC电子钱包的圈存过程详解

    基于pboc的电子钱包的圈存过程,供智能卡行业的开发人员参考 一. 圈存 首先终端和卡片有一个共同的密钥叫做圈存密钥:LoadKey   (Load即圈存的意思,unLoad,是圈提的意思) 假设Lo ...

  8. 53张牌中找出缺少的牌的花色和点数--raid3,4,5,6的容错原理

    一副扑克牌,抽出一张,要求找出抽出的牌的点数和花色. 算法的主要思想就是用异或运算来确定丢的牌的花色.四种花色分别如下表示:红桃用1(二进制0001)表示,黑桃用2(二进制0010)表示,黑桃用4(0 ...

  9. Bootstrap入门五:表格

    table样式: .table:表格基本样式,很少的padding,灰色的细水平分隔线. .table-striped:斑马纹样式,隔行换色. .table-bordered:为表格和其中的每个单元格 ...

  10. ASP.Net MVC中数据库数据导出Excel,供HTTP下载(转)

    转自http://www.cnblogs.com/hipo/archive/2012/03/13/2394019.html 一.关于下载 一般对下载权限有没有限制,或安全性要求不高的情况下,基于web ...