丢弃重口味的xml配置--spring4用groovy配置bean(转)

简介: spring4之前,bean的配置可以主要分为两种方式,一种是使用基于xml,个人非常讨厌这种方式,因为明明一件很简单的事,放在xml中就会多了不少繁杂的信息。另一种方式,是从spring3.0开始,spring提供了是基于java的配置,相比于xml的配置方式,看起来会好一点儿。

 

spring4之前,bean的配置可以主要分为两种方式,一种是使用基于xml,个人非常讨厌这种方式,因为明明一件很简单的事,放在xml中就会多了不少繁杂的信息。另一种方式,是从spring3.0开始,spring提供了是基于java的配置,相比于xml的配置方式,看起来会好一点儿。而在几天前release的spring4.0中,我们可以用groovy作为spring的配置文件啦!比起最早的基于xml配置,使用groovy会更加灵活,而且干扰信息会更少。比起基于java的配置,groovy配置还要更加精炼!

这篇博客就对现有的xml,java配置方式进行回顾,然后再简单的展示spring4提供的groovy配置方式。本篇博客的代码在github上:https://github.com/kiwiwin/spring4-bean-demo

 

例子中,我们使用gradle引入依赖:

    compile("org.springframework:spring-core:4.0.0.RELEASE",
            "org.springframework:spring-context:4.0.0.RELEASE")

 

 下面展示一个很简单的例子,一场足球赛,有homeTeam主队和awayTeam客队,这两个bean是通过构造函数传入。另外还有homeScore和awayScore表示主队和客队分别得到的分数,通过property的方式传入。

 

下面比较xml、java、groovy三种配置方式

1.使用xml进行bean配置

football-match-config.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 id="footballMatch" class="org.kiwi.spring.groovy.FootballMatch">
        <constructor-arg ref="homeTeam"/>
        <constructor-arg ref="awayTeam"/>
        <property name="homeScore" value="3"/>
        <property name="awayScore" value="1"/>
    </bean>

    <bean id="homeTeam" class="org.kiwi.spring.groovy.FootballTeam">
        <constructor-arg value="Manchester United"/>
    </bean>

    <bean id="awayTeam" class="org.kiwi.spring.groovy.FootballTeam">
        <constructor-arg value="AC Milan"/>
    </bean>

</beans>

 

虽然我们的IDE已经足够聪明能够帮我们生成xml的各种标签,但是这样的东西看起来确实还是让人觉得不爽。不过如果你真的还是很喜欢这种,那我只能说你开心就好。

2.使用java进行bean配置

首先定义bean:

FootballMatchConfig.java

package org.kiwi.spring.groovy;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FootballMatchConfig {
    @Bean
    public FootballMatch footballMatch() {
        final FootballMatch footballMatch = new FootballMatch(homeTeam(), awayTeam());
        footballMatch.setHomeScore(3);
        footballMatch.setAwayScore(1);
        return footballMatch;
    }

    @Bean
    public FootballTeam homeTeam() {
        return new FootballTeam("Manchester United");
    }

    @Bean
    public FootballTeam awayTeam() {
        return new FootballTeam("AC Milan");
    }
}

 

 

然后从context中定义java配置文件的位置:

football-match-java-config.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"
       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:component-scan
            base-package="org.kiwi.spring.groovy" />
</beans>

 

 

比起基于纯粹xml的bean配置,基于java的bean配置不再受限于xml本身。配置本身更清晰。

3.使用groovy进行bean配置

FootballMatchConfig.groovy

import org.kiwi.spring.groovy.FootballMatch
import org.kiwi.spring.groovy.FootballTeam

beans {
    homeTeam(FootballTeam, "Manchester United")
    awayTeam(FootballTeam, "AC Milan")

    footballMatch(FootballMatch, homeTeam, awayTeam) {
        homeScore = 3
        awayScore = 1
    }
}

 

 

简单吧!谁看到这种简单的配置方式不会心动呢?

与之前的ApplcationContext不同,为了支持基于groovy的bean配置。spring4中提供了新的正对Groovy的

ApplicationContext:GenericGroovyApplicationContext。

public class FootballApp {
    public static void main(String[] args) {
        final GenericGroovyApplicationContext context = new GenericGroovyApplicationContext("FootballMatchConfig.groovy");
        final FootballMatch footballMatch = (FootballMatch) context.getBean("footballMatch");
        System.out.println(footballMatch.display());
    }
}

 

重口味的程序员可能觉得这是吃饱了没事儿撑的用groovy进行bean配置。但是想下maven和gradle,maven基于xml,gradle基于groovy。同样的配置,在gradle中显得十分精炼,同时,因为gradle使用了groovy这样一种语言,而不是xml这种文件,使其更加灵活,在定义task方面会更加的方便。

在这篇博客中,虽然没有写太多基于groovy配置更加强大的方面,但是简洁是显而易见的。简洁就是美。

我们可以非常保守的想一下,在我们所有可以使用xml的地方,其最终都会被其他简洁的语言所替代。

 

温馨提示:

在初次在IntelliJ中尝试使用groovy进行bean配置时,发生过找不到配置文件的问题。这是因为groovy的配置文件后缀是.groovy。IntelliJ的编译器会把它当成是源文件,而不是配置文件。所以需要在Compiler设置中的Resource Patterns那一栏中去掉groovy对应的!?*.groovy即可

 

相关文章
|
3月前
|
负载均衡 监控 Java
Spring Cloud Gateway 全解析:路由配置、断言规则与过滤器实战指南
本文详细介绍了 Spring Cloud Gateway 的核心功能与实践配置。首先讲解了网关模块的创建流程,包括依赖引入(gateway、nacos 服务发现、负载均衡)、端口与服务发现配置,以及路由规则的设置(需注意路径前缀重复与优先级 order)。接着深入解析路由断言,涵盖 After、Before、Path 等 12 种内置断言的参数、作用及配置示例,并说明了自定义断言的实现方法。随后重点阐述过滤器机制,区分路由过滤器(如 AddRequestHeader、RewritePath、RequestRateLimiter 等)与全局过滤器的作用范围与配置方式,提
Spring Cloud Gateway 全解析:路由配置、断言规则与过滤器实战指南
|
3月前
|
Java 关系型数据库 MySQL
Spring Boot自动配置:魔法背后的秘密
Spring Boot 自动配置揭秘:只需简单配置即可启动项目,背后依赖“约定大于配置”与条件化装配。核心在于 `@EnableAutoConfiguration` 注解与 `@Conditional` 系列条件判断,通过 `spring.factories` 或 `AutoConfiguration.imports` 加载配置类,实现按需自动装配 Bean。
|
3月前
|
人工智能 Java 开发者
【Spring】原理解析:Spring Boot 自动配置
Spring Boot通过“约定优于配置”的设计理念,自动检测项目依赖并根据这些依赖自动装配相应的Bean,从而解放开发者从繁琐的配置工作中解脱出来,专注于业务逻辑实现。
1307 0
|
5月前
|
Java Spring
Spring Boot配置的优先级?
在Spring Boot项目中,配置可通过配置文件和外部配置实现。支持的配置文件包括application.properties、application.yml和application.yaml,优先级依次降低。外部配置常用方式有Java系统属性(如-Dserver.port=9001)和命令行参数(如--server.port=10010),其中命令行参数优先级高于系统属性。整体优先级顺序为:命令行参数 &gt; Java系统属性 &gt; application.properties &gt; application.yml &gt; application.yaml。
995 0
|
2月前
|
前端开发 Java 应用服务中间件
《深入理解Spring》 Spring Boot——约定优于配置的革命者
Spring Boot基于“约定优于配置”理念,通过自动配置、起步依赖、嵌入式容器和Actuator四大特性,简化Spring应用的开发与部署,提升效率,降低门槛,成为现代Java开发的事实标准。
|
3月前
|
缓存 Java 应用服务中间件
Spring Boot配置优化:Tomcat+数据库+缓存+日志,全场景教程
本文详解Spring Boot十大核心配置优化技巧,涵盖Tomcat连接池、数据库连接池、Jackson时区、日志管理、缓存策略、异步线程池等关键配置,结合代码示例与通俗解释,助你轻松掌握高并发场景下的性能调优方法,适用于实际项目落地。
607 5
|
3月前
|
传感器 Java 数据库
探索Spring Boot的@Conditional注解的上下文配置
Spring Boot 的 `@Conditional` 注解可根据不同条件动态控制 Bean 的加载,提升应用的灵活性与可配置性。本文深入解析其用法与优势,并结合实例展示如何通过自定义条件类实现环境适配的智能配置。
201 0
探索Spring Boot的@Conditional注解的上下文配置
|
4月前
|
安全 算法 Java
在Spring Boot中应用Jasypt以加密配置信息。
通过以上步骤,可以在Spring Boot应用中有效地利用Jasypt对配置信息进行加密,这样即使配置文件被泄露,其中的敏感信息也不会直接暴露给攻击者。这是一种在不牺牲操作复杂度的情况下提升应用安全性的简便方法。
1043 10
|
5月前
|
人工智能 安全 Java
Spring Boot yml 配置敏感信息加密
本文介绍了如何在 Spring Boot 项目中使用 Jasypt 实现配置文件加密,包含添加依赖、配置密钥、生成加密值、在配置中使用加密值及验证步骤,并提供了注意事项,确保敏感信息的安全管理。
1190 1
|
5月前
|
SQL XML Java
配置Spring框架以连接SQL Server数据库
最后,需要集成Spring配置到应用中,这通常在 `main`方法或者Spring Boot的应用配置类中通过加载XML配置或使用注解来实现。
468 0

热门文章

最新文章