学习之路之springmvc设计模式以及xml注入.....

学习设计模式springmvc

首先创建entity,dao,service,controller

架构图片:

在这里插入图片描述
1.entity,创建User实体类

 

## **通过set方法注入**

package cn.run.eneity;

public class User {
    private Integer id;
    private  String name;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

2.创建UserDao接口以及UserDaoImpl实现

UserDao

package cn.run.dao;

import cn.run.eneity.User;

public interface UserDao {
     void addUser(User user);
}

UserDaoImpl

package cn.run.dao;

import cn.run.eneity.User;

public class UserDaoImpl implements UserDao {

    @Override
    public void addUser(User user) {
        System.out.println("增加了"+user);
    }

}

3创建UserService和UserviceImpl

UserService

package cn.run.service;

import cn.run.eneity.User;

public interface UserService {
    void addUser(User user);
}

UserServiceImpl

package cn.run.service.impl;

import cn.run.dao.UserDao;
import cn.run.eneity.User;
import cn.run.service.UserService;

public class UserServiceImpl implements UserService {
    private UserDao userDao;//基于spring的Dao 面向接口

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

    @Override
    public void addUser(User user) {
        userDao.addUser(user);
    }
}

创建UserController

package cn.run.controller;

import cn.run.eneity.User;
import cn.run.service.UserService;

public class UserContoller {
    //spring容器注入
    private UserService userService;
    // spring容器注入user
    private User user;

    public void setUser(User user) {
        this.user = user;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    public void addUser(){
        userService.addUser(user);
    }
}

创建beanTwo.xml配置文件,给spring管理我们的Bean和interface

beanTwo:

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

<!--构建user对象 -->
    <bean id="user" class="cn.run.eneity.User">
        <property name="id" value="100"></property>
        <property name="name" value="springmvc设计模式"></property>
    </bean>
    <!--构建Dao对象
    根据面向接口编程
    id是接口
    class是路径-->
    <bean id="userDao" class="cn.run.dao.UserDaoImpl"/>

    <!--构建Service-->
    <bean id="userService" class="cn.run.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao"></property>
    </bean>

    <!--构建controller层-->
    <bean id="userController" class="cn.run.controller.UserContoller">
        <property name="user" ref="user"></property>
        <property name="userService" ref="userService"></property>
    </bean>




</beans>

创建测试类:

import cn.run.controller.UserContoller;
import cn.run.eneity.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Calendar;

public class MyTest {
    

    @Test
    public void TestSpringAop(){
      ApplicationContext context=  new ClassPathXmlApplicationContext("beanTwo.xml");
          UserContoller userContoller=(UserContoller) context.getBean("userController");
          userContoller.addUser();
        System.out.println("rush b");
    }
}

结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值