0714框架课程作业

0714框架课程作业

一、作业1

1.创建项目,并导入spring相关的jar包

2.创建一个People类

package com.openlab.pojo;

public class People {
    private String name;
    private String words;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getWords() {
        return words;
    }

    public void setWords(String words) {
        this.words = words;
    }

    public void talk(){
        System.out.println( name + "说:\"" + words +"\"");
    }
}

3.创建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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">

    <bean id="people1" class="com.openlab.pojo.People">
        <property name="name" value="张嘎"></property>
        <property name="words" value="三天不打小鬼子,手都痒痒!"></property>
    </bean>

    <bean id="people2" class="com.openlab.pojo.People">
        <property name="name" value="Rod"></property>
        <property name="words" value="世界上有10种人,认识二进制的和不认识二进制的。"></property>
    </bean>
    
</beans>

4.测试

package com.openlab.test;

import com.openlab.pojo.People;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test01 {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        People people1 = (People) ac.getBean("people1");
        people1.talk();

        People people2 = (People) ac.getBean("people2");
        people2.talk();
    }
}

二、作业2

1.创建项目,并导入spring相关的jar包

2.创建ink包

创建ink接口

package com.openlab.ink;

public interface Ink {

    String getColor();
}

创建ColorfulInk类

package com.openlab.ink;

public class ColorfulInk implements Ink {
    @Override
    public String getColor() {
        return "彩色墨盒";
    }
}

创建GrayInk类

package com.openlab.ink;

public class GrayInk implements Ink {

    @Override
    public String getColor() {
        return "黑白墨盒";
    }
}

3.创建paper包

创建Paper接口

package com.openlab.paper;

public interface Paper {

    String getSize();
}

创建A4Paper类

package com.openlab.paper;

public class A4Paper implements Paper {
    @Override
    public String getSize() {
        return "A4";
    }
}

创建B5Paper类

package com.openlab.paper;

public class B5Paper implements Paper {
    @Override
    public String getSize() {
        return "B5";
    }
}

4.创建printer包

创建Printer类

package com.openlab.printer;

import com.openlab.ink.Ink;
import com.openlab.paper.Paper;

public class Printer {
    private Ink ink;
    private Paper paper;

    public Ink getInk() {
        return ink;
    }

    public void setInk(Ink ink) {
        this.ink = ink;
    }

    public Paper getPaper() {
        return paper;
    }

    public void setPaper(Paper paper) {
        this.paper = paper;
    }

    public void printInfo(){
        System.out.println("打印机使用"+ paper.getSize() + "大小的纸张和" + ink.getColor() + "打印了信息" );
    }
}

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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">

    <bean id="printer" class="com.openlab.printer.Printer">
        <property name="ink" ref="grayInk"></property>
        <property name="paper" ref="A4"></property>
    </bean>

    <bean id="colorInk" class="com.openlab.ink.ColorfulInk"></bean>

    <bean id="grayInk" class="com.openlab.ink.GrayInk"></bean>

    <bean id="A4" class="com.openlab.paper.A4Paper"></bean>

    <bean id="B5" class="com.openlab.paper.B5Paper"></bean>

</beans>

6.测试

package com.openlab.test;

import com.openlab.printer.Printer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test02 {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        Printer printer = (Printer) ac.getBean("printer");
        printer.printInfo();
    }
}

三、作业3

1.创建项目,并导入spring相关的jar包

2.创建pojo包

创建User类

package com.openlab.pojo;

public class User {

    private int id;
    private String username;
    private String password;
    private String address;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

3.创建dao包

创建UserDao类

package com.openlab.dao;

import com.openlab.pojo.User;

public class UserDao {

    public int insertUser(User user){
        System.out.println("向数据库中插入用户成功,用户名为"+user.getUsername() + ",用户地址为" + user.getAddress());
        return 1;
    }

    public int deleteUser(User user){
        System.out.println("删除了用户" + user.getUsername());
        return 1;
    }
}

4.创建service包

创建UserService接口

package com.openlab.service;

import com.openlab.pojo.User;

public interface UserService {

    int addUser(User user);

    int removeUser(User user);
}

5.创建service.impl包

创建UserServiceImpl类

package com.openlab.service.impl;

import com.openlab.dao.UserDao;
import com.openlab.pojo.User;
import com.openlab.service.UserService;

public class UserServiceImpl implements UserService {

    private UserDao userDao;

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public int addUser(User user) {
        int i = userDao.insertUser(user);
        return i;
    }

    @Override
    public int removeUser(User user) {
        int i = userDao.deleteUser(user);
        return i;
    }
}

6.创建logger包

创建UserServiceLogger类

package com.openlab.logger;


import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;

public class UserServiceLogger {

    private static final Logger log = Logger.getLogger(UserServiceLogger.class);

    public void beforePrintLogger(JoinPoint joinPoint){
        log.info("调用了" + joinPoint.getTarget() + "的" + joinPoint.getSignature().getName() + "方法,方法参数是"
        + joinPoint.getArgs());
    }

    public void afterReturningPrintLogger(JoinPoint joinPoint ,Object result){
        log.info("调用了" + joinPoint.getTarget() + "的" + joinPoint.getSignature().getName() + "方法,返回值是"
                + result);
    }
}

7.配置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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">

    <bean id="userDao" class="com.openlab.dao.UserDao"></bean>

    <bean id="userService" class="com.openlab.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao"></property>
    </bean>

    <bean id="userServiceLogger" class="com.openlab.logger.UserServiceLogger"></bean>

    <aop:config>
        <aop:pointcut id="addUserPointCut" expression="execution(* com.openlab.service.impl.UserServiceImpl.*(..))"></aop:pointcut>

        <aop:aspect ref="userServiceLogger">
            <aop:before method="beforePrintLogger" pointcut-ref="addUserPointCut"></aop:before>
            <aop:after-returning method="afterReturningPrintLogger" pointcut-ref="addUserPointCut" returning="result"></aop:after-returning>
        </aop:aspect>
    </aop:config>

</beans>

8.测试

package com.openlab.test;

import com.openlab.pojo.User;
import com.openlab.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test03 {

    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = (UserService) ac.getBean("userService");
        User user = new User();
        user.setId(1);
        user.setUsername("zhangsan");
        user.setPassword("111");
        user.setAddress("China");
        userService.addUser(user);
        userService.removeUser(user);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,放心下载使用!有问题及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我! 毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值