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();
}
}