实体类:

package com.spring.model;

public class DogPet {

    private int id;
private String name;
private int age;
private String kind;
private String sex;
private String health;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getKind() {
return kind;
}
public void setKind(String kind) {
this.kind = kind;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getHealth() {
return health;
}
public void setHealth(String health) {
this.health = health;
} public String toString()
{
return id+"--"+name+"--"+kind+"--"+age+"--"+health;
}
}

接口Service:

package com.spring.service;

public interface DogPetService {
public void queryAllDogPets();
}

实现类ServiceImpl:

package com.spring.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.spring.service.DogPetService;
import com.spring.dao.DogPetDAO;
import com.spring.model.DogPet; @Service("dogPetService")
public class DogPetServiceImpl implements DogPetService{ private DogPetDAO dogPetDAO; public DogPetDAO getDogPetDAO() {
return dogPetDAO;
} @Resource(name="dogPetDAO2")
public void setDogPetDAO(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 org.springframework.stereotype.Service; import com.spring.model.DogPet; @Service(value="dogPetDAO2")
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/> <context:component-scan base-package="com.spring"></context:component-scan>
<!--
<bean id="dogPetService" class="com.spring.service.impl.DogPetServiceImpl"> </bean> <bean id="dogPetDAO1" class="com.spring.dao.DogPetDAO"> </bean>
-->
</beans>

test类:

package com.spring.test;

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

spring_150803_service的更多相关文章

随机推荐

  1. EasyUI datagrid frozencolumn的bug???

    今天碰到了个很蛋疼的问题.我用到了easyui 的 treegrid,内容只显示一列,我把它设置成了冻结列. 在谷歌调试下,因为内容比较多,所以,会有竖向的滚动条.但是,到了ie和火狐,滚动条神奇般没 ...

  2. UIImageView swift

    // // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...

  3. KinectStudio使用教程

    在Kinect SDK 2.0安装结束之后,会有一个KinectStudio的调试工具,他可以将动作记录下,以后即便脱离了Kinect传感器也可以愉快的调试了.现在我们来看看如何使用 首先打开Kine ...

  4. [shell基础]——I/O重定向

    文件标识符(FD) 1. Linux使用文件标识符(FD)来标识一个进程正在访问的特定文件 2. 当打开一个文件或创建一个文件时,Linux将返回一个文件标识符供其他操作引用 3. 文件标识符是一个小 ...

  5. HTML5就是现在:深入了解Polyfills

    http://blog.csdn.net/wang16510/article/details/8960312 https://github.com/Modernizr/Modernizr/wiki/H ...

  6. 电梯调度--c++--软件工程

    一.设计思路 (1)将乘客要去的楼层数存起来. (2)假设yi为乘客要爬楼层数之和,yi=n1*|(n1-ni)|+n2*|(n2-ni)|+..+n18*|(n18-ni)| (3)比较y1到y18 ...

  7. C#制作高仿360安全卫士窗体(三)

    距上篇C#制作高仿360安全卫士窗体(二)也将近一个多月了,这个月事情还是像往常一样的多.不多我也乐在其中,毕竟我做的是我喜欢做的东西.今天特地抽空把怎么制作文本框写一下.同时也希望有爱好这些玩意的同 ...

  8. 解决jquery-easyui1.3.3 combobox 多选模式不兼容IE8问题

    扩展Array的原型对象,加入indexOf方法 if(!Array.prototype.indexOf){    Array.prototype.indexOf = function(target) ...

  9. AngularJS打印问题

    http://stackoverflow.com/questions/22189544/print-a-div-using-javascript-in-angularjs-single-page-ap ...

  10. 【BZOJ】【1005】【HNOI2008】明明的烦恼

    Prufer序列/排列组合+高精度 窝不会告诉你我是先做了BZOJ1211然后才来做这题的>_>(为什么?因为我以前不会高精度呀……) 在A了BZOJ 1211和1089之后,蒟蒻终于有信 ...