相关 知识 >>>

相关 练习 >>>

实现要求:

在图书管理系统中,学生管理模块、书籍管理模块和借还书管理模块等其他模块,相互配合协作,促使系统的运行流畅。定义三个模块,使用<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); } }

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

  1. Python——import与reload模块的区别

     原创声明:本文系博主原创文章,转载或引用请注明出处. 1. 语法不同 import sys reload('sys') 2. 导入特性不同 import 和reload都可以对同一个模块多次加载, ...

  2. python 模块基础 和常用的模块

    模块的定义 一个模块就是以.py结尾的python 文件,用来从逻辑上组织python代码.注意,模块名和变量名一样开头不能用数字,可以是双下划线和字母. 为什么要用模块? 将一些复杂的需要重复使用的 ...

  3. python day 8: re模块补充,导入模块,hashlib模块,字符串格式化,模块知识拾遗,requests模块初识

    目录 python day 8 1. re模块补充 2. import模块导入 3. os模块 4. hashlib模块 5. 字符串格式:百分号法与format方法 6. 模块知识拾遗 7. req ...

  4. day16模块,导入模板完成的三件事,起别名,模块的分类,模块的加载顺序,环境变量,from...import语法导入,from...import *,链式导入,循环导入

    复习 ''' 1.生成器中的send方法 -- 给当前停止的yield发生信息 -- 内部调用__next__()取到下一个yield的返回值 2.递归:函数的(直接,间接)自调用 -- 回溯 与 递 ...

  5. Spring 笔记(三)Bean 装配

    前言 Spring 有两大核心,也就分成两份笔记分别记录. 其一是管理应用中对象之间的协作关系,实现方式是依赖注入(DI),注入依赖的过程也被称为装配(Wiring). 基于 JavaConfig 的 ...

  6. ECMAScript 6教程 (三) Class和Module(类和模块)

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出 原文连接,博客地址为 http://www.cnblogs.com/jasonnode/ .该系列课程是 ...

  7. MyEclipse Spring 学习总结三 SpringMVC

    MyEclipse Spring 学习总结三 SpringMVC 一.SpringMVC原理 1.Springmvc 框架介绍 1)Spring 框架停工了构建Web应用程序的全功能MVC模块.Spr ...

  8. 【死磕 Spring】—– IOC 之解析Bean:解析 import 标签

    原文出自:http://cmsblogs.com 在博客[死磕Spring]----- IOC 之 注册 BeanDefinition中分析到,Spring 中有两种解析 Bean 的方式.如果根节点 ...

  9. Spring的第三天AOP之xml版

    Spring的第三天AOP之xml版 ssm框架 spring  AOP介绍 AOP(Aspect Oriented Programming),面向切面编程.它出来的目的并不是去取代oop,而是对它的 ...

随机推荐

  1. C++ 数字分类

           1012 数字分类 (20分) 输入格式: 每个输入包含 1 个测试用例.每个测试用例先给出一个不超过 1000 的正整数 N,随后给出 N 个不超过 1000 的待分类的正整数.数字间 ...

  2. 100个Shell脚本——【脚本4】自定义rm命令

    [脚本4]自定义rm命令 linux系统的rm命令太危险,一不小心就会删除掉系统文件. 写一个shell脚本来替换系统的rm命令,要求当删除一个文件或者目录时,都要做一个备份,然后再删除.下面分两种情 ...

  3. spring注解-自动装配

    Spring利用依赖注入(DI)完成对IOC容器中中各个组件的依赖关系赋值 一.@Autowired 默认优先按照类型去容器中找对应的组件(applicationContext.getBean(Boo ...

  4. Linux:-e、-d、-f、-L、-r、-w、-x、-s、-h、

    -e filename 如果 filename存在,则为真 -d filename 如果 filename为目录,则为真 -f filename 如果 filename为常规文件,则为真 -L fil ...

  5. 【Spring Framework】Spring 入门教程(一)控制反转和依赖注入

    参考资料 Spring 教程 说在前面 什么样的架构,我们认为是一个优秀的架构? 判断准则:可维护性好,可扩展性好,性能. 什么叫可扩展性好? 答:在不断添加新的代码的同时,可以不修改原有代码,即符合 ...

  6. linux 操作只读变量

    由于该操作需要用到 gdb,所以需要先 安装好 gdb 1. 查询是否有gdb: 2. 如果没有,需要先执行 yum install gdb 命令进行安装 3. 定义 只读变量 abc 并打印值: a ...

  7. java 对 final 关键字 深度理解

    基础理解 : 1.修饰类 当用final去修饰一个类的时候,表示这个类不能被继承.处于安全,在JDK中,被设计为final类的有String.System等,这些类不能被继承 .注意:被修饰的类的成员 ...

  8. new Date()与setDate()参数

    New Date()与setDate()参数 相信网上已经有很多关于日期的文章了,这里只是我自己再工作中遇到的问题然后加以总结: new Date() new Date() 一共有六种形式,五种带参数 ...

  9. Apifox(2)快速上手apifox

    快速上手 使用场景 Apifox 是接口管理.开发.测试全流程集成工具,使用受众为整个研发技术团队,主要使用者为前端开发.后端开发和测试人员. 前端开发 接口文档管理 接口数据 Mock 接口调试 前 ...

  10. java多线程3:synchronized

    线程安全 多个线程共同访问一个对象的实例变量,那么就可能出现线程不安全的问题. 先看一段代码示例,定义一个对象 MyDomain1 public class MyDomain1 { private i ...