Spring的PostConstruct和PreDestroy注解
1 介绍
Spring允许我们在bean创建和销毁时,执行自定义的动作。例如,我们可以通过实现InitializingBean 和DisposableBean 接口来实现。
在这篇文章中,我们将使用@PostConstruct和@PreDestroy来完成这一点。
2 @PostConstruct注解
对于Spring来说,它只会在bean初始化之后,对注有@PostConstruct的方法调用一次。需要牢记的是,即使初始化工作啥事也没做,带有@PostConstruct的方法还是会被调用。注有@PostConstruct注解的方法可以是任意的访问级别,但它不能是静态方法。
一个使用@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);
}
}
上面的例子,会首先初始化UserRepository ,之后就会执行@PostConstruct 方法。
3.@PreDestroy注解
在spring把我们的bean移出应用上下文之前,带有@PreDestroy注解的方法会被执行一次。和@PostConstruct注解一样,注有@PostConstruct注解的方法可以是任意的访问级别,但它不能是静态方法。
@Component
public class UserRepository {
private DbConnection dbConnection;
@PreDestroy
public void preDestroy() {
dbConnection.close();
}
}
此方法的作用是在bean销毁之前,释放资源或者是执行其他的清理任务,例如:关闭数据库连接等。
Java9+
@PostConstruct和 @PreDestroy注解是JavaEE的一部分,而从Java9开始,JavaEE就已经过时了,并从Java11中移除出去了。如果我们想使用这俩个注解,就不得不添加额外的依赖。
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
5、源码
package javax.annotation;
import java.lang.annotation.*;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.*;
/**
* The PostConstruct annotation is used on a method that needs to be executed
* after dependency injection is done to perform any initialization. This
* method MUST be invoked before the class is put into service. This
* annotation MUST be supported on all classes that support dependency
* injection. The method annotated with PostConstruct MUST be invoked even
* if the class does not request any resources to be injected. Only one
* method can be annotated with this annotation. The method on which the
* PostConstruct annotation is applied MUST fulfill all of the following
* criteria:
* <p>
* <ul>
* <li>The method MUST NOT have any parameters except in the case of
* interceptors in which case it takes an InvocationContext object as
* defined by the Interceptors specification.</li>
* <li>The method defined on an interceptor class MUST HAVE one of the
* following signatures:
* <p>
* void <METHOD>(InvocationContext)
* <p>
* Object <METHOD>(InvocationContext) throws Exception
* <p>
* <i>Note: A PostConstruct interceptor method must not throw application
* exceptions, but it may be declared to throw checked exceptions including
* the java.lang.Exception if the same interceptor method interposes on
* business or timeout methods in addition to lifecycle events. If a
* PostConstruct interceptor method returns a value, it is ignored by
* the container.</i>
* </li>
* <li>The method defined on a non-interceptor class MUST HAVE the
* following signature:
* <p>
* void <METHOD>()
* </li>
* <li>The method on which PostConstruct is applied MAY be public, protected,
* package private or private.</li>
* <li>The method MUST NOT be static except for the application client.</li>
* <li>The method MAY be final.</li>
* <li>If the method throws an unchecked exception the class MUST NOT be put into
* service except in the case of EJBs where the EJB can handle exceptions and
* even recover from them.</li></ul>
* @since Common Annotations 1.0
* @see javax.annotation.PreDestroy
* @see javax.annotation.Resource
*/
@Documented
@Retention (RUNTIME)
@Target(METHOD)
public @interface PostConstruct {
}
网友评论