• [spring]spring管理的入门项目快速搭建


    1.spring简介

    • Spring框架是一个开源的应用程序框架,是针对bean的生命周期进行管理的轻量级容器。

    • Spring解决了开发者在J2EE开发中遇到的许多常见的问题,提供了功能强大IOC、AOP及Web MVC等功能。

    • Spring可以单独应用于构筑应用程序,也可以和Struts、Webwork、Tapestry等众多Web框架组合使用,并且可以与 Swing等桌面应用程序AP组合。

    • Spring不仅仅能应用于J2EE应用程序之中,也可以应用于桌面应用程序以及小应用程序之中。

    • Spring框架主要由七部分组成,分别是 Spring Core、 Spring AOP、 Spring ORM、 Spring DAO、Spring Context、 Spring Web和 Spring Web MVC。

    官方文档地址:

    https://docs.spring.io/spring-framework/docs/4.3.9.RELEASE/spring-framework-reference/

    https://docs.spring.io/spring-framework/docs/5.2.0.RELEASE/spring-framework-reference/core.html#spring-core

    中文

    https://www.docs4dev.com/docs/zh/spring-framework/5.1.3.RELEASE/reference/

    优点:

    • 开源免费
    • 轻量级的非入侵式的
    • 控制反转(IOC),面向切面编程(aop)
    • 支持事务处理

    使用spring的jar包支持:

    
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-webartifactId>
        <version>5.3.22version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    七大模块:

    image-20220723204456031

    弊端:发展了太久后,配置越来越多,人称“配置地狱”

    2.IOC理论推导

    在我们之前的业务中,用户的需求可能会影响程序的代码,可能需要修改代码,如果程序的代码量十分大,修改一次的成本十分的昂贵!

    原来的方式:

    private UserMapper usermapper=new UserMapperImpl();
    
    • 1

    现在将对象的传递由new变成set动态注入

    private UserMapper userMapper;
    public void setUserMapper(UserMapper userMapper){
        this.userMapper=userMapper;
    }
    
    • 1
    • 2
    • 3
    • 4

    原来是程序控制的,现在变成用户控制了。

    3.一个spring项目的快速搭建

    (1)写一个实体类

    package com.pojo;
    
    /**
     * @author panglili
     * @create 2022-07-23-21:40
     */
    public class HelloSpring {
        private String name;
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getName() {
            return name;
        }
    
        @Override
        public String toString() {
            return name;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    (2)将实体类配置在spring容器

    
    <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="helloSpring" class="com.pojo.HelloSpring">
    <property name="name"  value="spring">property>
    bean>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    (3)测试

    import com.pojo.HelloSpring;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * @author panglili
     * @create 2022-07-23-21:43
     */
    public class MyTest {
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext( "application.xml");
            HelloSpring hello =(HelloSpring) context.getBean("helloSpring");
            System.out.println(hello.toString());
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
  • 相关阅读:
    考前必看科目一交警手势图解_准橙考试网
    HyperBDR云容灾深度解析五:全程助力业务级容灾,实现分钟级RTO业务恢复
    【部署篇】Docker配置MySQL容器+远程连接
    DcatAdmin使用模版文件时模板标签不生效
    工业光网助力工业企业数字化转型发展
    倒数 3 天|RocketMQ 能力全景图即将发布,定义下一代消息队列未来方向
    22-08-06 西安 EasyExcel实现字典表导入、导出
    常用的日期格式整理
    MySQL进阶-sql执行时间的查询
    springboot+vue+elementUI基于SpringBoot的冬奥会科普平台#毕业设计
  • 原文地址:https://blog.csdn.net/weixin_48595687/article/details/126000121