Groovy Bean Definitions – Groovy Bean的定义

最后修改: 2017年 11月 7日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this quick article, we’ll focus on how we can use a Groovy-based configuration in our Java Spring projects.

在这篇短文中,我们将重点讨论如何在我们的Java Spring项目中使用基于Groovy的配置。

2. Dependencies

2.依赖性

Before we start, we need to add the dependency to our pom.xml file. We need to also add a plugin for the sake of compiling our Groovy files.

在我们开始之前,我们需要在我们的pom.xml文件中添加依赖性。为了编译我们的Groovy文件,我们还需要添加一个插件。

Let’s add the dependency for Groovy first to our pom.xml file:

让我们先在pom.xml文件中添加Groovy的依赖性。

<dependency>
    <groupId>org.codehaus.groovy</groupId>
    <artifactId>groovy</artifactId>
    <version>2.5.10</version>
</dependency>

Now, let’s add the plugin:

现在,让我们来添加这个插件。

<build>
    <plugins>
        //...
        <plugin>
            <groupId>org.codehaus.gmavenplus</groupId>
            <artifactId>gmavenplus-plugin</artifactId>
            <version>1.9.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>addSources</goal>
                        <goal>addTestSources</goal>
                        <goal>generateStubs</goal>
                        <goal>compile</goal>
                        <goal>generateTestStubs</goal>
                        <goal>compileTests</goal>
                        <goal>removeStubs</goal>
                        <goal>removeTestStubs</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Here, we use gmavenplus-plugin with all the goals.

在这里,我们使用gmavenplus-plugin与所有目标

The latest versions of these libraries can be found on Maven Central.

这些库的最新版本可以在Maven Central上找到。

3. Defining Beans

3.Bean的定义

Since version 4, Spring provides support for Groovy-based configurations. This means that Groovy classes can be legitimate Spring beans.

从第4版开始,Spring提供了对基于Groovy的配置的支持。这意味着,Groovy类可以成为合法的Spring Bean。

To illustrate this, we’re going to define a bean using the standard Java configuration and then we’re going to configure the same bean using Groovy. This way, we’ll be able to see the difference.

为了说明这一点,我们要用标准的Java配置来定义一个Bean,然后用Groovy来配置同一个Bean。这样一来,我们就能看到其中的差别。

Let’s create a simple class with a few properties:

让我们创建一个有几个属性的简单类。

public class JavaPersonBean {
    private String firstName;
    private String lastName;

    // standard getters and setters
}

It’s important to remember about getters/setters – they’re crucial for the mechanism to work.

记住getters/setters很重要–它们对机制的运作至关重要。

3.1. Java Configuration

3.1. Java配置

We can configure the same bean using a Java-based configuration:

我们可以使用基于Java的配置来配置同一个Bean。

@Configuration
public class JavaBeanConfig {

    @Bean
    public JavaPersonBean javaPerson() {
        JavaPersonBean jPerson = new JavaPersonBean();
        jPerson.setFirstName("John");
        jPerson.setLastName("Doe");
        
        return jPerson;
    }
}

3.2. Groovy Configuration

3.2.Groovy配置

Now, we can see the difference when we use Groovy to configure the previously created bean:

现在,我们可以看到当我们使用Groovy来配置之前创建的Bean时的不同之处。

beans {
    javaPersonBean(JavaPersonBean) {
        firstName = 'John'
        lastName = 'Doe'
    }
}

Note that before defining beans configuration, we should import the JavaPersonBean class. Also, inside the beans block, we can define as many beans as we need.

请注意,在定义Bean配置之前,我们应该导入JavaPersonBean类。此外,beans块中,我们可以根据需要定义任意多的bean。

We defined our fields as private and although Groovy makes it look like it’s accessing them directly, it’s doing it using provided getters/setters.

我们把我们的字段定义为私有,虽然Groovy让它看起来像是直接访问它们,但它是通过提供的getters/setters来做的。

4. Additional Bean Settings

4.其他Bean设置

As with the XML and Java-based configuration, we can configure not only beans.

与基于XML和Java的配置一样,我们不仅可以配置Bean。

If we need to set an alias for our bean, we can do it easily:

如果我们需要为我们的Bean设置一个alias,我们可以轻松做到。

registerAlias("bandsBean","bands")

If we want to define the bean’s scope:

如果我们想定义Bean的范围:的话

{ 
    bean ->
        bean.scope = "prototype"
}

To add lifecycle callbacks for our bean, we can do:

为了给我们的Bean添加生命周期回调,我们可以这样做。

{ 
    bean ->
        bean.initMethod = "someInitMethod"
        bean.destroyMethod = "someDestroyMethod"
}

We can also specify inheritance in the bean definition:

我们也可以在Bean定义中指定继承性。

{ 
    bean->
        bean.parent="someBean"
}

Finally, if we need to import some previously defined beans from an XML configuration, we can do this using the importBeans():

最后,如果我们需要从XML配置中导入一些先前定义的Bean,我们可以使用importBeans():来实现。

importBeans("somexmlconfig.xml")

5. Conclusion

5.结论

In this tutorial, we saw how we to create Spring Groovy bean configurations. We also covered setting additional properties on our beans such as their aliases, scopes, parents, methods for initialization or destruction, and how to import other XML-defined beans.

在本教程中,我们看到了如何创建Spring Groovy Bean的配置。我们还介绍了如何为Bean设置额外的属性,如别名、作用域、父类、初始化或销毁的方法,以及如何导入其他XML定义的Bean。

Although the examples are simple, they can be extended and used for creating any type of Spring config.

虽然这些例子很简单,但它们可以被扩展并用于创建任何类型的Spring配置。

A full example code that is used in this article can be found in our GitHub project. This is a Maven project, so you should be able to import it and run it as it is.

本文使用的完整示例代码可以在我们的GitHub项目中找到。这是一个Maven项目,所以你应该能够导入它并按原样运行。