亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java中的@PostConstruct注解的使用

 更新時間:2023年06月25日 08:38:25   作者:wild_man  
本文主要介紹了Java中的@PostConstruct注解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

問題

之前在做后端項目遇到一個奇怪的問題,我裝配到Spring容器中的一個Bean在另外一個類中無法被注入,該Bean的類型如下:

@Component
@Data
public class FeishuCrawlerConfig{...}

我使用@Component注解將其裝配到Spring容器中,然后在另外一個類中將其自動注入,格式如下:

@Component
public class FeishuTenantTokenEntity {
    @Autowired
    private FeishuCrawlerConfig feishuConfig;
    ...
}

正常來講feishuConfig這個變量會被自動注入完成初始化,但是后面我在使用這個變量時卻拋出了NPE。后來經過跟同事的討論才發(fā)現(xiàn)我是在FeishuTenantTokenEntity的構造方法中調用到了feishuConfig的方法,導致了NPE的產生。構造方法如下:

public FeishuTenantTokenEntity() IOException {
    // refreshTokenValue()方法中使用到feishuConfig的方法
    this.tokenValue = refreshTokenValue();
    this.accessTime = System.currentTimeMillis();
}

產生這個問題的原因是Spring中的Bean在初始化時,會先執(zhí)行其構造方法,再注入@Autowired注解標注的其他Bean。我們的代碼中在構造方法中就使用到了@Autowired注入的feishuConfig對象,這時它還沒有初始化完成,自然會拋出NPE。這個問題的解決方法就是使用@PostConstruct注解標注的方法替代FeishuTenantTokenEntity的構造方法,該方法如下:

@PostConstruct
public void init() throws IOException {
    this.tokenValue = refreshTokenValue();
    this.accessTime = System.currentTimeMillis();
}

Spring在初始化Bean時,會在注入@Autowired注解標注的Bean后執(zhí)行@PostConstruct注解標注的方法。我們在init()方法中使用feishuConfig的方法顯然是沒問題的,并且它還能替代構造方法的作用。Spring中Bean初始化的執(zhí)行順序是構造方法>依賴注入( @Autowired )> @PostConstruct標注的方法。

@PostConstruct注解

講到這里,不得不細說一下@PostConstruct注解。

/**
 * 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 dependenc
 * 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 &#060;METHOD&#062;(InvocationContext)
 * <p>
 * Object &#060;METHOD&#062;(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 &#060;METHOD&#062;()
 * </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 {
}

這是Java官方對@PostConstruct注解的注釋文檔,可以看到@PostConstruct注解是用于初始化方法上,該方法在依賴注入完成后執(zhí)行。Java對被@PostConstruct標注的方法做出了如下限制:

  • 除攔截器方法外,該方法不能有任何參數(shù)
  • @PostConstruct標注的攔截器方法不能拋出application異常
  • 該方法可以被public,protected,package private和private修飾
  • 該方法不能為靜態(tài)的,但是可以被final修飾 簡而言之,如果你的類中的構造方法需要使用到依賴注入的變量,你可以用@PostConstruct標注的方法來替代構造方法完成初始化。

Spring如何實現(xiàn) @PostConstruct注解

我們知道在Spring中,Bean的初始化一般分為實例化,屬性賦值,初始化和銷毀這四個過程。在實例化階段的前后,InstantiationAwareBeanPostProcessor接口的postProcessBeforeInstantiation和postProcessAfterInstantiation方法會被調用。在初始化階段的前后,BeanPostProcessor接口的postProcessBeforeInitialization和postProcessAfterInitialization方法會被調用。開發(fā)者也可以繼承這些接口拓展功能。如下圖所示:

在這四個過程中間,Bean的依賴注入發(fā)生在屬性賦值這個階段。Spring會在postProcessBeforeInstantiation方法中也就是依賴注入完成之后調用@PostConstruct注解標注的方法,完成Bean的部分初始化工作。 Spring的具體做法就是在創(chuàng)建Bean時,會將它里面被@PostConstruct注解標注的方法保存到Bean的元數(shù)據(jù)中,在后面調用postProcessBeforeInstantiation方法時,會利用反射調用Bean的元數(shù)據(jù)中被 @PostConstruct注解標注的方法,從而完成部分初始化工作。感興趣的同學可以看看源碼。

注意

Java官方已在Java 9中棄用了@PostConstruct注解,并在Java 11中刪除了@PostConstruct注解。 實現(xiàn)InitializingBean接口并重寫其中的afterPropertiesSet方法也可以實現(xiàn)@PostConstruct注解相同的功能。

/**
 * Interface to be implemented by beans that need to react once all their properties
 * have been set by a {@link BeanFactory}: e.g. to perform custom initialization,
 * or merely to check that all mandatory properties have been set.
 *
 * <p>An alternative to implementing {@code InitializingBean} is specifying a custom
 * init method, for example in an XML bean definition. For a list of all bean
 * lifecycle methods, see the {@link BeanFactory BeanFactory javadocs}.
 *
 * @author Rod Johnson
 * @author Juergen Hoeller
 * @see DisposableBean
 * @see org.springframework.beans.factory.config.BeanDefinition#getPropertyValues()
 * @see org.springframework.beans.factory.support.AbstractBeanDefinition#getInitMethodName()
 */
public interface InitializingBean {
   /**
    * Invoked by the containing {@code BeanFactory} after it has set all bean properties
    * and satisfied {@link BeanFactoryAware}, {@code ApplicationContextAware} etc.
    * <p>This method allows the bean instance to perform validation of its overall
    * configuration and final initialization when all bean properties have been set.
    * @throws Exception in the event of misconfiguration (such as failure to set an
    * essential property) or if initialization fails for any other reason
    */
   void afterPropertiesSet() throws Exception;
}

到此這篇關于Java中的@PostConstruct注解的使用的文章就介紹到這了,更多相關Java @PostConstruct注解內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論