一文讲透Spring三种集成方式

简介: 一文讲透Spring三种集成方式

案例一:HelloWorld快速入门(基于xml驱动)

1、新建空项目

这是15年以前的书籍常见的helloword案例,整体结构包含后续的文件结构如下:

2、pom导入依赖

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
    </dependencies>

3、添加spring配置文件

<?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="com.iyyxx.HelloWorld" id="helloWorld"/>
</beans>

3、编写测试代码

public class HelloWorld {
    public String say(){
        return "Hello Spring";
    }
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
//        HelloWorld helloWorld = (HelloWorld) applicationContext.getBean("helloWorld"); //可以用xml中配置的id,或下面用class的方式,更推荐
        HelloWorld helloWorld = (HelloWorld) applicationContext.getBean(HelloWorld.class);
        System.out.println(helloWorld.say());
    }
}

Spring版本有很多,目前2022年10月,市场主流Spring5,最新5.3,这里采用验证合适5.X.X.Release版本

案例二:HelloWorld快速入门(注解驱动)

Spring3流行的时候,市场热门书籍一边倒的使用纯注解驱动,后面证实确实成了趋势!

1、新建空项目

同案例1

2、pom导入依赖

同案例1

3、添加spring配置类

案例1是创建配置文件,这里是创建配置类

@Configuration
public class SpringConfig {
    @Bean
    public HelloWorld helloWorld(){
        return new HelloWorld();
    }
}

3、编写测试代码

案例1采用ClassPathXmlApplicationContext,这里采用注解类AnnotationConfigApplicationContext

public class HelloWorld {
    public String say(){
        return "Hello Spring by Annotation";
    }
    public static void main(String[] args) {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class); //这里采用注解容器类
        HelloWorld helloWorld = (HelloWorld) applicationContext.getBean(HelloWorld.class);
        System.out.println(helloWorld.say());
    }
}

Spring版本有很多,目前2022年10月,市场主流Spring5,最新5.3,这里采用验证合适5.X.X.Release版本,另外注解驱动还有另一种主流写法,具体如下

这也是在SpringBoot时代的常用做法

//另一种写法,用扫描器,Bean也用上@Component标签(mvc还有专有但雷同的标记如@Service@Controller)
@Configuration
@ComponentScan(basePackages = "com.iyyxx")
public class SpringConfig {
    @Bean
    public HelloWorld helloWorld(){
        return new HelloWorld();
    }
}
@Component
public class HelloWorld {
 //...
}

案例三:HelloWorld快速入门(注解驱动,但保留bean.xml)

有些历史项目喜欢这么玩,当年造成了不少困扰,这里补充一下,不过确实是SSM年代的主流!

具体配置与纯注解驱动类似,只是把扫包设置及后续可能要开启aop放到了xml中,并且spring容器管理类依旧使用ClassPathXmlApplicationContext

<?xml version="1.0" encoding="UTF-8"?>
<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"
       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/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 配置spring创建容器时扫描的包 -->
    <context:component-scan base-package="com.iyyxx"></context:component-scan>
    <!-- 使AOP支持注解 -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

结语

Spring 项目集成的三种方式非常轻松的完成了,是不是还不过瘾,后面还有一系列的【一文讲解透】系列分享给大家!

相关文章
|
18天前
|
数据可视化 Java BI
将 Spring 微服务与 BI 工具集成:最佳实践
本文探讨了 Spring 微服务与商业智能(BI)工具集成的潜力与实践。随着微服务架构和数据分析需求的增长,Spring Boot 和 Spring Cloud 提供了构建可扩展、弹性服务的框架,而 BI 工具则增强了数据可视化与实时分析能力。文章介绍了 Spring 微服务的核心概念、BI 工具在企业中的作用,并深入分析了两者集成带来的优势,如实时数据处理、个性化报告、数据聚合与安全保障。同时,文中还总结了集成过程中的最佳实践,包括事件驱动架构、集中配置管理、数据安全控制、模块化设计与持续优化策略,旨在帮助企业构建高效、智能的数据驱动系统。
将 Spring 微服务与 BI 工具集成:最佳实践
|
2月前
|
XML 人工智能 Java
Spring Boot集成Aviator实现参数校验
Aviator是一个高性能、轻量级的Java表达式求值引擎,适用于动态表达式计算。其特点包括支持多种运算符、函数调用、正则匹配、自动类型转换及嵌套变量访问,性能优异且依赖小。适用于规则引擎、公式计算和动态脚本控制等场景。本文介绍了如何结合Aviator与AOP实现参数校验,并附有代码示例和仓库链接。
170 0
|
2月前
|
安全 Java 数据库
第16课:Spring Boot中集成 Shiro
第16课:Spring Boot中集成 Shiro
557 0
|
2月前
|
消息中间件 存储 Java
第15课: Spring Boot中集成ActiveMQ
第15课: Spring Boot中集成ActiveMQ
323 0
|
3月前
|
人工智能 Java 测试技术
Spring Boot 集成 JUnit 单元测试
本文介绍了在Spring Boot中使用JUnit 5进行单元测试的常用方法与技巧,包括添加依赖、编写测试类、使用@SpringBootTest参数、自动装配测试模块(如JSON、MVC、WebFlux、JDBC等),以及@MockBean和@SpyBean的应用。内容实用,适合Java开发者参考学习。
450 0
|
20天前
|
监控 Cloud Native Java
Spring Integration 企业集成模式技术详解与实践指南
本文档全面介绍 Spring Integration 框架的核心概念、架构设计和实际应用。作为 Spring 生态系统中的企业集成解决方案,Spring Integration 基于著名的 Enterprise Integration Patterns(EIP)提供了轻量级的消息驱动架构。本文将深入探讨其消息通道、端点、过滤器、转换器等核心组件,以及如何构建可靠的企业集成解决方案。
82 0
|
6月前
|
安全 Java Apache
微服务——SpringBoot使用归纳——Spring Boot中集成 Shiro——Shiro 身份和权限认证
本文介绍了 Apache Shiro 的身份认证与权限认证机制。在身份认证部分,分析了 Shiro 的认证流程,包括应用程序调用 `Subject.login(token)` 方法、SecurityManager 接管认证以及通过 Realm 进行具体的安全验证。权限认证部分阐述了权限(permission)、角色(role)和用户(user)三者的关系,其中用户可拥有多个角色,角色则对应不同的权限组合,例如普通用户仅能查看或添加信息,而管理员可执行所有操作。
325 0
|
6月前
|
安全 Java 数据安全/隐私保护
微服务——SpringBoot使用归纳——Spring Boot中集成 Shiro——Shiro 三大核心组件
本课程介绍如何在Spring Boot中集成Shiro框架,主要讲解Shiro的认证与授权功能。Shiro是一个简单易用的Java安全框架,用于认证、授权、加密和会话管理等。其核心组件包括Subject(认证主体)、SecurityManager(安全管理员)和Realm(域)。Subject负责身份认证,包含Principals(身份)和Credentials(凭证);SecurityManager是架构核心,协调内部组件运作;Realm则是连接Shiro与应用数据的桥梁,用于访问用户账户及权限信息。通过学习,您将掌握Shiro的基本原理及其在项目中的应用。
255 0
|
2月前
|
Java 关系型数据库 数据库连接
Spring Boot项目集成MyBatis Plus操作PostgreSQL全解析
集成 Spring Boot、PostgreSQL 和 MyBatis Plus 的步骤与 MyBatis 类似,只不过在 MyBatis Plus 中提供了更多的便利功能,如自动生成 SQL、分页查询、Wrapper 查询等。
269 3