Spring PostConstruct and PreDestroy Annotations – Spring的PostConstruct和PreDestroy注解

最后修改: 2019年 1月 18日

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

1. Introduction

1.绪论

Spring allows us to attach custom actions to bean creation and destruction. We can, for example, do it by implementing the InitializingBean and DisposableBean interfaces.

Spring允许我们在bean创建和销毁时附加自定义动作。例如,我们可以通过实现InitializingBeanDisposableBean接口来实现。

In this quick tutorial, we’ll look at a second possibility, the @PostConstruct and @PreDestroy annotations.

在这个快速教程中,我们将看看第二种可能性,即@PostConstruct@PreDestroy注释。

2. @PostConstruct

2.@PostConstruct

Spring calls the methods annotated with @PostConstruct only once, just after the initialization of bean properties. Keep in mind that these methods will run even if there’s nothing to initialize.

Spring只调用一次用@PostConstruct注释的方法,就在Bean属性的初始化之后。请记住,即使没有任何东西需要初始化,这些方法也会运行。

The method annotated with @PostConstruct can have any access level, but it can’t be static.

@PostConstruct注释的方法可以有任何访问级别,但它不能是静态的。

One possible use of @PostConstruct is populating a database. For instance, during development, we might want to create some default users:

@PostConstruct的一个可能用途是填充数据库。例如,在开发过程中,我们可能想创建一些默认用户。

@Component
public class DbInit {

    @Autowired
    private UserRepository userRepository;

    @PostConstruct
    private void postConstruct() {
        User admin = new User("admin", "admin password");
        User normalUser = new User("user", "user password");
        userRepository.save(admin, normalUser);
    }
}

The above example will first initialize UserRepository and then run the @PostConstruct method.

上面的例子将首先初始化UserRepository,然后运行@PostConstruct方法。

3. @PreDestroy

3.@PreDestroy

A method annotated with @PreDestroy runs only once, just before Spring removes our bean from the application context.

带有@PreDestroy注释的方法只运行一次,就在Spring将我们的Bean从应用上下文中移除之前。

Same as with @PostConstruct, the methods annotated with @PreDestroy can have any access level, but can’t be static.

@PostConstruct一样,用@PreDestroy注释的方法可以有任何访问级别,但不能是静态的。

@Component
public class UserRepository {

    private DbConnection dbConnection;
    @PreDestroy
    public void preDestroy() {
        dbConnection.close();
    }
}

The purpose of this method should be to release resources or perform other cleanup tasks, such as closing a database connection, before the bean gets destroyed.

这个方法的目的应该是在Bean被销毁之前释放资源或执行其他清理任务,比如关闭数据库连接。

4. Java 9+

4.JAVA 9+

Note that both the @PostConstruct and @PreDestroy annotations are part of Java EE. Since Java EE was deprecated in Java 9, and removed in Java 11, we have to add an additional dependency to use these annotations:

请注意,@PostConstruct@PreDestroy注解都是Java EE的一部分。由于Java EE在Java 9中被废弃,并在Java 11中被删除,因此我们必须添加一个额外的依赖来使用这些注释。

<dependency>
    <groupId>javax.annotation</groupId>
    <artifactId>javax.annotation-api</artifactId>
    <version>1.3.2</version>
</dependency>

5. Conclusion

5.总结

In this brief article, we learned how to use the @PostConstruct and @PreDestroy annotations.

在这篇简短的文章中,我们学习了如何使用@PostConstruct@PreDestroy注释。

As always, all the source code is available on GitHub.

一如既往,所有的源代码都可以在GitHub上获得。