spring 概述
1.1 简单案例
如下图所示,模拟一个简单的MVC
项目,前端发起查询产品请求,后端通过查询数据库返回前端请求结果。
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
数据库时,需要完成的步骤:
-
增加
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(); } }
-
修改核心代码
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
即可。
1.2 spring 框架介绍
整个Spring
框架被分成多个模块,应⽤程序可以选择需要的部分。core
是核⼼容器的模块,包括模块配置和依赖注⼊机制。Spring
框架还为不同的应⽤程序体系结构提供了基础⽀持,包括消息传递,事务数据和持久化以及Web
,还包括基于Servlet
的Spring MVC Web
框架,以及Spring WebFlux
响应式Web
框架。
GroupId | ArtifactId | 说明 |
---|---|---|
org.springframework | spring-beans | Bean支持,包含Groovy |
org.springframework | spring-aop | 基于代理的AOP支持 |
org.springframework | spring-aspects | 基于AspectJ的切面 |
org.springframework | spring-context | 应用上下文运行时,包括调度和远程抽象 |
org.springframework | spring-context-support | ⽀持将常⻅的第三⽅类库集成到 Spring 应⽤上下 ⽂ |
org.springframework | spring-core | 其他模块所依赖的核⼼模块 |
org.springframework | spring-expression | Spring 表达式语⾔,SpEL |
org.springframework | spring-instrument | JVM 引导的仪表(监测器)代理 |
org.springframework | spring-instrumenttomcat | Tomcat 的仪表(监测器)代理 |
org.springframework | spring-jdbc | ⽀持包括数据源设置和 JDBC 访问⽀持 |
org.springframework | spring-jms | ⽀持包括发送/接收JMS消息的助⼿类 |
org.springframework | spring-messaging | 对消息架构和协议的⽀持 |
org.springframework | spring-orm | 对象/关系映射,包括对 JPA 和 Hibernate 的⽀持 |
org.springframework | spring-oxm | 对象/XML 映射(Object/XML Mapping,OXM) |
org.springframework | spring-test | 单元测试和集成测试⽀持组件 |
org.springframework | spring-tx | 事务基础组件,包括对 DAO 的⽀持及 JCA 的集成 |
org.springframework | spring-web | web⽀持包,包括客户端及web远程调⽤ |
org.springframework | spring-webmvc | REST web 服务及 web 应⽤的 MVC 实现 |
org.springframework | spring-webmvc-portlet | ⽤于 Portlet 环境的MVC实现 |
org.springframework | spring-websocket | WebSocket 和 SockJS 实现,包括对 STOMP 的⽀ 持 |
org.springframework | spring-jcl | jakarta Commons Logging ⽇志系统 |