Metody oznaczone @PostConstruct i @PreDestroy nie wywołują się

0

Mam taki kod, w którym metody init i close z App się nie wywołują. Czego może brakować?

package com.demo.annotationsconfig;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = {
        "com.demo.annotationsconfig"
})
public class SpringConfig {
}

package com.demo.annotationsconfig;

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

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

@Component
@Scope("prototype")
public class App {
    @Autowired
    @Qualifier("logger")
    private Service mainService;

    @Autowired
    private Service[] services;

    private int id;
    private String name;

    public App() {
    }

    public App(int id) {
        this.id = id;
    }

    public App(int id, String name) {
        this.id = id;
        this.name = name;
    }

//    @Autowired
//    public App(@Qualifier("logger") Service mainService) {
//        this.mainService = mainService;
//    }

//    @Autowired
//    public App(Service[] services) {
//        this.services = services;
//    }

    @PostConstruct
    public void init() {
        System.out.println("The application has been initialized");
    }

    @PreDestroy
    public void close() {
        System.out.println("Bye bye");
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Service getMainService() {
        return mainService;
    }

    public void setMainService(Service mainService) {
        this.mainService = mainService;
    }

    public Service[] getServices() {
        return services;
    }

    public void setServices(Service[] services) {
        this.services = services;
    }
}

import com.demo.annotationsconfig.App;
import com.demo.annotationsconfig.SpringConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Test {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);

        App app = context.getBean("app", App.class);
        app.getServices()[0].run();
    }
}
1

Pokaż zależności, wrzuć cały projekt na github.
U mnie działa.
Ale tak pro prawdzie, to szkoda, że działa. :-(
@PostConstruct to straszny syf.
W Javie sa konstruktory i działają zarąbiście i zawsze ( nawet w springu im się zdarza działać, a to już coś).
Jak juz musisz tego Sprina pchać (nie wiadomo po co), to chociaż wywal @Autowired na polach i zacznij korzystać z wstrzykiwania przez konstruktory.
To pierwszy krok do normalności.
http://olivergierke.de/2013/11/why-field-injection-is-evil/

1

Z @PreDestroy problem jest taki że nie działa gdy Bean ma Scope ustawiony na prototype.

1 użytkowników online, w tym zalogowanych: 0, gości: 1