spring_150803_service
实体类:
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的更多相关文章
随机推荐
- Tutorial: Facebook analytics using Power BI Desktop
In this tutorial you learn how to import and visualize data from Facebook. During the tutorial you'l ...
- python中的小技巧
1.求1~100以内的素数 prime=filter(lambda x: not [x%i for i in range(2,x) if x%i==0], range(2,101))#列表推导,一行搞 ...
- Effective Objective-C 2.0之Note.04
“类族”(class cluster)是一种很有用的模式(pattern),可以隐藏“抽象基类”(abstract base class)背后的实现细节.Objective-C的系统框架中普遍使用此模 ...
- Objective-C 一些概念
Automatic Reference Counting (ARC)
- 横屏下的ImagePickerController
Try this way.... As per Apple Document, ImagePicker Controller never Rotate in Landscape mode. You h ...
- C# SQL增删查改
DBHelper: /// <summary> /// 执行查询 /// </summary> /// <param name="sql">有效 ...
- Delphi 递归搜索.SVN文件夹并“处理”
实在是没有想到删除.SVN文件夹是件这么痛苦的事情,最近在整理公司历史资料文件,粗略统计一下600G左右,本想一次提交到服务器上省事,没想到啊没想到,传输中途程序崩掉了,本地文件夹剩下一大堆.svn文 ...
- loadView, viewDidLoad 快速使用
一 loadView: 在每次访问 UIViewController时,且其 view = nil 时,会调用这个方法,所以大家在开发中想自己设置 view 的可以用这个方法,在这个方法中自定义 v ...
- 带搜索框的下拉框chosen.jQury.js
下载所需js,css png资源 <link href="chosen.css" rel="stylesheet" type="text ...
- shell脚本变量定义注意别跟系统变量重名了……
写了个很简单的脚本,执行结果却很奇怪. 1 #!/bin/bash PATH=$HOME/vlc_transplant4 rm -f $PATH/vlc ln -s $PATH/bin/vlc-s ...