Spring练习,定义三个模块,使用<import>标签完成分模块配置开发,模拟实现学生借书和还书的过程,将结束输出到控制台。

相关 知识 >>>

相关 练习 >>>

 

实现要求:

在图书管理系统中,学生管理模块、书籍管理模块和借还书管理模块等其他模块,相互配合协作,促使系统的运行流畅。定义三个模块,使用<import>标签完成分模块配置开发,模拟实现学生借书和还书的过程,将结束输出到控制台。

要求如下:

  • 定义学生管理模块、书籍管理模块和借还书管理模块。
  • 使用<import>标签完成分模块配置开发

实现步骤:

在com.zn.demo.student包下创建Student类,添加stuId属性和stuName属性

package com.zn.mhys.demo.student;

public class Student {
	private String stuId;
	private String stuName;
	public String getStuId() {
		return stuId;
	}
	public void setStuId(String stuId) {
		this.stuId = stuId;
	}
	public String getStuName() {
		return stuName;
	}
	public void setStuName(String stuName) {
		this.stuName = stuName;
	}
	
}

在com.zn.demo.student包下创建applicationContext-student.xml配置文件,注册Student类到容器

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.springframework.org/schema/beans"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">

	<bean id="student" class="com.zn.mhys.demo.student.Student"></bean>
	
</beans>

在com.zn.demo.book包下创建Book类,添加bookId属性和bookName属性

package com.zn.mhys.demo.book;

public class Book {
	private String bookId;
	private String bookName;
	public String getBookId() {
		return bookId;
	}
	public void setBookId(String bookId) {
		this.bookId = bookId;
	}
	public String getBookName() {
		return bookName;
	}
	public void setBookName(String bookName) {
		this.bookName = bookName;
	}
	
}

在com.zn.demo.book包下创建applicationContext-book.xml配置文件,注册Book类到容器。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.springframework.org/schema/beans"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">

	<bean id="book" class="com.zn.mhys.demo.book.Book"></bean>
	
</beans>

在com.zn.demo.service包下创建BorrowAndReturnService类,声明borrowBook()方法和returnBook()方法

package com.zn.mhys.demo.service;

import com.zn.mhys.demo.book.Book;
import com.zn.mhys.demo.student.Student;

public class BorrowAndReturnService {
	public void borrowBook(Student student,Book book){
		System.out.println("学生编号:"+student.getStuId()+",学生姓名:"+student.getStuName()
		+",借阅书名:"+book.getBookName()+",编号:"+book.getBookId()+"的书名");
	}
	public void returnBook(Student student,Book book){
		System.out.println("学生编号:"+student.getStuId()+",学生姓名:"+student.getStuName()
		+",归还书名:"+book.getBookName()+",编号:"+book.getBookId()+"的书名");
	}
}

在com.zn.demo.service包下创建applicationContext-service.xml配置文件,注册BorrowAndReturnService类到容器

<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
		xmlns="http://www.springframework.org/schema/beans" 
		xmlns:p="http://www.springframework.org/schema/p" 
		xmlns:context="http://www.springframework.org/schema/context" 
		xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-4.2.xsd ">
	
	<!-- 2.1.4 -->
	<import resource="com/zn/mhys/demo/book/applicationContext-book.xml"/>
	<import resource="com/zn/mhys/demo/student/applicationContext-student.xml"/>
	<import resource="com/zn/mhys/demo/service/applicationContext-service.xml"/>
	
</beans>

在src目录下创建applicationContext.xml配置文件,引入3个模块的配置文件 在com.zn.demo.test包下创建Test测试类

package com.zn.mhys.demo.test;

import java.sql.Connection;

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

import com.zn.mhys.demo.book.Book;
import com.zn.mhys.demo.service.BorrowAndReturnService;
import com.zn.mhys.demo.service.JdbcService;
import com.zn.mhys.demo.student.Student;

public class Test {

	public static void main(String[] args) {

		ClassPathResource resource = new ClassPathResource("applicationContext.xml");
		XmlBeanFactory context = new XmlBeanFactory(resource);
		
//		2.1.4
		Student student = (Student)context.getBean("student");
		student.setStuId("003");
		student.setStuName("杨明金");
		Book book = (Book)context.getBean("book");
		book.setBookId("130006");
		book.setBookName("spring企业级开发");
		BorrowAndReturnService bars = (BorrowAndReturnService) context.getBean("service");
		bars.borrowBook(student, book);
		bars.returnBook(student, book);
		
	}

}

 

posted @ 2021-05-14 14:10  明金同学  阅读(35)  评论(0编辑  收藏  举报