实体类:

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. Ubuntu无值守安装mysql

    1. 使用apt-get -d install 命令下载安装包, 其中-d表示下载不安装. 下载后的deb包放在/var/cache/apt/archives目录 2. 使用dpkg-preconfi ...

  2. C#和ASP.Net面试题目集锦

    1.有哪几种方法可以实现一个类存取另外一个类的成员函数及属性,并请举列来加以说明和分析.2.A类是B类的基类,并且都有自己的构造,析构函数,请举例证明B类从实例化到消亡过程中构造,析构函数的执行过程. ...

  3. 浅析Objective-C字面量

    编写Objective-C程序时,总会用到某几个类,它们属于Foundation框架.虽然从技术上来说,不用Foundation框架也能写出Objective-C代码,但实际上却经常要用到此框架.这几 ...

  4. [转]ubuntu(12.04)下, 命令 ,内核 源代码的获取

    [转]ubuntu(12.04)下, 命令 ,内核 源代码的获取 http://blog.chinaunix.net/uid-18905703-id-3446099.html 1.命令:例如:要查看l ...

  5. eclipse插件开发中全局对象的获取

    转自:http://blog.csdn.net/liaomin416100569/article/details/7165425 1.获取系统默认的ShellShell shell = Platfor ...

  6. iptables端口转发

    iptables -t nat -A PREROUTING -d {外网ip}/32 -p tcp -m tcp --dport 4956 -j DNAT --to-destination 10.1. ...

  7. Eclipse中的常用快捷键

    快捷修复 Command+1 //int a=100L; //int a=(int) 100L; 快捷删除行 Command+D 快速起新行 Shift+Enter (当本行代码很长时,将光标定在本行 ...

  8. win8.1 cygwin编译java轻量虚拟机avian

    1.背景 昨天在网上看到别人用aauto写本地小程序写的很爽,我觉得如果java的jre能小一点,凭借java庞大的第三方类库写小工具也还算不错的.本人就经常用eclipse+一些commons包写些 ...

  9. 【Integer To Roman】cpp

    题目: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from ...

  10. 【BZOJ】【1019】【SHOI2008】汉诺塔

    递推/DP 类似普通汉诺塔的一个递推(模拟?$10^{18}$没法模拟吧…… 题解:http://blog.csdn.net/regina8023/article/details/43016813 因 ...