簡體   English   中英

@PostConstruct 和 @PreDestroy 注釋不起作用

[英]@PostConstruct and @PreDestroy annotations do not work

我有網球教練課:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Component
public class TennisCoach implements Coach {
    @Autowired
    @Qualifier("randomFortuneService")
    private FortuneService fortuneService;

    public TennisCoach() {
        System.out.println("Inside default constructor");
    }

    @PostConstruct
    public void doMyStartupStuff() {
        System.out.println("Inside postconstructor");
    }

    @PreDestroy
    public void doMyFinalStuff() {
        System.out.println("Inside predestroyer");
    }

    @Override
    public String getDailyFortune() {
        return fortuneService.getFortune();
    }

    @Override
    public String getDailyWorkout() {
        return "Practice your backhand volley";
    }
}

還有這樣的主類:

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AnnotationBeanScopeDemoApp {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("applicationContext.xml");

        Coach theCoach = context.getBean("tennisCoach", Coach.class);

        System.out.println(theCoach.getDailyFortune());
        System.out.println(theCoach.getDailyWorkout());

        context.close();
    }
}

預期輸出為:

默認構造函數內部

內部后構造函數

每日財富

練習你的反手截擊

驅逐艦內部

但我明白了:

默認構造函數內部

每日財富

練習你的反手截擊

@PostContruct 和 @PreDestroy 注釋似乎無法正常工作。 我找不到問題的原因。

默認情況下,Spring 可能不處理 JSR-250 生命周期注釋。 檢查您的applicationContext.xml是否包含元素:

<beans ...
    <context:annotation-config/>
    ....
</beans>

配置 Spring 以了解@PostConstruct@PrePersist

欲了解更多信息,請參閱這篇文章

來自Spring Core文檔的第1.9.8 - Using @PostConstruct and @PreDestroy1.9.8 - Using @PostConstruct and @PreDestroy

如果CommonAnnotationBeanPostProcessor已在 Spring ApplicationContext注冊,則在生命周期中與相應的 Spring 生命周期接口方法或顯式聲明的回調方法相同的點調用攜帶這些注釋之一的方法。

申報

<context:annotation-config/>

applicationContext.xml文件中,隱式注冊一個CommonAnnotationBeanPostProcessor

否則,您需要使用 ( docs ) 手動注冊它

<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />

如果您使用 java 9+,則添加依賴項javax.annotation:javax.annotation-api (或使用 java 8)。

或者,如果您使用 spring-boot 1,請使用 spring-boot 2。

您的 Spring 不知道注釋的 @PostContruct 和 @PreDestroy。 在 applicationContext.xml 中添加此代碼

<bean class = "org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>

或者在 TenisCoach 類中實現 InitializingBean、DisposableBean 接口。

Eclipse 無法導入 @PostConstruct 或 @PreDestroy

這是因為 Java 9 及更高版本。

使用 Java 9 及更高版本時,javax.annotation 已從其默認類路徑中刪除。 解決方案

  1. 下載 javax.annotation-api-1.3.2.jar 從

maven.org

  1. 將 JAR 文件復制到項目的 lib 文件夾中

使用以下步驟將其添加到您的 Java 構建路徑。

  1. 右鍵單擊您的項目,選擇屬性

  2. 在左側,單擊 Java Build Path

  3. 在對話框的頂部中心,單擊庫

  4. 單擊 Classpath,然后單擊 Add JARs ...

  5. 導航到 JAR 文件 /lib/javax.annotation-api-1.3.2.jar

  6. 單擊確定,然后單擊應用並關閉

Eclipse 將重新構建您的項目並解決相關的構建錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM