【JAVA基础篇教学】第十五篇:Java中Spring详解说明

简介: 【JAVA基础篇教学】第十五篇:Java中Spring详解说明

博主打算从0-1讲解下java基础教学,今天教学第十五篇:Java中Spring详解说明。


Spring 框架是一个广泛应用于 Java 开发的轻量级、全栈式的企业应用开发框架,它提供了众多功能强大的模块,用于简化企业级应用程序的开发。下面详细说明 Spring 框架的各种模块以及示例代码。


基础版简单介绍,进阶篇会详细一一解释说明!

一、Spring 核心容器(Spring Core Container)

Spring 核心容器是 Spring 框架的核心部分,它包含了以下两个重要的功能:


  1. 依赖注入(Dependency Injection,DI):通过依赖注入,Spring 容器负责将对象之间的依赖关系注入到对象中,降低了组件之间的耦合性。
  2. 面向切面编程(Aspect-Oriented Programming,AOP):AOP 允许程序员定义横切关注点,然后通过切面将这些关注点与主业务逻辑分离开来,从而实现更好的模块化和代码复用。


1.示例代码:

Beans.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">
 
    <!-- 声明一个名为 "helloWorld" 的 bean -->
    <bean id="helloWorld" class="com.example.HelloWorld">
        <property name="message" value="Hello, Spring!"/>
    </bean>
</beans>

HelloWorld.java

public class HelloWorld {
    private String message;
 
    public void setMessage(String message) {
        this.message = message;
    }
 
    public void getMessage() {
        System.out.println("Your Message : " + message);
    }
}
 
public class MainApp {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
        HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
        obj.getMessage();
    }
}

二、Spring 数据访问(Spring Data Access)

Spring 数据访问模块提供了对数据访问的支持,包括以下功能:


  • JDBC 模板:简化了 JDBC 编程,提供了更简单和更少样板代码的方式来执行数据库操作。
  • 对象关系映射(Object-Relational Mapping,ORM):Spring 框架支持使用各种 ORM 框架(如 Hibernate、JPA 等)进行持久化操作。
  • 事务管理:Spring 提供了对声明式事务的支持,使得事务管理更加简单和方便。


1.示例代码:

Beans.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">
 
    <!-- 配置数据源 -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/test"/>
        <property name="username" value="root"/>
        <property name="password" value="password"/>
    </bean>
 
    <!-- 配置 JDBC 模板 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>
 
    <!-- 声明一个名为 "studentDAO" 的 bean -->
    <bean id="studentDAO" class="com.example.StudentDAO">
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>
</beans>

StudentDAO.java

public class StudentDAO {
    private JdbcTemplate jdbcTemplate;
 
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
 
    public void create(String name, Integer age) {
        String SQL = "insert into Student (name, age) values (?, ?)";
        jdbcTemplate.update(SQL, name, age);
        System.out.println("Created Record Name = " + name + " Age = " + age);
    }
}
 
public class MainApp {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
        StudentDAO studentDAO = (StudentDAO) context.getBean("studentDAO");
        studentDAO.create("Alice", 20);
    }
}

三、Spring Web 模块

Spring Web 模块提供了对 Web 开发的支持,包括 Spring MVC 和 RESTful Web 服务等。

1.示例代码:

Beans.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">
 
    <!-- 配置 Spring MVC -->
    <context:component-scan base-package="com.example"/>
    <mvc:annotation-driven/>
 
    <!-- 配置视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

HelloWorldController.java

@Controller
public class HelloWorldController {
    @RequestMapping("/hello")
    public ModelAndView helloWorld() {
        String message = "Hello World, Spring MVC!";
        return new ModelAndView("hello", "message", message);
    }
}

以上是 Spring 框架及其各种模块的详细说明和示例代码,包括了 XML 配置部分。Spring 框架提供了强大的功能和丰富的模块,可以大大简化企业级应用程序的开发和维护。

相关文章
|
2天前
|
消息中间件 Java BI
使用Java和Spring Batch实现批处理
使用Java和Spring Batch实现批处理
|
4天前
|
安全 Java 数据安全/隐私保护
使用Java和Spring Security实现身份验证与授权
使用Java和Spring Security实现身份验证与授权
|
4天前
|
消息中间件 负载均衡 Java
Java和Spring Cloud构建分布式系统
Java和Spring Cloud构建分布式系统
|
2天前
|
消息中间件 负载均衡 Java
使用Java和Spring Cloud构建分布式系统
使用Java和Spring Cloud构建分布式系统
|
2天前
|
IDE 前端开发 Java
Java中的Spring框架与企业级应用开发实践
Java中的Spring框架与企业级应用开发实践
|
2天前
|
安全 Java 数据安全/隐私保护
使用Java和Spring Security实现身份验证与授权
使用Java和Spring Security实现身份验证与授权
|
4天前
|
消息中间件 Java BI
使用Java和Spring Batch实现批处理
使用Java和Spring Batch实现批处理
|
2天前
|
监控 安全 Java
Java中的线程调度与性能优化技巧
Java中的线程调度与性能优化技巧
|
2天前
|
缓存 安全 Java
Java中的线程安全问题及解决方案
Java中的线程安全问题及解决方案
|
2天前
|
并行计算 安全 Java
Java中的多线程与并发编程详解
Java中的多线程与并发编程详解