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

相关文章
|
1天前
|
监控 负载均衡 Java
Spring Boot与微服务治理框架的集成
Spring Boot与微服务治理框架的集成
|
1天前
|
前端开发 Java API
Spring Boot与GraphQL的集成
Spring Boot与GraphQL的集成
|
1天前
|
开发框架 Java 数据库
Spring Boot集成多数据源的最佳实践
Spring Boot集成多数据源的最佳实践
|
1天前
|
缓存 安全 Java
Spring Boot与GraphQL的集成最佳实践
Spring Boot与GraphQL的集成最佳实践
|
1天前
|
负载均衡 Java Nacos
Spring Boot与微服务治理框架的集成策略
Spring Boot与微服务治理框架的集成策略
|
1天前
|
Java 数据库连接 数据库
Spring Boot与MyBatis的集成应用
Spring Boot与MyBatis的集成应用
|
1天前
|
XML JSON Java
Spring Boot与Solr的集成应用
Spring Boot与Solr的集成应用
|
1天前
|
Java 索引 Spring
教程:Spring Boot中集成Elasticsearch的步骤
教程:Spring Boot中集成Elasticsearch的步骤
|
1天前
|
NoSQL Java MongoDB
如何在Spring Boot应用中集成MongoDB数据库
如何在Spring Boot应用中集成MongoDB数据库
|
1天前
|
消息中间件 Java Spring
实现Spring Boot与RabbitMQ消息中间件的无缝集成
实现Spring Boot与RabbitMQ消息中间件的无缝集成