Spring基础案例

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

提示:这里可以添加本文要记录的大概内容:

例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。


提示:以下是本篇文章正文内容,下面案例可供参考

一、 Spring的概念

  • Spring框架是一个轻量级开放源代码JavaEE应用程序一站式框架,其主要核心为IOC和AOP
    • 轻量级开放源代码
      • 开源免费,轻量级框架值得是不依赖其他的框架能独立使用 (EJB)
    • 一站式
      • 表现层 Servlet SpringMVC
      • 业务层 Service Spring在整个web项目中起到承上启下的作用
      • 数据层 JDBC SpringDataJpa
    • IOC和AOP
      • IOC(控制反转) 以前创建对象 UserDao user = new UserDao();
        • 将对象的创建交个Spring框架(容器)
        • DI(依赖注入)
      • AOP(面向切面)
        • 在不修改原代码的情况,对象功能进行增强
        • 底层原理(动态代理)

二、 Spring 作用

  • Spring是一个项目管理框架,同时也是一套Java EE解决方案。
  • Spring是众多优秀设计模式的组合(工厂、单例、代理、适配器、包装器、观察者、模板、策略)。
  • Spring并未替代现有框架产品,而是将众多框架进行有机整合,简化企业级开发,俗称"胶水框架"。

三、Spring的组成

Spring架构由诸多模块组成,可分类为

  • 核心技术:依赖注入,事件,资源,i18n,验证,数据绑定,类型转换,SpEL,AOP。
  • 测试:模拟对象,TestContext框架,Spring MVC测试,WebTestClient。
  • 数据访问:事务,DAO支持,JDBC,ORM,封送XML。
  • Spring MVC和 Spring WebFlux Web框架。
  • 集成:远程处理,JMS,JCA,JMX,电子邮件,任务,调度,缓存。
  • 语言:Kotlin,Groovy,动态语言。
    请添加图片描述

四、Spring入门案例

1.引入spring依赖

代码如下(示例):

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>Spring</artifactId>
        <groupId>com.jw</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>Spring01</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <!--如果只做Spring的基础开发只需要导入spring-context依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.27</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

2.编写spring的配置文件

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

    <!--
        配置UserDao的信息
                id:对象的唯一标识
                class:对象的全限定名
     -->
    <bean id="userService" class="com.jw.service.impl.UserServiceImpl"></bean>

</beans>

该处使用的url网络请求的数据。


3.测试

创建Spring容器,获取对象实例


import com.jw.service.UserService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

    /**
     * Spring的开发步骤
     *   1、引入Spring依赖
     *   2、编写spring的配置文件(配置bean的信息)
     *   3、创建spring容器获取对象
     */
public class TestSpring01 {
    @Test
    public void testSpring01(){
        //创建ClassPathXmlApplicationContext对象并获取SpringIOC容器
        final ClassPathXmlApplicationContext ac
                = new ClassPathXmlApplicationContext("applicationContext.xml");
        //从IOC容器中获取对象
        final UserService userService = (UserService) ac.getBean("userService");
        //System.out.println(userService);
        userService.addUser();
    }
}

4.补充

在这里插入图片描述
UserService.java

package com.jw.service;

/**
 * 用户服务接口
 *
 * @author jw
 */
public interface UserService {

    /**
     * 添加用户
     */
    void addUser();

}

UserServiceImpl.java

import com.jw.service.UserService;

/**
 * 用户服务实现类
 *
 * @author jw
 */
public class UserServiceImpl implements UserService {

    /**
     * 添加用户
     */
    @Override
    public void addUser() {
        System.out.println("add user");
    }

}
  • 19
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值