后端方向进阶——Spring框架

各位帅哥美女,编辑不易,动动发财小手,来个三连加关注,后续会有更加优秀的推文推出~

本章节目标:掌握IOC,AOP思想

目录

1. 介绍Spring框架

2. IoC(Inversion of Control)容器

2.1 创建一个简单的Bean

2.2 配置IoC容器

2.3 使用IoC容器

3. AOP(Aspect-Oriented Programming)

3.1 创建一个切面

3.2 配置AOP

4. 数据访问与事务管理

4.1 数据访问

4.1.1 创建数据访问对象(DAO)

4.1.2 实现数据访问对象

4.1.3 配置数据源和DAO

4.2 事务管理

4.2.1 创建事务管理器

4.2.2 配置事务管理器

5. Spring MVC

5.1 创建控制器

5.2 配置Spring MVC

5.3 创建视图

5.4 配置Web.xml

总述——Spring框架的基本功能


1. 介绍Spring框架

Spring是一个全面的企业应用开发框架,提供了从数据访问到消息传递的全栈式解决方案。它以轻量、松耦合、可扩展、面向切面编程(AOP)等特性而闻名。

2. IoC(Inversion of Control)容器

IoC容器是Spring框架的核心,负责管理和组织应用中的组件(Bean)。通过IoC容器,实现了依赖注入(DI)。

2.1 创建一个简单的Bean

public class HelloWorld {
    private String message;

    public void setMessage(String message) {
        this.message = message;
    }

    public void printMessage() {
        System.out.println("Message: " + message);
    }
}

2.2 配置IoC容器

<!-- applicationContext.xml -->
<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="helloWorld" class="com.example.HelloWorld">
        <property name="message" value="Hello, Spring!"/>
    </bean>

</beans>

2.3 使用IoC容器

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
        obj.printMessage();
    }
}

3. AOP(Aspect-Oriented Programming)

AOP是一种编程范式,通过在程序中插入特定的代码片段(切面),实现横切关注点的分离。

3.1 创建一个切面

public class LoggingAspect {
    public void beforeAdvice() {
        System.out.println("Before advice: Logging...");
    }
}

3.2 配置AOP

<!-- applicationContext.xml -->
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="helloWorld" class="com.example.HelloWorld">
        <property name="message" value="Hello, Spring!"/>
    </bean>

    <bean id="loggingAspect" class="com.example.LoggingAspect"/>

    <aop:config>
        <aop:aspect ref="loggingAspect">
            <aop:before method="beforeAdvice" pointcut="execution(* com.example.HelloWorld.printMessage())"/>
        </aop:aspect>
    </aop:config>

</beans>

4. 数据访问与事务管理

Spring提供了对各种数据访问技术的支持,并包含了事务管理的功能。

4.1 数据访问

4.1.1 创建数据访问对象(DAO)
public interface StudentDAO {
    void addStudent(Student student);
    List<Student> getAllStudents();
}
4.1.2 实现数据访问对象
public class JdbcStudentDAO implements StudentDAO {
    private DataSource dataSource;

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    public void addStudent(Student student) {
        // 实现添加学生的数据库操作
    }

    @Override
    public List<Student> getAllStudents() {
        // 实现获取所有学生的数据库操作
    }
}
4.1.3 配置数据源和DAO
<!-- applicationContext.xml -->
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mydatabase"/>
        <property name="username" value="username"/>
        <property name="password" value="password"/>
    </bean>

    <bean id="jdbcStudentDAO" class="com.example.JdbcStudentDAO">
        <property name="dataSource" ref="dataSource"/>
    </bean>

</beans>

4.2 事务管理

4.2.1 创建事务管理器
public class StudentTransactionManager {
    private PlatformTransactionManager transactionManager;

    public void setTransactionManager(PlatformTransactionManager transactionManager) {
        this.transactionManager = transactionManager;
    }

    @Transactional
    public void performTransaction() {
        // 执行需要事务管理的操作
    }
}
4.2.2 配置事务管理器
<!-- applicationContext.xml -->
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:annotation-config/>
    <tx:annotation-driven/>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <bean id="studentTransactionManager" class="com.example.StudentTransactionManager">
        <property name="transactionManager" ref="transactionManager"/>
    </bean>

</beans>

5. Spring MVC

Spring MVC 是一种基于 MVC 模式的 Web 框架,用于构建 Web 应用程序。

5.1 创建控制器

@Controller
public class HelloWorldController {
    @RequestMapping("/hello")
    public String hello(Model model) {
        model.addAttribute("message", "Hello, Spring MVC");
        return "hello";
    }
}

5.2 配置Spring MVC

<!-- applicationContext.xml -->
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:annotation-config/>
    <mvc:annotation-driven/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <bean class="com.example.HelloWorldController"/>

</beans>

5.3 创建视图

在 "/WEB-INF/views/" 目录下创建一个名为 "hello.jsp" 的视图文件。

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Hello Spring MVC</title>
</head>
<body>
    <h2>${message}</h2>
</body>
</html>

5.4 配置Web.xml

<!-- web.xml -->
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
    http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1">

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

总述——Spring框架的基本功能

Spring框架是一个全面的企业应用开发框架,提供了多种功能和特性,使得Spring框架成为一个强大且灵活的框架,适用于各种规模和类型的企业应用开发,主要围绕以下几个核心模块展开:

·IoC容器: IoC是Spring的核心概念之一。IoC容器负责管理和组织应用中的组件(Bean),并通过依赖注入(DI)将它们组装起来。这使得应用的组件不需要自行管理依赖关系,而是由容器来负责。IoC容器的常见实现有ApplicationContext

·AOP: AOP是一种编程范式,通过在程序中插入特定的代码片段(切面),实现横切关注点的分离。Spring的AOP模块允许开发者在应用中声明横切关注点,并将其与业务逻辑分离,提高代码的模块化和可维护性。

·数据访问: Spring框架提供了对各种数据访问技术的支持,包括JDBC、ORM(例如Hibernate、MyBatis)等。Spring的数据访问模块简化了数据库操作,提供了声明式事务管理、异常处理等功能。

·事务管理: Spring框架支持声明式事务管理,通过使用注解或XML配置,可以轻松地管理事务。这包括事务的开启、提交、回滚等操作,确保在数据库操作中的一致性和可靠性。

·Spring MVC: Spring MVC是基于MVC(Model-View-Controller)模式的Web框架,用于构建Web应用程序。它提供了强大的请求处理机制、视图解析、数据绑定等功能,使得开发者能够更轻松地构建灵活、可维护的Web应用。

·Spring Boot: Spring Boot是Spring框架的一个子项目,旨在简化Spring应用程序的开发和部署。通过约定大于配置的原则,Spring Boot可以让开发者更轻松地创建独立运行、自包含的、生产级别的Spring应用。

·面向切面编程(AspectJ): Spring集成了AspectJ,提供了更强大的AOP功能。AspectJ是AOP领域的一种成熟的解决方案,Spring框架允许你使用AspectJ的注解或XML配置来定义切面。

·Spring Security: Spring Security是Spring框架的安全性解决方案,用于处理认证(Authentication)和授权(Authorization)等安全相关的功能,使得开发者能够更容易地保护应用程序。

·Spring Cloud: 面向微服务架构的Spring Cloud为开发者提供了一套完整的工具和框架,用于构建分布式系统。它包括服务发现、负载均衡、分布式配置、断路器等组件,帮助开发者构建和部署云原生应用。

以上是Spring框架的一些基本用法,包括IoC容器的配置和使用、AOP的实现、数据访问与事务管理、以及Spring MVC的简单搭建。这些示例代码能够帮助你理解Spring框架的核心概念和基本用法。当然,Spring框架的功能远不止这些,你可以根据具体需求深入学习和实践。

最后的最后,别忘了关注我哟~

  • 25
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小羊一定要努力变强

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值