一、spring 概述

一、spring 概述
二、XML配置spring容器


spring 概述

1.1 简单案例

如下图所示,模拟一个简单的MVC项目,前端发起查询产品请求,后端通过查询数据库返回前端请求结果。

spring demo 结构图

1.1.1 没有使用spring案例

1.1.1.1 完成基本功能

Product商品类

src\main\java\org\ieening\entity\Product.java

package org.ieening.entity;

public class Product {
    private int id;
    private String name;

    public Product() {
        System.out.println("product init ... ");
    }
}

ProductService服务接口

src\main\java\org\ieening\service\ProductService.java

package org.ieening.service;

import org.ieening.entity.Product;

public interface ProductService {
    Product getProductById();
}

ProductServiceImpl商品服务实现类

package org.ieening.service.impl;

import org.ieening.dao.ProductDao;
import org.ieening.dao.impl.ProductDaoMysqlImpl;
import org.ieening.dao.impl.ProductDaoOracleImpl;
import org.ieening.entity.Product;
import org.ieening.service.ProductService;

public class ProductServiceImpl implements ProductService {
    // ProductDao productDao = new ProductDaoMysqlImpl();
    ProductDao productDao = new ProductDaoOracleImpl();

    public ProductDao getProductDao() {
        return productDao;
    }

    public void setProductDao(ProductDao productDao) {
        this.productDao = productDao;
    }

    @Override
    public Product getProductById() {
        System.out.println("");
        return productDao.selectProductById();
    }

}

商品数据Dao访问接口

src\main\java\org\ieening\dao\ProductDao.java

package org.ieening.dao;

import org.ieening.entity.Product;

public interface ProductDao {
    Product selectProductById();
}

ProductDaoMySQLImpl 使⽤MySQL数据库的数据访问实现类

src\main\java\org\ieening\dao\impl\ProductDaoMysqlImpl.java

package org.ieening.dao.impl;

import org.ieening.dao.ProductDao;
import org.ieening.entity.Product;

public class ProductDaoMysqlImpl implements ProductDao {

    @Override
    public Product selectProductById() {
        System.out.println("search Product in mysql database");
        return new Product();
    }

}

测试类

src\main\java\org\ieening\controller\TestProductServiceWithoutSpring.java

package org.ieening.controller;

import org.ieening.service.impl.ProductServiceImpl;

public class TestProductServiceWithoutSpring {
    public static void main(String[] args) {
        ProductServiceImpl productServiceImpl = new ProductServiceImpl();
        productServiceImpl.getProductById();
    }
}

运行测试类,得到结果:

search Product in mysql database
product init ... 
1.1.1.2 更换数据库

如果想要修改成使用Oracle数据库时,需要完成的步骤:

  1. 增加Oracle数据库ProductDao实现类;

    package org.ieening.dao.impl;
    
    import org.ieening.dao.ProductDao;
    import org.ieening.entity.Product;
    
    public class ProductDaoOracleImpl implements ProductDao {
    
        @Override
        public Product selectProductById() {
            System.out.println("search Product in oracle database");
            return new Product();
        }
    }
    
  2. 修改核心代码src\main\java\org\ieening\service\impl\ProductServiceImpl.java

使用一般方法完成的项目修改功能时,不仅需要增加新的功能,还需要修改原来的业务代码,这样做的风险是非常高的。

1.1.2 使用spring

修改上面的案例,使用spring。使用Spring需要导入相关的jar包,导入完成后,增加spring.xml文件。

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

    <bean class="org.ieening.entity.Product" id="product" />
    <bean class="org.ieening.dao.impl.ProductDaoMysqlImpl" id="productDaoMysqlImpl" />
    <bean class="org.ieening.dao.impl.ProductDaoOracleImpl" id="productDaoOracleImpl" />
    <bean class="org.ieening.service.impl.ProductServiceImpl" id="productService">
        <property name="productDao" ref="productDaoMysqlImpl"></property> <!-- 注释1-->
    </bean>
</beans>

修改测试代码:

package org.ieening.controller;

import org.ieening.service.impl.ProductServiceImpl;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestProductServiceWithSpring {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext(
                "spring.xml");
        ProductServiceImpl productService = (ProductServiceImpl) classPathXmlApplicationContext
                .getBean("productService");
        productService.getProductById();
        classPathXmlApplicationContext.close();
    }
}

spring框架的加持下,想要实现更换成Oracle数据库,只需要将注释1中ref属性修改为productDaoMysqlImpl即可。

测试代码 – helloworld

1.2 spring 框架介绍

整个Spring框架被分成多个模块,应⽤程序可以选择需要的部分。core是核⼼容器的模块,包括模块配置和依赖注⼊机制。Spring框架还为不同的应⽤程序体系结构提供了基础⽀持,包括消息传递,事务数据和持久化以及Web,还包括基于ServletSpring MVC Web框架,以及Spring WebFlux响应式Web框架。

spring framework

GroupIdArtifactId说明
org.springframeworkspring-beansBean支持,包含Groovy
org.springframeworkspring-aop基于代理的AOP支持
org.springframeworkspring-aspects基于AspectJ的切面
org.springframeworkspring-context应用上下文运行时,包括调度和远程抽象
org.springframeworkspring-context-support⽀持将常⻅的第三⽅类库集成到 Spring 应⽤上下 ⽂
org.springframeworkspring-core其他模块所依赖的核⼼模块
org.springframeworkspring-expressionSpring 表达式语⾔,SpEL
org.springframeworkspring-instrumentJVM 引导的仪表(监测器)代理
org.springframeworkspring-instrumenttomcatTomcat 的仪表(监测器)代理
org.springframeworkspring-jdbc⽀持包括数据源设置和 JDBC 访问⽀持
org.springframeworkspring-jms⽀持包括发送/接收JMS消息的助⼿类
org.springframeworkspring-messaging对消息架构和协议的⽀持
org.springframeworkspring-orm对象/关系映射,包括对 JPA 和 Hibernate 的⽀持
org.springframeworkspring-oxm对象/XML 映射(Object/XML Mapping,OXM)
org.springframeworkspring-test单元测试和集成测试⽀持组件
org.springframeworkspring-tx事务基础组件,包括对 DAO 的⽀持及 JCA 的集成
org.springframeworkspring-webweb⽀持包,包括客户端及web远程调⽤
org.springframeworkspring-webmvcREST web 服务及 web 应⽤的 MVC 实现
org.springframeworkspring-webmvc-portlet⽤于 Portlet 环境的MVC实现
org.springframeworkspring-websocketWebSocket 和 SockJS 实现,包括对 STOMP 的⽀ 持
org.springframeworkspring-jcljakarta Commons Logging ⽇志系统
  • 23
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值