关闭

Spring学习札记:第2节 属性注入方式以及模拟BeanFactory实现

369人阅读 评论(0) 收藏 举报
分类:

属性注入方式大致有:
Setter注入
构造方法注入
注解注入
(1)java annotation
(2)spring annotation

 

下面使用MyEclipse完成一个Setter注入属性的实例。


1.建立一个JAVA Project,名字为:Spring_IocByEclipse
2.导入Spring核心包:Spring 3.0 Core Libraries
3.创建如下几个包及相应的类:
(1)实体包及相应的类

包:net.nw.vo

类:Students.java

package net.nw.vo;

//学生类
public class Students {
	private String sid;     //学号
	private String sname;   //姓名
	private int age;		//年龄
	
	public String getSid() {
		return sid;
	}
	public void setSid(String sid) {
		this.sid = sid;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}        
	
	
}


 

(2)DAO包及相应的类

包:net.nw.dao(接口层)

类:StudentsDAO.java

package net.nw.dao;

import net.nw.vo.Students;

public interface StudentsDAO {
	//保存学生
	public boolean saveStudents(Students s);

}



(3)DAO实现包

包:net.nw.dao.impl,接口实现层

类:StudentsDAOImpl.java

package net.nw.dao.impl;

import net.nw.dao.StudentsDAO;
import net.nw.vo.Students;

public class StudentsDAOImpl implements StudentsDAO {

	@Override
	public boolean saveStudents(Students s) {
		// TODO Auto-generated method stub
		if(s!=null){
			System.out.println("学号:"+s.getSid());
			System.out.println("姓名:"+s.getSname());
			System.out.println("年龄:"+s.getAge());
			return true;
		}else{
			return false;
			
		}
			
		
	}

}


(4)服务层包

包:net.nw.service

类:StudentsService.java

package net.nw.service;

import net.nw.dao.StudentsDAO;
import net.nw.vo.Students;

public class StudentsService {
	private StudentsDAO sDao;
	
	
	public StudentsDAO getsDao() {
		return sDao;
	}


	public void setsDao(StudentsDAO sDao) {
		this.sDao = sDao;
	}


	//保存学生
	public boolean saveStudents(Students s){
		if(sDao.saveStudents(s)){
			return true;
		}else{
			return false;
		}
	}

}


(5)配置applicationContext.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
	<bean name="students" class="net.nw.vo.Students"/>
	<bean name="studentsDAO" class="net.nw.dao.impl.StudentsDAOImpl"/>
	<bean name= "studentsService" class="net.nw.service.StudentsService">
		<property name="sDao" ref="studentsDAO"></property>
	</bean>
</beans>


(6)测试类

建立一个源文件夹:test

建立一个测试包:net.nw.service

建立一个测试类:TestStudentsService.java

package net.nw.service;

import net.nw.vo.Students;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import junit.framework.Assert;
import junit.framework.TestCase;

public class TestStudentsService extends TestCase{
	public void testSaveStudents(){
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		Students s = (Students)ctx.getBean("students");
		s.setSid("2010101010");
		s.setSname("赵云");
		s.setAge(65);
		StudentsService sService = (StudentsService)ctx.getBean("studentsService");
		Assert.assertEquals(true, sService.saveStudents(s));
	}

}


4.测试结果:

学号:2010101010
姓名:赵云
年龄:65

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:10890次
    • 积分:258
    • 等级:
    • 排名:千里之外
    • 原创:14篇
    • 转载:13篇
    • 译文:0篇
    • 评论:1条