Rhythmli's blog Rhythmli's blog
首页
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • HTML
  • CSS
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Rhythmli

知识就是财富
首页
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • HTML
  • CSS
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 技术文档

  • GitHub技巧

  • Nodejs

  • 博客搭建

  • CSDN迁移

    • Spring IOC
    • Ngnix 阿里云
    • 最长回文子串
    • 面试题xuexixiexue
    • 哈希集合和哈希映射的简单设计
    • leetcode2021.11.03
    • Leetcode2021.11.2
    • JUC学习
    • 869. 重新排序得到 2 的幂
    • Java自动装箱拆箱
    • 55. 跳跃游戏
    • 剑指 Offer II 085. 生成匹配的括号
    • 300. 最长递增子序列
    • Java并发编程之美 01
    • 134. 加油站
    • 139. 单词拆分
    • 岛屿类问题题解
    • 138. 复制带随机指针的链表
    • 347. 前 K 个高频元素
    • 剑指 Offer II 026. 重排链表
    • 剑指 Offer II 025. 链表中的两数相加
    • 剑指 Offer II 014. 字符串中的变位词
    • 剑指 Offer II 010. 和为 k 的子数组
    • 剑指 Offer II 009. 乘积小于 K 的子数组
    • 剑指 Offer II 008. 和大于等于 target 的最短子数组
    • 剑指 Offer II 007. 数组中和为 0 的三个数
    • 剑指 Offer II 006. 排序数组中两个数字之和
    • 剑指 Offer II 002. 二进制加法
    • 129. 求根节点到叶节点数字之和
    • 113.路径总和 II
    • leetcode18. 四数之和
    • 编译OpenCV 以及 openc_contrib 提示缺少boostdesc_bgm.i文件出错的解决
    • fork()浅学习
    • SSM 增删改查
    • springmvc helloworld
    • Spring01 hello实验
    • 树的DFS和BFS
    • leetcode——二分法
    • Halo博客搭建
    • 计算机视觉领域的一些牛人博客,超有实力的研究机构等的网站链接---转载
    • opencv+python+OpenPose姿态实时识别
    • 03.KNN算法 李航统计学习方法
    • 02.感知机 李航统计学习方法
    • 01.最小二乘法拟合 李航统计学习方法
    • Pycharm atplotlib.pyplot图像不显示解决方法
    • 论文阅读01 SVM+kNN图像分类
    • Java简单实现计算器——用数组实现栈
    • 【剑指Offer3】无重复字符的最长子串
    • 【剑指Offer5】最长回文字符串
    • TF-IDF求取文本相似度
    • JAVA_day02
    • JAVA_day01
    • 递归产生回文数
    • 中国象棋QT登录注册以及悔棋功能
    • STM32F4学习笔记(基础介绍篇)
    • leetcode_04 递归,回溯与分治
    • leetcode03_贪心算法
    • leetcode01--链表
    • leetcode_02栈
    • Docker学习入门
    • C/C++编译与链接 程序员的自我修养:链接 装载和库
    • Nginx简单学习
    • JAVA网络编程
    • JVM初步学习
    • Spring简单学习
      • 标准项目格式
      • 设计模式中的几个原则
      • Redis和IDEA简单创建及增删改查
      • Mybatis快速入门01
      • Redis全程学习笔记(附带学习的视频教程)
      • QT入门学习中最基础的那些事儿
      • QT中文输出错误问题:C2001
      • OOP:面向对象编程
      • LINUX常用命令集合(待续)
      • 《C和指针》简单学习笔记
      • 二叉树,栈存储及遍历小程序
      • 数据结构简单学习笔记
      • 扑克牌
      • C++动态内存和智能指针
      • 设计模式之简单工厂模式
      • leetcode_01数组
      • 嵌入式Linux移植应用
      • LINUX 进程与线程 信号量 通信
    • 技术
    • CSDN迁移
    梁山话事人
    最新推荐文章2026-08-07
    目录

    Spring简单学习

    原文链接:https://blog.csdn.net/qq_39355828/article/details/112235749 (opens new window) B站搜索 狂神说JAVA

    # 1.DAO层

    全称为data access object,为了建立一个健壮的J2EE应用,应该将所有对数据源的访问操作抽象封装在一个公共API中。用程序设计的语言来说,就是建立一个接口,接口中定义了此应用程序中将会用到的所有事务方法。在这个应用程序中,当需要和数据源进行交互的时候则使用这个接口,并且编写一个单独的类来实现这个接口在逻辑上对应这个特定的数据存储。

    # 2.service层

    service层叫服务层,被称为服务,肯定是相比之下比较高层次的一层结构,相当于将几种操作封装起来。用户只和service交互,而DAO层对用户不可见。同时减少了耦合。

    # 3.IDEA中建立过程

    1.建立maven项目,

    2.导入依赖,刷新maven

    3.删除src文件,新建module,在java中新建 com.name.UserDao 和接口实现类UserDaoImpl

    4.新建service层, 新建com.name.service,建立Userservice类接口实现类UserserviceImpl接口实现类

    5.新建test进行测试。

    # 4.代码及结构01

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-79BZZChm-1609830120623)(标准项目格式.assets/1609765525939.png)]

    //UserDao
    package com.li.dao;
    
    public interface UserDao {
        void getUser();
    }
    //实现类
    package com.li.dao;
    
    public class UserDaoImpl implements UserDao
    {
        public void getUser()
        {
            System.out.println("获取用户数据");
        }
    }
    //service
    package com.li.service;
    
    public interface UserService {
        void getUser();
    }
    //service实现类
    package com.li.service;
    import com.li.dao.UserDao;
    import com.li.dao.UserDaoImpl;
    public class UserServiceImpl implements UserService
    {
        private UserDao userDao=new UserDao();
    
        public void getUser()
        {
            userDao.getUser();
    
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36

    缺点:上述方式改动太多,较为繁琐,耦合性还是太强

    # 5.利用IOC进行改进

    程序主动创建对象,用set注入之后,程序不再具有主动性,变成了被动的接受对象。从本质上解决问题,系统耦合性降低,专注业务实现,。实现控制反转

    //修改service实现类如下
    package com.li.service;
    import com.li.dao.UserDao;
    import com.li.dao.UserDaoImpl;
    import com.li.dao.UserDaoMysql;
    
    public class UserServiceImpl implements UserService
    {
        private UserDao userDao;
        public void setUserDao(UserDao userDao)
        {
            this.userDao=userDao;
        }
        public void getUser()
        {
            userDao.getUser();
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    import com.li.dao.UserDaoImpl;
    import com.li.dao.UserDaoMysql;
    import com.li.service.UserService;
    import com.li.service.UserServiceImpl;
    
    public class test
    {
        public static void main(String[] args) {
         UserService userService=new UserServiceImpl();
            ((UserServiceImpl)userService).setUserDao(new UserDaoMysql());
            userService.getUser();
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13

    这是IoC(控制反转)的原型,反转(理解):主动权交给了用户

    在这里插入图片描述

    6.利用Spring进行修改

    写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:util="http://www.springframework.org/schema/util"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
            <!--相当于new一个对象-->
           <bean id="DaoImpl" class="com.li.dao.UserDaoImpl"/>
           <bean id="MysqlImpl" class="com.li.dao.UserDaoMysql"/>
            <!--相当于new一个对象 ref表明要引用某一个对象-->
           <bean id="UserServiceImpl" class="com.li.service.UserServiceImpl">
                   <property name="userDao" ref="DaoImpl"/>
           </bean>
    </beans>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12

    修改test IOC默认使用无参构造,可以使用无参构造

    public class test
    {
        public static void main(String[] args) {
            //获取上下文
            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
            UserServiceImpl userService= (UserServiceImpl)context.getBean("UserServiceImpl");
            userService.getUser();
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9

    # 6.Spring配置

    # 1Bean配置

    <!--id:bean的唯一标识符,也就是相当于我们学的对象名
    class:bean对象所对应的会限定名:包名+类型
    name:也是别名,而且name可以同时取多个别名 -->
    <bean id="user" class="pojo.User" name="u1 u2,u3;u4">
        <property name="name" value="iii"/>
    </bean>
    <!-- 使用时
    	User user2 = (User) context.getBean("u1");
    -->
    
    1
    2
    3
    4
    5
    6
    7
    8
    9

    # 2 import

    import一般用于团队开发使用,它可以将多个配置文件,导入合并为一个
    用import将所有人的beans.xml合并为一个总的!
    
    - 张三(beans.xm1)
    
    - 李四(beans2.xm1)
    
    - 王五(beans3.xm1)
    
    - applicationContext.xml
    
      ```xml
      <import resource="beans.xm1"/>
      <import resource="beans2.xml"/>
      <import resource="beans3.xm1"/>
      ```
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16

    # 7.依赖注入

    # 7.1 set注入

    student类

    package com.li.pojo;
    
    import java.util.*;
    
    public class student {
        //别忘了写get和set方法(用lombok注解也行)
        private String name;
        private Address address;
    
        private String[] books;
        private List<String> hobbies;
    
        private Map<String, String> card;
        private Set<String> game;
    
        private Properties infor;
        private String wife;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Address getAddress() {
            return address;
        }
    
        public void setAddress(Address address) {
            this.address = address;
        }
    
        public String[] getBooks() {
            return books;
        }
    
        public void setBooks(String[] books) {
            this.books = books;
        }
    
        public List<String> getHobbies() {
            return hobbies;
        }
    
        public void setHobbies(List<String> hobbies) {
            this.hobbies = hobbies;
        }
    
        public Map<String, String> getCard() {
            return card;
        }
    
        public void setCard(Map<String, String> card) {
            this.card = card;
        }
    
        public Set<String> getGame() {
            return game;
        }
    
        public void setGame(Set<String> game) {
            this.game = game;
        }
    
        public Properties getInfor() {
            return infor;
        }
    
        public void setInfor(Properties infor) {
            this.infor = infor;
        }
    
        public String getWife() {
            return wife;
        }
    
        public void setWife(String wife) {
            this.wife = wife;
        }
    
        @Override
        public String toString() {
            return "Student{" +"\n"+
                    "name='" + name + '\'' +"\n"+
                    ", address=" + address.toString() +"\n"+
                    ", books=" + Arrays.toString(books) +"\n"+
                    ", hobbies=" + hobbies +"\n"+
                    ", card=" + card +"\n"+
                    ", game=" + game +"\n"+
                    ", infor=" + infor +"\n"+
                    ", wife='" + wife + '\'' +"\n"+
                    '}';
        }
    
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97

    Address类

    package com.li.pojo;
    
    import java.util.*;
    
    public class student {
        //别忘了写get和set方法(用lombok注解也行)
        private String name;
        private Address address;
    
        private String[] books;
        private List<String> hobbies;
    
        private Map<String, String> card;
        private Set<String> game;
    
        private Properties infor;
        private String wife;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Address getAddress() {
            return address;
        }
    
        public void setAddress(Address address) {
            this.address = address;
        }
    
        public String[] getBooks() {
            return books;
        }
    
        public void setBooks(String[] books) {
            this.books = books;
        }
    
        public List<String> getHobbies() {
            return hobbies;
        }
    
        public void setHobbies(List<String> hobbies) {
            this.hobbies = hobbies;
        }
    
        public Map<String, String> getCard() {
            return card;
        }
    
        public void setCard(Map<String, String> card) {
            this.card = card;
        }
    
        public Set<String> getGame() {
            return game;
        }
    
        public void setGame(Set<String> game) {
            this.game = game;
        }
    
        public Properties getInfor() {
            return infor;
        }
    
        public void setInfor(Properties infor) {
            this.infor = infor;
        }
    
        public String getWife() {
            return wife;
        }
    
        public void setWife(String wife) {
            this.wife = wife;
        }
    
        @Override
        public String toString() {
            return "Student{" +"\n"+
                    "name='" + name + '\'' +"\n"+
                    ", address=" + address.toString() +"\n"+
                    ", books=" + Arrays.toString(books) +"\n"+
                    ", hobbies=" + hobbies +"\n"+
                    ", card=" + card +"\n"+
                    ", game=" + game +"\n"+
                    ", infor=" + infor +"\n"+
                    ", wife='" + wife + '\'' +"\n"+
                    '}';
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96

    Beans.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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
            <bean id="address" class="com.li.pojo.Address">
            <property name="address" value="address你好" />
            </bean>
        <bean id="Student" class="com.li.pojo.student">
            <!--第一种,普通值注入 -->
            <property name="name" value="name你好" />
            <!--第二种,ref注入 -->
            <property name="address" ref="address" />
    
            <!--数组注入 -->
            <property name="books">
                <array>
                    <value>三国</value>
                    <value>西游</value>
                    <value>水浒</value>
                </array>
            </property>
    
            <!--list列表注入 -->
            <property name="hobbies">
                <list>
                    <value>唱</value>
                    <value>跳</value>
                    <value>rap</value>
                    <value>篮球</value>
                </list>
            </property>
    
            <!--map键值对注入 -->
            <property name="card">
                <map>
                    <entry key="username" value="root" />
                    <entry key="password" value="root" />
                </map>
            </property>
    
            <!--set(可去重)注入 -->
            <property name="game">
                <set>
                    <value>wangzhe</value>
                    <value>lol</value>
                    <value>galname</value>
                </set>
            </property>
    
            <!--空指针null注入 -->
            <property name="wife">
                <null></null>
            </property>
    
            <!--properties常量注入 -->
            <property name="infor">
                <props>
                    <prop key="id">20200802</prop>
                    <prop key="name">cbh</prop>
                </props>
            </property>
        </bean>
    </beans>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63

    使用p和c命名空间需要导入xml约束

    xmlns:p=“http://www.springframework.org/schema/p”

    xmlns:c=“http://www.springframework.org/schema/c”

    <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"
           xmlns:c="http://www.springframework.org/schema/c"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <!--p命名空间注入/set注入,可以直接注入属性的值-》property-->
        <bean id="user" class="com.li.pojo.User" p:name="020" p:id="20" >
        </bean>
    
        <!--c命名空间,通过构造器注入,需要写入有参和无参构造方法-》construct-args-->
        <bean id="user2" class="com.li.pojo.User" c:name="123" c:id="22"></bean>
    </beans>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14

    # 8.Bean的自动装配

    • 自动装配是Spring满足bean依赖的一种方式
    • Spring会在上下文自动寻找,并自动给bean装配属性
    1. 在xml中显示配置
    2. 在java中显示配置
    3. 隐式的自动装配bean

    # 9.AOP

    提供声明式事务,允许用户自定义切面

    • 横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志,安全,缓存,事务等等…

    • 切面(Aspect):横切关注点 被模块化的特殊对象。即,它是一个类。(Log类)

    • 通知(Advice):切面必须要完成的工作。即,它是类中的一个方法。(Log类中的方法)

    • 目标(Target):被通知对象。(生成的代理类)

    • 代理(Proxy):向目标对象应用通知之后创建的对象。(生成的代理类)

    • 切入点(PointCut):切面通知执行的”地点”的定义。(最后两点:在哪个地方执行,比如:method.invoke())

    • 连接点(JointPoint):与切入点匹配的执行点。

      切入点可以理解为被增强的方法 要扩展功能的那个方法

    编辑 (opens new window)
    #JAVA#订阅专栏#查看详情
    上次更新: 2026/08/11, 13:36:18
    JVM初步学习
    标准项目格式

    ← JVM初步学习 标准项目格式→

    最近更新
    01
    Spring IOC
    03-31
    02
    Git修改分支名
    08-11
    03
    CSS给table的tbody添加滚动条
    06-29
    更多文章>
    Theme by Vdoing | Copyright © 2019-2026 Evan Xu | MIT License
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式